如何使返回类型混合和self?

问题描述 投票:0回答:1
class Foo 
{
    final public function get(string $path): mixed
    {
        // return mixed or self type;
    }

    final public function otherMethod()
    {

    }
}

在此示例中,返回值可以是任何类型,包括。自己的对象(

static
)。

如何确保PhpStorm编辑器可以提供如下提示:

$obj->get('path.to.any.data')->otherMethod();

目前,由于类型原因,混合编辑器无法提供提示。

php phpstorm
1个回答
0
投票

PHPStorm 提供了向项目添加元文件的功能,可用于根据方法参数值告诉 PHPStorm 期望哪种返回类型。

但是,这可能不适合纯动态路径/值。

请参阅:https://www.jetbrains.com/help/phpstorm/ide-advanced-metadata.html

有很多可以使用的可能性。一个基本的例子来感受一下(免责声明:未经测试):

<?php

// FILENAME: ./.phpstorm.meta.php

/**
 * Extend PhpStorms code completion capabilities by providing a meta file
 *
 * @link https://www.jetbrains.com/help/phpstorm/ide-advanced-metadata.html
 */

namespace PHPSTORM_META {

    expectedArguments(
        \Vendor\Namespace\Foo::get(),
        0,
        'path.to.any.data',
        'path.to.some.class',
    );
    override(Vendor\Namespace\Foo::get(), map([
        'path.to.any.data' => 1234123,
        'path.to.some.class' => \Vendor\Namespace\SomeClassOrInterface::class,
    ]));

}
© www.soinside.com 2019 - 2024. All rights reserved.