phpcsstandards/phpcsdevtools
Composer 安装命令:
composer require --dev phpcsstandards/phpcsdevtools
包简介
Tools for PHP_CodeSniffer sniff developers.
README 文档
README
This is a set of tools to assist developers of sniffs for PHP CodeSniffer.
Installation
Composer Project-based Installation
Run the following from the root of your project:
composer config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
composer require --dev phpcsstandards/phpcsdevtools:^1.0
Composer Global Installation
If you work on several different sniff repos, you may want to install this toolset globally:
composer global config allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
composer global require --dev phpcsstandards/phpcsdevtools:^1.0
Composer will automatically install dependencies and register the PHPCSDebug standard with PHP_CodeSniffer using the Composer PHPCS plugin.
Stand-alone Installation
- Install PHP CodeSniffer via your preferred method.
- Register the path to PHPCS in your system
$PATHenvironment variable to make thephpcscommand available from anywhere in your file system. - Download the latest PHPCSDevTools release and unzip/untar it into an arbitrary directory. You can also choose to clone this repository using git.
- Add the path to the directory in which you placed your copy of the PHPCSDevTools repo to the PHP CodeSniffer configuration using the below command:
phpcs --config-set installed_paths /path/to/PHPCSDevTools
⚠️ Warning: Theinstalled_pathscommand overwrites any previously setinstalled_paths. If you have previously setinstalled_pathsfor other external standards, runphpcs --config-showfirst and then run theinstalled_pathscommand with all the paths you need separated by commas, i.e.:phpcs --config-set installed_paths /path/1,/path/2,/path/3
Features
Checking whether all sniffs in a PHPCS standard are feature complete
You can now check whether each and every sniff in your standard is accompanied by a documentation XML file (warning) as well as unit test files (error). The tool will also verify that there are no documentation or test files in your standard which are not associated with a sniff ("orphaned" files).
To use the tool, run it from the root of your standards repo like so:
# When installed as a project dependency: vendor/bin/phpcs-check-feature-completeness # When installed globally with Composer: phpcs-check-feature-completeness # When installed as a git clone or otherwise: php -f "path/to/PHPCSDevTools/bin/phpcs-check-feature-completeness"
If all is good, you will see the following messages:
All # sniffs are accompanied by unit tests and documentation.
No orphaned documentation or test files found.
If there are files missing, you will see errors/warnings for each missing file, like so:
WARNING: Documentation missing for path/to/project/StandardName/Sniffs/Category/SniffNameSniff.php.
ERROR: Unit tests missing for path/to/project/StandardName/Sniffs/Category/SniffNameSniff.php.
If orphaned files are found you will receive a warning for each orphaned file found, like so:
WARNING: Orphaned documentation file found path/to/project/StandardName/Docs/Category/NonExistantSniffStandard.xml.
WARNING: Orphaned test file found path/to/project/StandardName/Test/Category/NonExistantSniffUnitTest.inc.
For the fastest results, it is recommended to pass the name of the subdirectory where your standard is located to the script, like so:
phpcs-check-feature-completeness ./StandardName
Options
directories <dir> One or more specific directories to examine.
Defaults to the directory from which the script is run.
-q, --quiet Turn off warnings for missing documentation and orphaned
files.
Equivalent to running with "--no-docs --no-orphans".
--exclude=<dir1,dir2> Comma-delimited list of (relative) directories to
exclude from the scan.
Defaults to excluding the /vendor/ directory.
--no-docs Disable missing documentation check.
--no-orphans Disable orphaned files check.
--no-progress Disable progress in console output.
--colors Enable colors in console output.
(disables auto detection of color support).
--no-colors Disable colors in console output.
-v Verbose mode.
-h, --help Print this help.
-V, --version Display the current version of this script.
Sniff Debugging
Once this project is installed, you will see a new PHPCSDebug ruleset in the list of installed standards when you run phpcs -i.
For now, this standard only contains one sniff: PHPCSDebug.Debug.TokenList.
This sniff will display compact, but detailed information about the tokens found in a (test case) file.
This sniff is compatible with PHPCS 3.1.0+.
Typical usage:
- Set up a test case file for a new sniff you intend to write.
- Run PHPCS over the test case file using this standard to see a list of the tokens found in the file:
phpcs ./SniffNameUnitTest.inc --standard=PHPCSDebug
- Or use it together with the new sniff you are developing:
phpcs ./SniffNameUnitTest.inc --standard=YourStandard,PHPCSDebug --sniffs=YourStandard.Category.NewSniffName,PHPCSDebug.Debug.TokenList
The output will look something along the lines of:
Ptr | Ln | Col | Cond | ( #) | Token Type | [len]: Content
-------------------------------------------------------------------------
0 | L1 | C 1 | CC 0 | ( 0) | T_OPEN_TAG | [ 5]: <?php
1 | L2 | C 1 | CC 0 | ( 0) | T_WHITESPACE | [ 0]:
2 | L3 | C 1 | CC 0 | ( 0) | T_COMMENT | [ 32]: // Boolean not operator: All OK.
3 | L4 | C 1 | CC 0 | ( 0) | T_IF | [ 2]: if
4 | L4 | C 3 | CC 0 | ( 0) | T_WHITESPACE | [ 1]: ⸱
5 | L4 | C 4 | CC 0 | ( 0) | T_OPEN_PARENTHESIS | [ 1]: (
6 | L4 | C 5 | CC 0 | ( 1) | T_WHITESPACE | [ 1]: ⸱
7 | L4 | C 6 | CC 0 | ( 1) | T_CONSTANT_ENCAPSED_STRING | [ 4]: 'bb'
8 | L4 | C 10 | CC 0 | ( 1) | T_WHITESPACE | [ 1]: ⸱
9 | L4 | C 11 | CC 0 | ( 1) | T_IS_NOT_IDENTICAL | [ 3]: !==
10 | L4 | C 14 | CC 0 | ( 1) | T_WHITESPACE | [ 1]: ⸱
11 | L4 | C 15 | CC 0 | ( 1) | T_CONSTANT_ENCAPSED_STRING | [ 4]: 'bb'
12 | L4 | C 19 | CC 0 | ( 1) | T_WHITESPACE | [ 1]: ⸱
13 | L4 | C 20 | CC 0 | ( 0) | T_CLOSE_PARENTHESIS | [ 1]: )
14 | L4 | C 21 | CC 0 | ( 0) | T_WHITESPACE | [ 1]: ⸱
15 | L4 | C 22 | CC 0 | ( 0) | T_OPEN_CURLY_BRACKET | [ 1]: {
16 | L4 | C 23 | CC 0 | ( 0) | T_WHITESPACE | [ 0]:
17 | L5 | C 1 | CC 0 | ( 0) | T_WHITESPACE | [ 1]: →
18 | L5 | C 2 | CC 0 | ( 0) | T_IF | [ 2]: if
19 | L5 | C 4 | CC 0 | ( 0) | T_WHITESPACE | [ 1]: ⸱
20 | L5 | C 5 | CC 0 | ( 0) | T_OPEN_PARENTHESIS | [ 1]: (
21 | L5 | C 6 | CC 0 | ( 0) | T_WHITESPACE | [ 0]:
PHPCS itself can also display similar information using the -vv or -vvv verbosity flags, however, when using those, you will receive a lot more information than just the token list and, while useful for debugging PHPCS itself, the additional information is mostly just noise when developing a sniff.
Documentation XSD Validation
This project contains an XML Schema Definition (XSD) to allow for validation PHPCS documentation XML files. Following the XSD will make sure your documentation can be correctly displayed when using the PHPCS --generator option.
In order to use it, you'll need to add the schema related attributes to the documentation element of the sniff documentation file, like so:
<documentation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd" title="Name of the sniff" >
If your IDE or editor supports automatic validation of XML files, you will be notified if your documentation XML file has the correct number of elements, correct type and number of certain attributes, and title length among other things.
Validating your docs against the XSD
You can validate your PHPCS XML documentation against the XSD file using xmllint. This validation can be run locally if you have xmllint installed, as well as in CI (continuous integration).
An example of a workflow job for GitHub Actions CI looks like this:
jobs: validate-xml: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Validate docs against schema uses: phpcsstandards/xmllint-validate@v1 with: pattern: "YourRuleset/Docs/**/*Standard.xml" xsd-url: "https://phpcsstandards.github.io/PHPCSDevTools/phpcsdocs.xsd"
Or if you use this in a job which runs a composer install, you can reference the file in the vendor directory.
jobs: validate-xml: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 ... # (install PHP, run composer) - name: Validate docs against schema uses: phpcsstandards/xmllint-validate@v1 with: pattern: "YourRuleset/Docs/**/*Standard.xml" xsd-file: "vendor/phpcsstandards/phpcsdevtools/DocsXsd/phpcsdocs.xsd"
👉 You'll need to replace the YourRuleset within the command with the name of your ruleset (of course).
Contributing
Contributions to this project are welcome. Clone this repository, branch off from develop, make your changes, commit them and send in a pull request.
If unsure whether the changes you are proposing would be welcome, open an issue first to discuss your proposal.
License
This code is released under the GNU Lesser General Public License (LGPLv3).
phpcsstandards/phpcsdevtools 适用场景与选型建议
phpcsstandards/phpcsdevtools 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 998.12k 次下载、GitHub Stars 达 17, 最近一次更新时间为 2019 年 08 月 14 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「debug」 「static analysis」 「phpcs」 「dev」 「PHP_CodeSniffer」 「Devtools」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 phpcsstandards/phpcsdevtools 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 phpcsstandards/phpcsdevtools 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 phpcsstandards/phpcsdevtools 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Debug your SimpleBus EventBus and CommandBus
nice output for debug functions for PHP 5.3
Bookdown.io With Bootswatch Styles And Prism Syntax Highlighting
TwigStan is a static analyzer for Twig templates powered by PHPStan
Sentiment analysis library for PHP.
Analysis module for finding problematical shop data.
统计信息
- 总下载量: 998.12k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 17
- 点击次数: 36
- 依赖项目数: 25
- 推荐数: 0
其他信息
- 授权协议: LGPL-3.0-or-later
- 更新时间: 2019-08-14