phpstan deprecation rules
2.0.1
要使用此扩展名,请在作曲家中需要它:
composer require --dev phpstan/phpstan-deprecation-rules
如果您还安装了phpstan/Extension-installer,那么您将全部设置!
如果您不想使用phpstan/extension-installer ,请在项目的phpstan config中包含规则。
includes:
- vendor/phpstan/phpstan-deprecation-rules/rules.neon
此扩展名在代码上发出折旧警告,该警告使用属性/函数/方法/类,这些属性为@deprecated 。
如果您不拥有要被视为弃用的代码,请使用phpstan存根文件来声明供应商文件的弃用,例如:
/** @deprecated */
class ThirdPartyClass {}
不弃用的代码的使用情况也未报告:
/** @deprecated */
function doFoo (): void
{
// not reported:
anotherDeprecatedFunction ();
}如果您有不同的标记代码的方式,该代码是故意调用不弃用符号的,并且您也不希望这些呼叫被报告,则可以通过实现DeprecatedScopeResolver接口来编写扩展程序。
例如,如果您标记了使用@group legacy测试不推荐代码的PHPUNIT测试,则可以以这种方式实现扩展名:
class GroupLegacyScopeResolver implements DeprecatedScopeResolver
{
public function isScopeDeprecated ( Scope $ scope ): bool
{
$ function = $ scope -> getFunction ();
return $ function !== null
&& $ function -> getDocComment () !== null
&& strpos ( $ function -> getDocComment (), ' @group legacy ' ) !== false ;
}
}并在您的配置文件中注册:
services :
-
class : GroupLegacyScopeResolver
tags :
- phpstan.deprecations.deprecatedScopeResolver了解有关范围的更多信息,范围是实现自定义Phpstan扩展名的核心概念。