使用 PHPDoc @属性 和 @method 解决 PhpStorm 魔术方法和 属性 警告 $this->stdout->styles
Resolve PhpStorm magic method and property warning using PHPDoc @property and @method for $this->stdout->styles
我正在尝试解决
"Field 'stdout' not found in ..."
和
"Method ... not found in array"
警告在 PhpStorm 上使用 PHPDocumentor 符号 @property
和 @method
我能够使用以下方法解决标准输出的警告:
* @property array stdout
但是现在如何解决 Method 'styles' not found in array
警告? (见上面的截图)
我编写了这段代码来演示我要实现的目标:
* @method array $stdout->styles(array $name, array $items)
这是一个使用 CakePhp 命令的 CakePhp 2 项目 Shell。
stdout->styles
在框架中声明。
有关更多上下文,这是我的代码的样子:
<?
class InvoiceShell extends AppShell {
public $uses = array('Invoice', 'Request');
public function main() {
$this->stdout->styles('success', ['text' => 'green']);
$this->stdout->styles('danger', ['text' => 'red']);
$this->stdout->styles('bold', ['bold' => true]);
.
.
.
}
}
您需要通过在 @property
声明中将 array
替换为 ConsoleOutput
来告诉 PHPDoc/PhpStorm stdout
的正确类型。
/**
* My cool class is cool.
*
* @property ConsoleOutput $stdout
*/
如果对象是具有自己的魔法访问器的通用容器,您可以声明一个接口,仅供 PhpStorm 完成代码。只需在项目的源文件夹之一的文件中声明它即可。
/**
* @method mixed styles(string, mixed)
* @method ...
*/
interface FakeForCodeCompletion { }
然后在您的 class 中使用 @property
引用 FakeForCodeCompletion
(最好是描述性名称),就好像它是真实的 class/interface。
有类似问题的可以用这个
/** @var $items \Data\Items | mixed */
$items = new \Data\Items;
$items->method('data');
$items->item->method('data');
我正在尝试解决
"Field 'stdout' not found in ..."
和
"Method ... not found in array"
警告在 PhpStorm 上使用 PHPDocumentor 符号 @property
和 @method
我能够使用以下方法解决标准输出的警告:
* @property array stdout
但是现在如何解决 Method 'styles' not found in array
警告? (见上面的截图)
我编写了这段代码来演示我要实现的目标:
* @method array $stdout->styles(array $name, array $items)
这是一个使用 CakePhp 命令的 CakePhp 2 项目 Shell。
stdout->styles
在框架中声明。
有关更多上下文,这是我的代码的样子:
<?
class InvoiceShell extends AppShell {
public $uses = array('Invoice', 'Request');
public function main() {
$this->stdout->styles('success', ['text' => 'green']);
$this->stdout->styles('danger', ['text' => 'red']);
$this->stdout->styles('bold', ['bold' => true]);
.
.
.
}
}
您需要通过在 @property
声明中将 array
替换为 ConsoleOutput
来告诉 PHPDoc/PhpStorm stdout
的正确类型。
/**
* My cool class is cool.
*
* @property ConsoleOutput $stdout
*/
如果对象是具有自己的魔法访问器的通用容器,您可以声明一个接口,仅供 PhpStorm 完成代码。只需在项目的源文件夹之一的文件中声明它即可。
/**
* @method mixed styles(string, mixed)
* @method ...
*/
interface FakeForCodeCompletion { }
然后在您的 class 中使用 @property
引用 FakeForCodeCompletion
(最好是描述性名称),就好像它是真实的 class/interface。
有类似问题的可以用这个
/** @var $items \Data\Items | mixed */
$items = new \Data\Items;
$items->method('data');
$items->item->method('data');