sirbrillig/phpcs-variable-analysis
Composer 安装命令:
composer require --dev sirbrillig/phpcs-variable-analysis
包简介
A PHPCS sniff to detect problems with variables.
关键字:
README 文档
README
Plugin for PHP_CodeSniffer static analysis tool that adds analysis of problematic variable use.
- Warns if variables are used without being defined. (Sniff code:
VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedVariable) - Warns if variables are used inside
unset()without being defined. (Sniff code:VariableAnalysis.CodeAnalysis.VariableAnalysis.UndefinedUnsetVariable) - Warns if variables are set or declared but never used. (Sniff code:
VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable) - Warns if
$this,self::$static_member,static::$static_memberis used outside class scope. (Sniff codes:VariableAnalysis.CodeAnalysis.VariableAnalysis.SelfOutsideClassorVariableAnalysis.CodeAnalysis.VariableAnalysis.StaticOutsideClass)
Installation
Requirements
VariableAnalysis requires PHP 5.4 or higher and PHP CodeSniffer version 3.13.5 or higher.
It also depends on PHPCSUtils version 1.0 or higher. If you install VariableAnalysis with Composer (the recommended method below), this dependency is installed for you automatically. If you install it standalone, you must install PHPCSUtils yourself; see the Standalone instructions.
With PHPCS Composer Installer
This is the easiest method.
First, install phpcodesniffer-composer-installer for your project if you have not already. This will also install PHPCS.
composer config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
composer require --dev dealerdirect/phpcodesniffer-composer-installer
Then install these standards.
composer require --dev sirbrillig/phpcs-variable-analysis
You can then include the sniffs by adding a line like the following to your phpcs.xml file.
<rule ref="VariableAnalysis"/>
It should just work after that!
Standalone
-
Install PHP_CodeSniffer (PHPCS) by following its installation instructions (via Composer, Phar file, PEAR, or Git checkout).
Do ensure that PHP_CodeSniffer's version matches our requirements.
-
Install VariableAnalysis. Download either the zip or tar.gz file from the VariableAnalysis latest release page. Expand the file and rename the resulting directory to
phpcs-variable-analysis. Move the directory to a place where you'd like to keep all your PHPCS standards. -
Install PHPCSUtils, which VariableAnalysis depends on. Download either the zip or tar.gz file from the PHPCSUtils latest release page. Expand the file and rename the resulting directory to
PHPCSUtils. Move the directory to the same place where you keep your PHPCS standards.Do ensure that PHPCSUtils' version matches our requirements.
-
Add the paths of the newly installed standards to the PHP_CodeSniffer installed_paths configuration. The following command should append the new standards to your existing standards (be sure to supply the actual paths to the directories you created above).
phpcs --config-set installed_paths "$(phpcs --config-show|grep installed_paths|awk '{ print $2 }'),/path/to/phpcs-variable-analysis,/path/to/PHPCSUtils"If you do not have any other standards installed, you can do this more easily (again, be sure to supply the actual paths):
phpcs --config-set installed_paths /path/to/phpcs-variable-analysis,/path/to/PHPCSUtils
Customization
There's a variety of options to customize the behaviour of VariableAnalysis, take a look at the included ruleset.xml.example for commented examples of a configuration.
The available options are as follows:
allowUnusedFunctionParameters(bool, defaultfalse): if set to true, function arguments will never be marked as unused.allowUnusedCaughtExceptions(bool, defaulttrue): if set to true, caught Exception variables will never be marked as unused.allowUnusedParametersBeforeUsed(bool, defaulttrue): if set to true, unused function arguments will be ignored if they are followed by used function arguments.allowUnusedVariablesBeforeRequire(bool, defaultfalse): if set to true, variables defined before arequire,require_once,include, orinclude_oncewill not be marked as unused. They may be intended for the required file.allowUndefinedVariablesInFileScope(bool, defaultfalse): if set to true, undefined variables in the file's top-level scope will never be marked as undefined. This can be useful for template files which use many global variables defined elsewhere.allowUnusedVariablesInFileScope(bool, defaultfalse): if set to true, unused variables in the file's top-level scope will never be marked as unused. This can be helpful when defining a lot of global variables to be used elsewhere.validUnusedVariableNames(string, defaultnull): a space-separated list of names of placeholder variables that you want to ignore from unused variable warnings. For example, to ignore the variables$junkand$unused, this could be set to'junk unused'.ignoreUnusedRegexp(string, defaultnull): a PHP regexp string (note that this requires explicit delimiters) for variables that you want to ignore from unused variable warnings. For example, to ignore the variables$_junkand$_unused, this could be set to'/^_/'.validUndefinedVariableNames(string, defaultnull): a space-separated list of names of placeholder variables that you want to ignore from undefined variable warnings. For example, to ignore the variables$postand$undefined, this could be set to'post undefined'. This can be used in combination withvalidUndefinedVariableRegexp.validUndefinedVariableRegexp(string, defaultnull): a PHP regexp string (note that this requires explicit delimiters) for variables that you want to ignore from undefined variable warnings. For example, to ignore the variables$postand$undefined, this could be set to'/^(post|undefined)$/'. This can be used in combination withvalidUndefinedVariableNames.allowUnusedForeachVariables(bool, defaulttrue): if set to true, unused values from thekey => valuesyntax in aforeachloop will never be marked as unused.sitePassByRefFunctions(string, defaultnull): a list of custom functions which pass in variables to be initialized by reference (egpreg_match()) and therefore should not require those variables to be defined ahead of time. The list is space separated and each entry is of the formfunctionName:1,2. The function name comes first followed by a colon and a comma-separated list of argument numbers (starting from 1) which should be considered variable definitions. The special value...in the arguments list will cause all arguments after the last number to be considered variable definitions.allowWordPressPassByRefFunctions(bool, defaultfalse): if set to true, a list of common WordPress pass-by-reference functions will be added to the list of PHP ones so that passing undefined variables to these functions (to be initialized by reference) will be allowed.
To set these these options, you must use XML in your ruleset. For details, see the phpcs customizable sniff properties page. Here is an example that ignores all variables that start with an underscore:
<rule ref="VariableAnalysis.CodeAnalysis.VariableAnalysis"> <properties> <property name="ignoreUnusedRegexp" value="/^_/"/> </properties> </rule>
See Also
- ImportDetection: A set of phpcs sniffs to look for unused or unimported symbols.
- phpcs-changed: Run phpcs on files, but only report warnings/errors from lines which were changed.
Original
This was forked from the excellent work in https://github.com/illusori/PHP_Codesniffer-VariableAnalysis
Contributing
Please open issues or PRs on this repository.
Any changes should be accompanied by tests and should pass linting and static analysis. Please use phpdoc (rather than actual types) for declaring types since this must run in PHP 5.4.
To run tests, make sure composer is installed, then run:
composer install # you only need to do this once
composer test
To run linting, use:
composer lint
To run static analysis, use:
composer static-analysis
sirbrillig/phpcs-variable-analysis 适用场景与选型建议
sirbrillig/phpcs-variable-analysis 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 51.86M 次下载、GitHub Stars 达 145, 最近一次更新时间为 2017 年 11 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「static analysis」 「phpcs」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sirbrillig/phpcs-variable-analysis 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sirbrillig/phpcs-variable-analysis 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sirbrillig/phpcs-variable-analysis 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting
TwigStan is a static analyzer for Twig templates powered by PHPStan
The coding standard applying to all Youdot PHP projects, based on Doctrine set of PHPCS rules, with additional checks.
CodeSniffer ruleset for the ZettaCMS coding standard
Sentiment analysis library for PHP.
Database Standardization and Analysis Tool for Laravel
统计信息
- 总下载量: 51.86M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 145
- 点击次数: 25
- 依赖项目数: 102
- 推荐数: 0
其他信息
- 授权协议: BSD-2-Clause
- 更新时间: 2017-11-01