【发布时间】:2018-07-31 23:38:05
【问题描述】:
我有一个名为 BaseSniff 的基类,并从中派生了几个类。
BaseSniff 包含一个名为 PHP_CodeSniffer_File 类型的成员 $phpcsFile。
在编辑BaseSniff 时,PhpStorm 将在使用$this->phpcsFile 时自动完成,但在派生类中这不起作用。
有没有办法做到这一点?
这是基类的声明方式
/**
* Base class for all Cardstream code sniffs.
*
* The class is constructed from a
* list of tokens to register
* list of code violations to check
* This class implements the 'process' method of the PHP_CodeSniffer_Sniff
* class.
* Derived classes are able to execute code before the actual processing of
* the token via the 'preProcess' method.
* The code violations are then processed and errors and warnings are reported.
*
* A code violation can stop the processing of the remaining
* violations for the file.
*
* @author Graham Labdon <graham.labdon@cardstream.com>
*/
abstract class BaseSniff implements \PHP_CodeSniffer_Sniff {
public function process(\PHP_CodeSniffer_File $phpcs_file, $stack_ptr) {
$this->phpcsFile = $phpcs_file;
}
/**
* Creates the sniff.
*
* Initialises the sniff
*
* @param array $register Array of tokens to register
* @param array $code_violations List of code violations to check
* @param string $sniff_name Name of sniff
* @return void
*/
public function __construct(
array $register,
array $code_violations,
$sniff_name
) {
$this->firstCall = true;
$this->registeredTokens = $register;
$this->codeViolations = $code_violations;
$this->sniffName = $sniff_name;
}
}
这是一个派生类
class MultilineFunctionCallSniff extends BaseSniff {
public function __construct() {
$violations = array();
$register = \PHP_CodeSniffer_Tokens::$functionNameTokens;
parent::__construct(
$register,
$violations,
"MultilineFunctionCall"
);
}
}
【问题讨论】:
-
请提供
BaseSniff的代码示例(它是如何定义的,尤其是那个字段)+ IDE 告诉的内容(完成不起作用的地方)。您很可能做错了什么(遗漏了一些内容)......因为它是一个绝对有效的基本功能。 -
已编辑问题以显示详细信息
-
所以..哪里/什么不起作用?据我了解
$this->phpcsFile在父\PHP_CodeSniffer_Sniff类中声明? -
在基类中我可以输入 $this->phpcsFile 然后按 ctrl+space 来完成。如果我在派生类中做同样的事情,我找不到完成
-
对不起......但这是非常广泛的描述。需要更具体一点......因为代码示例太短且有限。但根据目前的信息,我可能会说:为
phpcsFile提供适当的类型提示。由于它是在某个父类上声明的(不是由您创建的)...您可以使用 PHPDoc 通过@property标签覆盖它以进行类注释 --@property \PHP_CodeSniffer_File $phpcs_file (optional description here))
标签: phpstorm