suilven/php-travis-enhancer

为PHP分析添加代码质量工具,并搜索代码重复

1.0.3 2020-07-09 21:02 UTC

This package is auto-updated.

Last update: 2024-09-26 22:37:44 UTC


README

Build Status Scrutinizer Code Quality codecov.io

Latest Stable Version Latest Unstable Version Total Downloads License Monthly Downloads Daily Downloads composer.lock

GitHub Code Size GitHub Repo Size GitHub Last Commit GitHub Activity GitHub Issues

codecov.io

为您的PHP项目添加更严格的PHPStan和PHPCS编码标准检查、代码风格检查、Psalm标准检查和代码重复检测。

用法

简介

此包的目标是为您的PHP包添加严格的编码检查。我手动将这些任务添加到几个模块中,并决定是时候尽可能地自动化这一过程了。因此,存在这个包。

以下任一或所有编码检查都可以添加

  • lint - 简单地解析您的PHP代码以查找语法错误,如果存在一些错误,构建将中断
  • phpcs - 将PHP代码与Slevomat编码标准的一部分进行校验。一些检查相互矛盾,我在ruleset.xml中注释了那些会破坏代码修复的检查。然后可以使用composer fixcs自动修复,但某些类型的修复只能手动完成。
  • phpstan - 在您的代码库上运行PHPStan,这是一个静态PHP分析工具。我将其设置为相当严格的6级,您可能希望手动更改此设置
  • psalm - 运行Psalm,一个替代的静态分析检查器。请注意,这个检查对PHPDoc相当严格,这是一个好事。
  • 重复 - 这是为Travis提供的额外检查,如果在代码中找到大量的重复代码,构建将失败。

警告!!

请注意,此模块会更改您的代码库,因此请在已备份的源代码树上运行此命令,最好是master分支之外的单独分支,这样以后可以提交拉取请求。

以下文件将被更改

  • travis.yml - 在矩阵中添加了额外的代码质量检查,安装和运行步骤
  • composer.json - 代码检查所需的包,运行代码检查的脚本
  • ruleset.xml - 基于PSR2,但添加了Slevomat编码标准
  • tests/phpstan.neon - phpstan的基本配置文件,用于自动加载文件

添加代码检查工具

安装

composer require --dev suilven/php-travis-enhancer

添加所有检查

这需要几分钟才能运行。如果已经存在相同的标志,或者相同的composer脚本名称,那么在.travis.ymlcomposer.json中分别保留原样。

vendor/bin/phpte all

添加单个检查

如果需要,可以单独添加检查。

vendor/bin/phpte phpstan
vendor/bin/phpte phpcs
vendor/bin/phpte lint
vendor/bin/phpte duplication
vendor/bin/phpte psalm

请注意,被修改的composer文件中的checkCode脚本将仅包含安装的单个命令。

移除

此模块是一次性的,因此使用后请将其移除

composer remove suilven/php-travis-enhancer

本地检查代码

如果使用all安装了检查,则composer checkCode将运行代码检查。这是对以下命令的别名:

composer checkcs && composer lint && composer psalm && composer phpstan

如果其中任何一个失败,它将立即退出。

运行看起来像什么?

这是一个片段,但希望这足以提供上下文。这里我正在测试一个旧的小型SilverStripe模块。

> composer checkcs
> vendor/bin/phpcs --standard=ruleset.xml --extensions=php --tab-width=4 -sp src tests
EE 2 / 2 (100%)



