totara/codesniffer
Composer 安装命令:
composer require totara/codesniffer
包简介
PHP Code Sniffer standard for Totara Coding Style.
关键字:
README 文档
README
This project contains a Code Sniffer Standard for Totara Learn.
Prerequisites
- PHP 7.4 or higher (including PHP 8.4 and 8.5)
- Composer (https://getcomposer.org/)
Please note that the minimum PHP requirement has changed. You can now only install and run Codesniffer on PHP 7.4 or higher.
You can still analyse versions of Totara compatible only to version 5.6, as long as you run Codesniffer on 7.4 or higher.
Installation
Via Composer
In your project, require this package by running:
composer require --dev totara/codesniffer
You may be prompted to allow the plugin dealerdirect/phpcodesniffer-composer-installer - if so answer yes.
This will then install the package in the correct location.
Confirm the Totara standards are available by running ./vendor/bin/phpcs -i.
The installed coding standards are MySource, PEAR, PSR1, PSR2, PSR12, Squiz, Zend, PHPCompatibility, PHPCSUtils and Totara
Manual Installation
Clone this repository into a folder of your choice.
In the root of the project run:
# Install composer packages (--dev is only required for running phpunit)
# If needed, change "composer" to the composer executable on your machine.
composer install --no-dev
# Update the config path
vendor/bin/phpcs --config-set installed_paths src/Standards"
# Now check what standards PHPCS can use
# The new Totara coding standard is automatically set after composer install
vendor/bin/phpcs -i
# should output something like
The installed coding standards are PEAR, Zend, PSR2, MySource, Squiz, PSR1, PSR12, Totara and PHPCompatibility
Optionally you can add phpcs to your path:
# Add this to your ~/.bash_profile or ~/.profile
# (Replace /path/to/this/project/ with the actual one on your machine)
export PATH="$PATH:/path/to/this/project/vendor/bin"
# don't forget to either do
source ~/.bash_profile
# or
source ~/.profile
# or start a new shell
Usage
Console
To run Code Sniffer using the Totara Standard on the console
vendor/bin/phpcs --standard=Totara [--extensions=php,js] [folder_or_file_to_check]
# Example:
vendor/bin/phpcs --standard=Totara --extensions=php /path/to/your/project/classes/[MyClassToCheck.php]
# or if you've added phpcs to your path
phpcs --standard=Totara --extensions=php classes/[MyClassToCheck.php]
As some Sniffs can also check JavaScript files you might want to limit the extensions to one of the two.
Default phpcs.xml file
If your project includes a phpcs.xml or phpcs.xml.dist file you can run Code Sniffer from within your project without specifying the standard or any other configuration set in that file.
/path/to/phpcs filesorfoldertocheck
See PHP Code Sniffer's Advanced Usage docs for more details.
PHPStorm
To set up PHPStorm to automatically check your files and highlight coding style violations directly in the code
- Open "Preferences"
- Go to "Languages & Framework" => "PHP" => "Quality Tools" => "Code Sniffer"
- Make sure that in the Configuration the "PHP Code Snifffer path" points to the "vendor/bin/phpcs" file within this project
- "Validate" the selection and click "Apply"
- Go to "Editor" => "Inspections"
- In the big tree of Inspections find "PHP" => "Quality Tools" => "PHP Code Sniffer Validation"
- Activate the inspection
- In the right panel under "Options" choose "Custom" as Standard and click on the "..." button next to it
- Choose the ruleset.xml file from this project (
src/Standards/Totara/ruleset.xml) - Click "OK"
That's it. Now all violations should be marked directly within your code view.
PHPCompatibility
The Totara standard does include the PHPCompatibility standard as well to check for problems on specific versions.
By default the PHPCompatibility rules will check only the PHP version you are running the phpcs command on. To choose a specific version to check either set the version on the console
# to run all the checks for PHP 8.4 and PHP 8.5
/path/to/phpcs --runtime-set testVersion 8.4-8.5 ...
or have a "testVersion" configuration option in your phpcs.xml file.
See PHPCompatibility for more details.
Development
Sniffs
In the xml file src/Standards/Totara/ruleset.xml you find all rules used for this Coding Standard. It is a mixture of Sniffs from other Standards and our own Sniffs.
If you create or modify Sniffs within this project make sure you stick to the PSR-2 Coding Standard (to match the style of the original Code Sniffer library) and PSR-4 autoloading.
Testing
If you create or modify Sniffs within this project make sure you create or update unit tests for it.
To run the unit tests:
# If you haven't done it before, run
composer install
# Then run all tests with:
vendor/bin/phpunit
Git Hook
There is also a pre-commit hook for git to run Code Sniffer on commit.
Check out the instructions on how to use it.
totara/codesniffer 适用场景与选型建议
totara/codesniffer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 417 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 09 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「standards」 「phpcs」 「totara」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 totara/codesniffer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 totara/codesniffer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 totara/codesniffer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP_CodeSniffer custom Sniff for Laravel coding standard
A set of abstractions that define core services provided by the DVSA
This is the ZooRoyal coding standard.
Vanio Coding Standard Ruleset for PHP_CodeSniffer.
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
统计信息
- 总下载量: 417
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: BSD-3-Clause
- 更新时间: 2023-09-19