FILE: /var/www/src/PrevNextSiblingExtension.php
----------------------------------------------------------------------
FOUND 9 ERRORS AFFECTING 6 LINES
----------------------------------------------------------------------
  1 | ERROR | [x] Missing declare(strict_types = 1).
    |       |     (SlevomatCodingStandard.TypeHints.DeclareStrictTypes.DeclareStrictTypesMissing)
 10 | ERROR | [ ] Method name "PrevNextSiblingExtension::NextSibling"
    |       |     is not in camel caps format
    |       |     (PSR1.Methods.CamelCapsMethodName.NotCamelCaps)
 10 | ERROR | [x] Expected 2 blank lines after method, found 1.
    |       |     (SlevomatCodingStandard.Classes.MethodSpacing.IncorrectLinesCountBetweenMethods)
 10 | ERROR | [ ] Method
    |       |     \WebOfTalent\PrevNextSibling\PrevNextSiblingExtension::NextSibling()
    |       |     does not have return type hint nor @return
    |       |     annotation for its return value.
    |       |     (SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint)
 13 | ERROR | [x] Useless variable $result.
    |       |     (SlevomatCodingStandard.Variables.UselessVariable.UselessVariable)
 14 | ERROR | [x] Expected 1 lines before "return", found 0.
    |       |     (SlevomatCodingStandard.ControlStructures.JumpStatementsSpacing.IncorrectLinesCountBeforeControlStructure)
 17 | ERROR | [ ] Method name
    |       |     "PrevNextSiblingExtension::PreviousSibling" is not
    |       |     in camel caps format
    |       |     (PSR1.Methods.CamelCapsMethodName.NotCamelCaps)
 17 | ERROR | [ ] Method
    |       |     \WebOfTalent\PrevNextSibling\PrevNextSiblingExtension::PreviousSibling()
    |       |     does not have return type hint nor @return
    |       |     annotation for its return value.
    |       |     (SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint)
 20 | ERROR | [x] Expected 1 lines before "return", found 0.
    |       |     (SlevomatCodingStandard.ControlStructures.JumpStatementsSpacing.IncorrectLinesCountBeforeControlStructure)
----------------------------------------------------------------------
PHPCBF CAN FIX THE 5 MARKED SNIFF VIOLATIONS AUTOMATICALLY
----------------------------------------------------------------------

自动修复错误

上面用X标记的错误可以自动修复。

> composer fixcs
> vendor/bin/phpcbf --standard=ruleset.xml --extensions=php --tab-width=4 -sp src tests
FF 2 / 2 (100%)



PHPCBF RESULT SUMMARY
----------------------------------------------------------------------
FILE                                                  FIXED  REMAINING
----------------------------------------------------------------------
/var/www/src/PrevNextSiblingExtension.php             5      4
/var/www/tests/PrevNextSiblingExtensionTest.php       15     0
----------------------------------------------------------------------
A TOTAL OF 20 ERRORS WERE FIXED IN 2 FILES
----------------------------------------------------------------------


自动修复后运行代码检查

以下错误需要手动修复

> composer checkcs
> vendor/bin/phpcs --standard=ruleset.xml --extensions=php --tab-width=4 -sp src tests
E. 2 / 2 (100%)



FILE: /var/www/src/PrevNextSiblingExtension.php
----------------------------------------------------------------------
FOUND 4 ERRORS AFFECTING 2 LINES
----------------------------------------------------------------------
 10 | ERROR | Method name "PrevNextSiblingExtension::NextSibling" is
    |       | not in camel caps format
    |       | (PSR1.Methods.CamelCapsMethodName.NotCamelCaps)
 10 | ERROR | Method
    |       | \WebOfTalent\PrevNextSibling\PrevNextSiblingExtension::NextSibling()
    |       | does not have return type hint nor @return annotation
    |       | for its return value.
    |       | (SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint)
 18 | ERROR | Method name "PrevNextSiblingExtension::PreviousSibling"
    |       | is not in camel caps format
    |       | (PSR1.Methods.CamelCapsMethodName.NotCamelCaps)
 18 | ERROR | Method
    |       | \WebOfTalent\PrevNextSibling\PrevNextSiblingExtension::PreviousSibling()
    |       | does not have return type hint nor @return annotation
    |       | for its return value.
    |       | (SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingAnyTypeHint)
----------------------------------------------------------------------

后记

我尝试将其应用于一个相当大的代码库(Manticore PHP Search Client),但由于错误数量庞大而放弃了。因此,我将它用于新项目(包括这个项目),并回过头来修复旧的小项目。

许可证

License