paulhenri-l / custom-php-cs-fixer
一个自动修复PHP代码风格的工具
Requires
- php: ^5.6 || ^7.0
- ext-json: *
- ext-tokenizer: *
- composer/semver: ^1.4
- composer/xdebug-handler: ^1.2
- doctrine/annotations: ^1.2
- php-cs-fixer/diff: ^1.3
- symfony/console: ^3.4.17 || ^4.1.6 || ^5.0
- symfony/event-dispatcher: ^3.0 || ^4.0 || ^5.0
- symfony/filesystem: ^3.0 || ^4.0 || ^5.0
- symfony/finder: ^3.0 || ^4.0 || ^5.0
- symfony/options-resolver: ^3.0 || ^4.0 || ^5.0
- symfony/polyfill-php70: ^1.0
- symfony/polyfill-php72: ^1.4
- symfony/process: ^3.0 || ^4.0 || ^5.0
- symfony/stopwatch: ^3.0 || ^4.0 || ^5.0
Requires (Dev)
- johnkary/phpunit-speedtrap: ^1.1 || ^2.0 || ^3.0
- justinrainbow/json-schema: ^5.0
- keradus/cli-executor: ^1.2
- mikey179/vfsstream: ^1.6
- php-coveralls/php-coveralls: ^2.1
- php-cs-fixer/accessible-object: ^1.0
- php-cs-fixer/phpunit-constraint-isidenticalstring: ^1.1
- php-cs-fixer/phpunit-constraint-xmlmatchesxsd: ^1.1
- phpunit/phpunit: ^5.7.27 || ^6.5.14 || ^7.1
- phpunitgoodpractices/traits: ^1.8
- symfony/phpunit-bridge: ^4.3 || ^5.0
- symfony/yaml: ^3.0 || ^4.0 || ^5.0
Suggests
- ext-dom: For handling output formats in XML
- ext-mbstring: For handling non-UTF8 characters in cache signature.
- php-cs-fixer/phpunit-constraint-isidenticalstring: For IsIdenticalString constraint.
- php-cs-fixer/phpunit-constraint-xmlmatchesxsd: For XmlMatchesXsd constraint.
- symfony/polyfill-mbstring: When enabling `ext-mbstring` is not possible.
Replaces
This package is auto-updated.
Last update: 2020-10-27 21:08:20 UTC
README
PHP编码标准修复器(PHP CS Fixer)工具可以将您的代码修复为符合标准;无论您是想遵循PSR-1、PSR-2等PHP编码标准,还是其他社区驱动的标准,如Symfony。您还可以通过配置定义您(团队)的风格。
它可以使您的代码现代化(如将PHP 5.6中的pow函数转换为**运算符)并(微)优化它。
如果您已经使用linter来识别代码中的编码标准问题,您知道手动修复它们是非常繁琐的,尤其是在大型项目中。此工具不仅会检测它们,还会为您修复它们。
PHP CS Fixer在GitHub上维护,网址为https://github.com/FriendsOfPHP/PHP-CS-Fixer。欢迎在此处提交错误报告和新功能想法。
您可以在https://gitter.im/PHP-CS-Fixer/Lobby上与我们就项目、配置、可能的改进、想法和问题进行交流,请访问我们!
需求
PHP版本至少为PHP 5.6.0。
安装
本地安装
下载php-cs-fixer.phar文件并将其存储在您的计算机上的某个位置。
全局(手动)
您可以使用以下命令轻松从系统中的任何位置访问最新的php-cs-fixer
$ wget https://cs.symfony.com/download/php-cs-fixer-v2.phar -O php-cs-fixer
或指定版本
$ wget https://github.com/FriendsOfPHP/PHP-CS-Fixer/releases/download/v2.16.3/php-cs-fixer.phar -O php-cs-fixer
或使用curl
$ curl -L https://cs.symfony.com/download/php-cs-fixer-v2.phar -o php-cs-fixer
然后
$ sudo chmod a+x php-cs-fixer $ sudo mv php-cs-fixer /usr/local/bin/php-cs-fixer
然后,只需运行php-cs-fixer
。
全局(Composer)
要安装PHP CS Fixer,请安装Composer并运行以下命令
$ composer global require friendsofphp/php-cs-fixer
然后确保您在PATH
中具有全局Composer二进制目录。该目录是平台相关的,有关详细信息,请参阅Composer文档。某些Unix系统的示例
$ export PATH="$PATH:$HOME/.composer/vendor/bin"
全局(Homebrew)
$ brew install php-cs-fixer
本地(PHIVE)
安装PHIVE并运行以下命令
$ phive install php-cs-fixer # use `--global` for global install
更新
本地
self-update命令尝试更新php-cs-fixer
$ php php-cs-fixer.phar self-update
全局(手动)
您可以通过以下命令更新 php-cs-fixer
$ sudo php-cs-fixer self-update
全局(Composer)
您可以通过以下命令更新 php-cs-fixer
$ ./composer.phar global update friendsofphp/php-cs-fixer
全局(homebrew)
您可以通过以下命令更新 php-cs-fixer
$ brew upgrade php-cs-fixer
本地(PHIVE)
$ phive update php-cs-fixer
用法
fix
命令尝试在一个指定的文件或目录以及其子目录中尽可能多地修复编码标准问题。
$ php php-cs-fixer.phar fix /path/to/dir $ php php-cs-fixer.phar fix /path/to/file
默认情况下,--path-mode
被设置为 override
,这意味着,如果您通过命令参数指定文件或目录的路径,则将忽略配置文件中提供的 Finder
的路径。您可以使用 --path-mode=intersection
将配置文件和参数中的路径合并。
$ php php-cs-fixer.phar fix --path-mode=intersection /path/to/dir
--format
选项用于指定输出格式。支持的格式有 txt
(默认格式)、json
、xml
、checkstyle
、junit
和 gitlab
。
注意:以下格式的输出将根据 XML 架构生成
junit
遵循 Jenkins 的 JUnit XML 架构checkstyle
遵循通用的 "checkstyle" XML 架构
--quiet
选项表示不输出任何消息。
--verbose
选项会显示应用的规则。当使用 txt
格式时,还会显示进度通知。
注意:如果在修复后出现“报告错误”的错误,您可以使用此选项进行更详细的调试
--verbose=0
或无选项:常规--verbose
,--verbose=1
,-v
:详细--verbose=2
,-vv
:非常详细--verbose=3
,-vvv
:调试
--rules
选项限制了应用到项目中的规则
$ php php-cs-fixer.phar fix /path/to/project --rules=@PSR2
默认情况下使用 PSR1 和 PSR2 规则。
--rules
选项允许您选择要应用的精确规则(规则名称必须用逗号分隔)
$ php php-cs-fixer.phar fix /path/to/dir --rules=line_ending,full_opening_tag,indentation_type
您还可以通过在规则名称前放置破折号来黑名单您不希望应用的规则,如果这样更方便,可以使用 -name_of_fixer
$ php php-cs-fixer.phar fix /path/to/dir --rules=-full_opening_tag,-indentation_type
当使用精确规则和黑名单规则的组合时,将应用精确规则,并排除黑名单中的规则
$ php php-cs-fixer.phar fix /path/to/project --rules=@Symfony,-@PSR1,-blank_line_before_statement,strict_comparison
可以使用 JSON 格式的字符串提供规则的完整配置
$ php php-cs-fixer.phar fix /path/to/project --rules='{"concat_space": {"spacing": "none"}}'
--dry-run
标志将在不更改您的文件的情况下运行修复器。
--diff
标志可以让修复器输出它所做的所有更改。
--diff-format
选项允许您指定修复器输出更改的格式
udiff
:统一差异格式;sbd
:Sebastianbergmann/diff 格式(在未指定 diff-format 时使用 --diff 的默认格式)。
--allow-risky
选项(传递 yes
或 no
)允许您设置是否允许运行有风险的规则。默认值取自配置文件。如果一个规则可能会更改代码行为,则认为它是风险的。默认情况下不运行任何有风险的规则。
--stop-on-violation
标志会在遇到第一个需要修复的文件时停止执行。
--show-progress
选项允许您选择进程进度的渲染方式
none
:禁用进度输出;run-in
:[已弃用] 简单的单行进度输出;estimating
:[已弃用] 多行进度输出,每行显示文件数和百分比。注意,使用此选项时,在处理之前评估文件列表以获取文件总数,然后将其保留在内存中,以避免两次使用文件迭代器。这会影响内存使用,因此不建议在非常大的项目中使用此选项。estimating-max
: [已弃用] 与dots
相同;dots
: 与estimating
相同,但使用所有终端列而不是默认的 80 列。
如果没有提供选项,则默认为 run-in
,除非使用了禁用输出的配置文件,在这种情况下默认为 none
。如果命令的详细程度小于 verbose
,则此选项不起作用。
$ php php-cs-fixer.phar fix --verbose --show-progress=estimating
命令还可以从标准输入读取,在这种情况下,它不会自动修复任何内容。
$ cat foo.php | php php-cs-fixer.phar fix --diff -
最后,如果您不需要在 CLI 级别保留 BC,则可以使用 PHP_CS_FIXER_FUTURE_MODE 开始使用在下一次 MAJOR 版本中将是默认选项的选项(统一差异、估计、全宽进度指示器)。
$ PHP_CS_FIXER_FUTURE_MODE=1 php php-cs-fixer.phar fix -v --diff
从可用的规则列表中选择。
align_multiline_comment [@PhpCsFixer]
多行文档注释的每一行都必须有一个星号 [PSR-5],并且必须与第一行对齐。
配置选项
comment_type
('all_multiline'
,'phpdocs_like'
,'phpdocs_only'
):是否仅修复 PHPDoc 注释 (phpdocs_only
),任何所有行都以星号开头的多行注释 (phpdocs_like
) 或任何多行注释 (all_multiline
);默认为'phpdocs_only'
array_indentation [@PhpCsFixer]
数组中的每个元素都必须缩进一次。
array_syntax [@Symfony, @PhpCsFixer]
PHP 数组应使用配置的语法声明。
配置选项
syntax
('long'
,'short'
):是否使用long
或short
数组语法;默认为'long'
backtick_to_shell_exec
将反引号运算符转换为
shell_exec
调用。binary_operator_spaces [@Symfony, @PhpCsFixer]
二元运算符应按配置的方式包围空间。
配置选项
align_double_arrow
(false
,null
,true
):是否应用、移除或忽略双箭头对齐;默认为false
。已弃用:请使用选项operators
和default
代替align_equals
(false
,null
,true
):是否应用、移除或忽略等于对齐;默认为false
。已弃用:请使用选项operators
和default
代替default
('align'
,'align_single_space'
,'align_single_space_minimal'
,'no_space'
,'single_space'
,null
):默认修复策略;默认为'single_space'
operators
(array
):字典二元运算符
=>修复策略
值,该值与默认策略不同;默认为[]
blank_line_after_namespace [@PSR2, @Symfony, @PhpCsFixer]
在命名空间声明之后必须有一个空行。
blank_line_after_opening_tag [@Symfony, @PhpCsFixer]
确保没有代码与 PHP 开头标签在同一行,并且它后面跟着一个空行。
blank_line_before_return
return 语句之前应该有一个空行。已弃用:请使用
blank_line_before_statement
代替。blank_line_before_statement [@Symfony, @PhpCsFixer]
任何配置的语句之前必须有一个空行。
配置选项
statements
(一个['break', 'case', 'continue', 'declare', 'default', 'die', 'do', 'exit', 'for', 'foreach', 'goto', 'if', 'include', 'include_once', 'require', 'require_once', 'return', 'switch', 'throw', 'try', 'while', 'yield']
的子集):必须先于空行执行的语句列表;默认为['break', 'continue', 'declare', 'return', 'throw', 'try']
braces [@PSR2, @Symfony, @PhpCsFixer]
每个结构的主体必须用花括号括起来。花括号应放置得当。花括号的主体应正确缩进。
配置选项
allow_single_line_closure
(bool
):是否允许单行 lambda 表示法;默认为false
position_after_anonymous_constructs
('next'
,'same'
):匿名结构(匿名类和 lambda 函数)之后的打开花括号是否应放在 "next" 或 "same" 行上;默认为'same'
position_after_control_structures
('next'
,'same'
):控制结构之后的大括号应该放在“下一行”还是“同一行”;默认为'same'
position_after_functions_and_oop_constructs
('next'
,'same'
):在类构造(非匿名类、接口、特质、方法和非lambda函数)之后,大括号应该放在“下一行”还是“同一行”;默认为'next'
cast_spaces [@Symfony, @PhpCsFixer]
类型转换和变量之间应该有一个空格或没有。
配置选项
space
('none'
,'single'
):类型转换和变量之间的间距;默认为'single'
class_attributes_separation [@Symfony, @PhpCsFixer]
类、特性和接口元素必须用一行空行隔开。
配置选项
elements
(['const', 'method', 'property']
的子集):类元素列表;'const', 'method', 'property';默认为['const', 'method', 'property']
class_definition [@PSR2, @Symfony, @PhpCsFixer]
类、特性和接口定义的关键字周围应该有一个空格。
配置选项
multi_line_extends_each_single_line
(bool
):是否应该多行定义;默认为false
;已弃用别名:multiLineExtendsEachSingleLine
single_item_single_line
(bool
):当包含单个项目时,是否应该单行定义;默认为false
;已弃用别名:singleItemSingleLine
single_line
(bool
):是否应该单行定义;默认为false
;已弃用别名:singleLine
class_keyword_remove
将
::class
关键字转换为FQCN字符串。combine_consecutive_issets [@PhpCsFixer]
多次使用
isset($var) &&
应该在一个调用中完成。combine_consecutive_unsets [@PhpCsFixer]
对一个多个项目调用
unset
应该在一个调用中完成。combine_nested_dirname [@PHP70Migration:risky, @PHP71Migration:risky]
将多个嵌套的
dirname
调用替换为一个只带有第二个$level
参数的调用。需要PHP >= 7.0。风险规则:当函数``dirname``被覆盖时为风险。
comment_to_phpdoc [@PhpCsFixer:risky]
当用于结构元素时,应将注释与注解转换为文档块。
风险规则:新的文档块可能意味着更多,例如,Doctrine实体可能在数据库中有一个新的列。
配置选项
ignored_tags
(array
):忽略的标签列表;默认为[]
compact_nullable_typehint [@PhpCsFixer]
在可空类型提示中移除多余的空格。
concat_space [@Symfony, @PhpCsFixer]
连接应该按照配置进行空格处理。
配置选项
spacing
('none'
,'one'
):连接操作符周围的间距;默认为'none'
constant_case [@PSR2, @Symfony, @PhpCsFixer]
PHP常量
true
、false
和null
必须使用正确的大小写。配置选项
case
('lower'
,'upper'
):是否使用upper
或lower
大小写语法;默认为'lower'
date_time_immutable
应该使用类
DateTimeImmutable
而不是DateTime
。风险规则:当代码依赖于修改``DateTime``对象或任何``date_create*``函数被覆盖时为风险。
declare_equal_normalize [@Symfony, @PhpCsFixer]
声明语句中的等号应该周围有空格或不按配置。
配置选项
space
('none'
,'single'
):等号周围的间距;默认为'none'
declare_strict_types [@PHP70Migration:risky, @PHP71Migration:risky]
在所有文件中强制执行严格类型声明。需要PHP >= 7.0。
风险规则:强制执行严格类型将停止非严格代码的工作。
dir_constant [@Symfony:risky, @PhpCsFixer:risky]
将
dirname(__FILE__)
表达式替换为等效的__DIR__
常量。风险规则:当函数``dirname``被覆盖时为风险。
doctrine_annotation_array_assignment [@DoctrineAnnotation]
Doctrine 注释必须使用配置的运算符在数组中进行赋值。
配置选项
ignored_tags
(array
): 必须被视为 Doctrine 注释的标签列表;默认为['abstract', 'access', 'code', 'deprec', 'encode', 'exception', 'final', 'ingroup', 'inheritdoc', 'inheritDoc', 'magic', 'name', 'toc', 'tutorial', 'private', 'static', 'staticvar', 'staticVar', 'throw', 'api', 'author', 'category', 'copyright', 'deprecated', 'example', 'filesource', 'global', 'ignore', 'internal', 'license', 'link', 'method', 'package', 'param', 'property', 'property-read', 'property-write', 'return', 'see', 'since', 'source', 'subpackage', 'throws', 'todo', 'TODO', 'usedBy', 'uses', 'var', 'version', 'after', 'afterClass', 'backupGlobals', 'backupStaticAttributes', 'before', 'beforeClass', 'codeCoverageIgnore', 'codeCoverageIgnoreStart', 'codeCoverageIgnoreEnd', 'covers', 'coversDefaultClass', 'coversNothing', 'dataProvider', 'depends', 'expectedException', 'expectedExceptionCode', 'expectedExceptionMessage', 'expectedExceptionMessageRegExp', 'group', 'large', 'medium', 'preserveGlobalState', 'requires', 'runTestsInSeparateProcesses', 'runInSeparateProcess', 'small', 'test', 'testdox', 'ticket', 'uses', 'SuppressWarnings', 'noinspection', 'package_version', 'enduml', 'startuml', 'fix', 'FIXME', 'fixme', 'override']
operator
(':'
,'='
): 要使用的运算符;默认为'='
doctrine_annotation_braces [@DoctrineAnnotation]
没有参数的 Doctrine 注释必须使用配置的语法。
配置选项
ignored_tags
(array
): 必须被视为 Doctrine 注释的标签列表;默认为['abstract', 'access', 'code', 'deprec', 'encode', 'exception', 'final', 'ingroup', 'inheritdoc', 'inheritDoc', 'magic', 'name', 'toc', 'tutorial', 'private', 'static', 'staticvar', 'staticVar', 'throw', 'api', 'author', 'category', 'copyright', 'deprecated', 'example', 'filesource', 'global', 'ignore', 'internal', 'license', 'link', 'method', 'package', 'param', 'property', 'property-read', 'property-write', 'return', 'see', 'since', 'source', 'subpackage', 'throws', 'todo', 'TODO', 'usedBy', 'uses', 'var', 'version', 'after', 'afterClass', 'backupGlobals', 'backupStaticAttributes', 'before', 'beforeClass', 'codeCoverageIgnore', 'codeCoverageIgnoreStart', 'codeCoverageIgnoreEnd', 'covers', 'coversDefaultClass', 'coversNothing', 'dataProvider', 'depends', 'expectedException', 'expectedExceptionCode', 'expectedExceptionMessage', 'expectedExceptionMessageRegExp', 'group', 'large', 'medium', 'preserveGlobalState', 'requires', 'runTestsInSeparateProcesses', 'runInSeparateProcess', 'small', 'test', 'testdox', 'ticket', 'uses', 'SuppressWarnings', 'noinspection', 'package_version', 'enduml', 'startuml', 'fix', 'FIXME', 'fixme', 'override']
syntax
('with_braces'
,'without_braces'
): 是否添加或删除括号;默认为'without_braces'
doctrine_annotation_indentation [@DoctrineAnnotation]
Doctrine 注释必须缩进四个空格。
配置选项
ignored_tags
(array
): 必须被视为 Doctrine 注释的标签列表;默认为['abstract', 'access', 'code', 'deprec', 'encode', 'exception', 'final', 'ingroup', 'inheritdoc', 'inheritDoc', 'magic', 'name', 'toc', 'tutorial', 'private', 'static', 'staticvar', 'staticVar', 'throw', 'api', 'author', 'category', 'copyright', 'deprecated', 'example', 'filesource', 'global', 'ignore', 'internal', 'license', 'link', 'method', 'package', 'param', 'property', 'property-read', 'property-write', 'return', 'see', 'since', 'source', 'subpackage', 'throws', 'todo', 'TODO', 'usedBy', 'uses', 'var', 'version', 'after', 'afterClass', 'backupGlobals', 'backupStaticAttributes', 'before', 'beforeClass', 'codeCoverageIgnore', 'codeCoverageIgnoreStart', 'codeCoverageIgnoreEnd', 'covers', 'coversDefaultClass', 'coversNothing', 'dataProvider', 'depends', 'expectedException', 'expectedExceptionCode', 'expectedExceptionMessage', 'expectedExceptionMessageRegExp', 'group', 'large', 'medium', 'preserveGlobalState', 'requires', 'runTestsInSeparateProcesses', 'runInSeparateProcess', 'small', 'test', 'testdox', 'ticket', 'uses', 'SuppressWarnings', 'noinspection', 'package_version', 'enduml', 'startuml', 'fix', 'FIXME', 'fixme', 'override']
indent_mixed_lines
(bool
): 是否缩进具有内容的行,直到闭括号;默认为false
doctrine_annotation_spaces [@DoctrineAnnotation]
修复 Doctrine 注释中的空格。
配置选项
after_argument_assignments
(null
,bool
): 是否在参数赋值运算符后添加、删除或忽略空格;默认为false
after_array_assignments_colon
(null
,bool
): 是否在数组赋值:
运算符后添加、删除或忽略空格;默认为true
after_array_assignments_equals
(null
,bool
): 是否在数组赋值=
运算符后添加、删除或忽略空格;默认为true
around_argument_assignments
(bool
): 是否修复参数赋值运算符周围的空格;默认为true
. 已弃用:使用选项before_argument_assignments
和after_argument_assignments
代替around_array_assignments
(bool
): 是否修复数组赋值运算符周围的空格;默认为true
. 已弃用:使用选项before_array_assignments_equals
、after_array_assignments_equals
、before_array_assignments_colon
和after_array_assignments_colon
代替around_commas
(bool
): 是否修复逗号周围的空格;默认为true
around_parentheses
(bool
): 是否修复括号周围的空格;默认为true
before_argument_assignments
(null
,bool
): 是否在参数赋值运算符前添加、删除或忽略空格;默认为false
before_array_assignments_colon
(null
,bool
): 是否在数组:
赋值运算符前添加、删除或忽略空格;默认为true
before_array_assignments_equals
(null
,bool
): 是否在数组=
赋值运算符前添加、删除或忽略空格;默认为true
ignored_tags
(array
): 必须被视为 Doctrine 注释的标签列表;默认为['abstract', 'access', 'code', 'deprec', 'encode', 'exception', 'final', 'ingroup', 'inheritdoc', 'inheritDoc', 'magic', 'name', 'toc', 'tutorial', 'private', 'static', 'staticvar', 'staticVar', 'throw', 'api', 'author', 'category', 'copyright', 'deprecated', 'example', 'filesource', 'global', 'ignore', 'internal', 'license', 'link', 'method', 'package', 'param', 'property', 'property-read', 'property-write', 'return', 'see', 'since', 'source', 'subpackage', 'throws', 'todo', 'TODO', 'usedBy', 'uses', 'var', 'version', 'after', 'afterClass', 'backupGlobals', 'backupStaticAttributes', 'before', 'beforeClass', 'codeCoverageIgnore', 'codeCoverageIgnoreStart', 'codeCoverageIgnoreEnd', 'covers', 'coversDefaultClass', 'coversNothing', 'dataProvider', 'depends', 'expectedException', 'expectedExceptionCode', 'expectedExceptionMessage', 'expectedExceptionMessageRegExp', 'group', 'large', 'medium', 'preserveGlobalState', 'requires', 'runTestsInSeparateProcesses', 'runInSeparateProcess', 'small', 'test', 'testdox', 'ticket', 'uses', 'SuppressWarnings', 'noinspection', 'package_version', 'enduml', 'startuml', 'fix', 'FIXME', 'fixme', 'override']
elseif [@PSR2, @Symfony, @PhpCsFixer]
应使用关键字
elseif
代替else if
,以便所有控制关键字看起来像单个单词。encoding [@PSR1, @PSR2, @Symfony, @PhpCsFixer]
PHP 代码必须仅使用 UTF-8 无 BOM (删除 BOM)。
ereg_to_preg [@Symfony:risky, @PhpCsFixer:risky]
将已弃用的
ereg
正则表达式函数替换为preg
。危险规则:如果覆盖了 ``ereg`` 函数,则存在危险。
error_suppression [@Symfony:risky, @PhpCsFixer:risky]
应该在弃用通知中添加错误控制运算符,或从其他情况中删除。
危险规则:因为添加/删除 ``@`` 可能会导致代码行为发生变化,或者如果覆盖了 ``trigger_error`` 函数。
配置选项
mute_deprecation_error
(bool
):是否在弃用通知中添加 ``@``;默认为true
noise_remaining_usages
(bool
):是否从剩余用法中删除 ``@``;默认为false
noise_remaining_usages_exclude
(array
):要从删除 ``@`` 中排除的全局函数列表;默认为[]
escape_implicit_backslashes [@PhpCsFixer]
在字符串和 heredoc 中转义隐式反斜杠,以方便理解哪些是 PHP 识别的特殊字符,哪些不是。
配置选项
double_quoted
(bool
):是否修复双引号字符串;默认为true
heredoc_syntax
(bool
):是否修复 heredoc 语法;默认为true
single_quoted
(bool
):是否修复单引号字符串;默认为false
explicit_indirect_variable [@PhpCsFixer]
为间接变量添加大括号,以便理解。需要 PHP >= 7.0。
explicit_string_variable [@PhpCsFixer]
在双引号字符串或 heredoc 语法中将隐式变量转换为显式变量。
final_class
所有类都必须是最终的,除了抽象类和 Doctrine 实体。
危险规则:在非抽象类子类化时存在危险。
final_internal_class [@PhpCsFixer:risky]
内部类应该是
final
。危险规则:将类更改为 ``final`` 可能会导致代码执行中断。
配置选项
annotation-black-list
(array
):必须省略以修复类的类级别注释标签,即使使用白名单中的所有标签。 (不区分大小写);默认为['@final', '@Entity', '@ORM\\Entity', '@ORM\\Mapping\\Entity', '@Mapping\\Entity']
annotation-white-list
(array
):必须设置的类级别注释标签,以便修复类。 (不区分大小写);默认为['@internal']
consider-absent-docblock-as-internal-class
(bool
):是否应将没有任何 DocBlock 的类修复为最终?;默认为false
final_public_method_for_abstract_class
抽象类的所有
public
方法应该是final
。危险规则:覆盖抽象类的 ``public`` 方法时存在危险。
final_static_access
在 ``final`` 类中将
static
访问转换为self
访问。fopen_flag_order [@Symfony:risky, @PhpCsFixer:risky]
在
fopen
调用中排序标志,b
和t
必须是最后的。危险规则:如果覆盖了函数 ``fopen``,则存在危险。
fopen_flags [@Symfony:risky, @PhpCsFixer:risky]
fopen
调用中的标志必须省略t
,并且b
必须省略或一致包含。危险规则:如果覆盖了函数 ``fopen``,则存在危险。
配置选项
b_mode
(bool
):必须使用b
标志(true
)或省略(false
);默认为true
full_opening_tag [@PSR1, @PSR2, @Symfony, @PhpCsFixer]
PHP 代码必须使用长
<?php
标签或短-echo<?=
标签,而不是其他标签变体。fully_qualified_strict_types [@PhpCsFixer]
将函数参数中导入的 FQCN 参数和返回类型转换为短版本。
function_declaration [@PSR2, @Symfony, @PhpCsFixer]
在函数声明中应适当地放置空格。
配置选项
closure_function_spacing
('none'
,'one'
):用于闭包中打开括号前的空格;默认为'one'
function_to_constant [@Symfony:risky, @PhpCsFixer:risky]
将返回常量的核心函数调用替换为常量。
危险规则:当配置为替换的任何函数被覆盖时,则存在危险。
配置选项
functions
(['get_called_class', 'get_class', 'get_class_this', 'php_sapi_name', 'phpversion', 'pi']
的子集):要修复的函数名列表;默认为['get_class', 'php_sapi_name', 'phpversion', 'pi']
function_typehint_space [@Symfony, @PhpCsFixer]
确保函数的参数和类型提示之间有一个空格。
general_phpdoc_annotation_remove
应从PHPDoc中省略配置的注解。
配置选项
annotations
(array
):要移除的注解列表,例如["author"]
;默认为[]
global_namespace_import
导入或完全限定全局类/函数/常量。
配置选项
import_classes
(false
,null
,true
):是否导入、不导入或忽略全局类;默认为true
import_constants
(false
,null
,true
):是否导入、不导入或忽略全局常量;默认为null
import_functions
(false
,null
,true
):是否导入、不导入或忽略全局函数;默认为null
hash_to_slash_comment
单行注释应使用双斜杠
//
而不是井号#
。已弃用:请使用single_line_comment_style
代替。header_comment
添加、替换或删除头部注释。
配置选项
comment_type
('comment'
,'PHPDoc'
):注释语法类型;默认为'comment'
;已弃用别名:commentType
header
(string
):适当的头部内容;必需location
('after_declare_strict'
,'after_open'
):插入头部注释的位置;默认为'after_declare_strict'
separate
('both'
,'bottom'
,'none'
,'top'
):头部注释是否应与文件内容通过新行分隔;默认为'both'
heredoc_indentation [@PHP73Migration]
Heredoc/nowdoc内容必须正确缩进。需要PHP >= 7.3。
heredoc_to_nowdoc [@PhpCsFixer]
尽可能将
heredoc
转换为nowdoc
。implode_call [@Symfony:risky, @PhpCsFixer:risky]
函数
implode
必须按照文档顺序使用两个参数调用。风险规则:当函数
implode
被覆盖时存在风险。include [@Symfony, @PhpCsFixer]
Include/Require和文件路径应以单个空格分隔。文件路径不应放在括号内。
increment_style [@Symfony, @PhpCsFixer]
如果可能,应使用前缀或后缀递增和递减运算符。
配置选项
style
('post'
,'pre'
):是否使用前缀或后缀递增和递减运算符;默认为'pre'
indentation_type [@PSR2, @Symfony, @PhpCsFixer]
代码必须使用配置的缩进类型。
is_null [@Symfony:risky, @PhpCsFixer:risky]
将
is_null($var)
表达式替换为null === $var
。风险规则:当函数
is_null
被覆盖时存在风险。配置选项
use_yoda_style
(bool
):是否应使用Yoda风格条件;默认为true
。已弃用:请使用yoda_style
修复器代替
line_ending [@PSR2, @Symfony, @PhpCsFixer]
所有PHP文件必须使用相同的行结束符。
linebreak_after_opening_tag
确保没有代码与PHP打开标签在同一行。
list_syntax
列表(
array
结构)赋值应使用配置的语法声明。需要PHP >= 7.1。配置选项
syntax
('long'
,'short'
):是否使用long
或short
list
语法;默认为'long'
logical_operators [@PhpCsFixer:risky]
使用逻辑运算符
&&
和||
而不是and
和or
。风险规则:有风险,因为您必须再次检查是否有意使用较低优先级的和/或。
lowercase_cast [@Symfony, @PhpCsFixer]
类型转换应小写。
lowercase_constants
PHP常量
true
、false
和null
必须全部小写。已弃用:请使用constant_case
代替。lowercase_keywords [@PSR2, @Symfony, @PhpCsFixer]
PHP关键字必须全部小写。
lowercase_static_reference [@Symfony, @PhpCsFixer]
类静态引用
self
、static
和parent
必须全部小写。magic_constant_casing [@Symfony, @PhpCsFixer]
应使用正确的命名方式引用魔法常量。
magic_method_casing [@Symfony, @PhpCsFixer]
魔法方法的定义和调用必须使用正确的命名方式。
mb_str_functions
用对应的mb函数替换非多字节安全函数。
风险规则:当任何函数被覆盖时,存在风险。
method_argument_space [@PSR2, @Symfony, @PhpCsFixer]
在方法参数和方法调用中,每个逗号之前不得有空格,每个逗号之后必须有一个空格。参数列表可以跨多行,其中每一行后续行缩进一次。这样做时,列表中的第一项必须在下一行,并且每行只能有一个参数。
配置选项
after_heredoc
(bool
):是否移除heredoc结束和逗号之间的空白;默认为false
ensure_fully_multiline
(bool
):确保多行参数列表的每个参数都单独一行;默认为false
。已弃用:请使用选项on_multiline
代替keep_multiple_spaces_after_comma
(bool
):是否保留逗号后面的多个空格;默认为false
on_multiline
('ensure_fully_multiline'
,'ensure_single_line'
,'ignore'
):定义如何处理包含换行符的函数参数列表;默认为'ignore'
method_chaining_indentation [@PhpCsFixer]
方法链必须正确缩进。不支持不同缩进级别的嵌套方法链。
method_separation
方法之间必须用一行空白分隔。已弃用:请使用
class_attributes_separation
代替。modernize_types_casting [@Symfony:risky, @PhpCsFixer:risky]
用相应的类型转换运算符替换
intval
、floatval
、doubleval
、strval
和boolval
函数调用。风险规则:当任何函数“intval”、“floatval”、“doubleval”、“strval”或“boolval”被覆盖时,存在风险。
multiline_comment_opening_closing [@PhpCsFixer]
DocBlocks必须以两个星号开始,多行注释必须以单个星号开始,在开斜杠之后。两者都必须在关斜杠之前以单个星号结束。
multiline_whitespace_before_semicolons [@PhpCsFixer]
禁止在分号之前有多个空行,或将分号移动到新行以进行链式调用。
配置选项
strategy
('new_line_for_chained_calls'
,'no_multi_line'
):禁止多个空行或将分号移动到新行以进行链式调用;默认为'no_multi_line'
native_constant_invocation [@Symfony:risky, @PhpCsFixer:risky]
在内部常量的常量调用之前添加前导
\
以加快解析。常量名称匹配区分大小写,除了null
、false
和true
。风险规则:当任何常量是命名空间或被覆盖时,存在风险。
配置选项
exclude
(array
):要忽略的常量列表;默认为['null', 'false', 'true']
fix_built_in
(bool
):是否修复由get_defined_constants
返回的常量。用户常量不计入此列表,必须在包含列表中指定;默认为true
include
(array
):要修复的附加常量列表;默认为[]
scope
('all'
,'namespaced'
):仅修复在命名空间内或所有常量调用中的常量调用;默认为'all'
native_function_casing [@Symfony, @PhpCsFixer]
应使用正确的命名方式调用由PHP定义的函数。
native_function_invocation [@Symfony:risky, @PhpCsFixer:risky]
在函数调用前添加前导
\
以加快解析速度。风险规则:当任何函数被覆盖时,存在风险。
配置选项
exclude
(array
):要忽略的函数列表;默认为[]
include
(array
):要修复的函数名或集合。定义的集合有@internal
(所有原生函数)、@all
(所有全局函数)和@compiler_optimized
(由 Zend 特别优化的函数);默认为['@internal']
scope
('all'
,'namespaced'
):仅修复在命名空间内或所有函数调用;默认为'all'
strict
(bool
):是否应移除函数调用前不应有的前导\
;默认为false
native_function_type_declaration_casing [@Symfony, @PhpCsFixer]
函数的原生类型提示应使用正确的格式。
new_with_braces [@Symfony, @PhpCsFixer]
所有使用 new 关键字创建的实例后面都必须有花括号。
no_alias_functions [@Symfony:risky, @PhpCsFixer:risky]
应使用主函数而不是别名。
风险规则:如果任何别名函数被覆盖,则存在风险。
配置选项
sets
(['@internal', '@IMAP', '@mbreg', '@all']
的子集):要修复的集合列表。定义的集合有@internal
(原生函数)、@IMAP
(IMAP 函数)、@mbreg
(来自ext-mbstring
)和@all
(所有列出集合);默认为['@internal', '@IMAP']
no_alternative_syntax [@PhpCsFixer]
将控制结构的选择性语法替换为使用花括号。
no_binary_string [@PhpCsFixer]
字符串前不应有二进制标志。
no_blank_lines_after_class_opening [@Symfony, @PhpCsFixer]
在类开括号后不应有空行。
no_blank_lines_after_phpdoc [@Symfony, @PhpCsFixer]
在文档块和被文档化的元素之间不应有空行。
no_blank_lines_before_namespace
在命名空间声明之前不应有空行。
no_break_comment [@PSR2, @Symfony, @PhpCsFixer]
在非空情况体中故意出现 fall-through 时,必须存在注释。
配置选项
comment_text
(string
):要使用的文本和用于检测的添加注释;默认为'no break'
no_closing_tag [@PSR2, @Symfony, @PhpCsFixer]
仅包含 PHP 的文件中必须省略关闭的
?>
标签。no_empty_comment [@Symfony, @PhpCsFixer]
不应有任何空注释。
no_empty_phpdoc [@Symfony, @PhpCsFixer]
不应有空 PHPDoc 块。
no_empty_statement [@Symfony, @PhpCsFixer]
删除无用的分号语句。
no_extra_blank_lines [@Symfony, @PhpCsFixer]
删除额外的空行和/或配置后的空行。
配置选项
tokens
(['break', 'case', 'continue', 'curly_brace_block', 'default', 'extra', 'parenthesis_brace_block', 'return', 'square_brace_block', 'switch', 'throw', 'use', 'useTrait', 'use_trait']
的子集):要修复的标记列表;默认为['extra']
no_extra_consecutive_blank_lines
删除额外的空行和/或配置后的空行。已弃用:使用
no_extra_blank_lines
代替。配置选项
tokens
(['break', 'case', 'continue', 'curly_brace_block', 'default', 'extra', 'parenthesis_brace_block', 'return', 'square_brace_block', 'switch', 'throw', 'use', 'useTrait', 'use_trait']
的子集):要修复的标记列表;默认为['extra']
no_homoglyph_names [@Symfony:risky, @PhpCsFixer:risky]
替换名称中意外使用的同源字符(非 ASCII 字符)。
风险规则:重命名类且不能重命名文件。您可能对重命名后的代码有字符串引用(``$$name``)。
no_leading_import_slash [@Symfony, @PhpCsFixer]
删除
use
子句中的前导斜杠。no_leading_namespace_whitespace [@Symfony, @PhpCsFixer]
命名空间声明行不应包含前导空白。
no_mixed_echo_print [@Symfony, @PhpCsFixer]
应使用语言构造
print
或echo
。配置选项
use
('echo'
,'print'
):所需的语言构造;默认为'echo'
no_multiline_whitespace_around_double_arrow [@Symfony, @PhpCsFixer]
操作符
=>
不应被多行空格包围。no_multiline_whitespace_before_semicolons
关闭分号前的多行空白是禁止的。已弃用:使用
multiline_whitespace_before_semicolons
代替。no_null_property_initialization [@PhpCsFixer]
属性不得显式用
null
初始化,除非它们有类型声明(PHP 7.4)。no_php4_constructor
将 PHP4 风格的构造函数转换为
__construct
。风险规则:当修复的旧式构造函数被覆盖或覆盖父类时,存在风险。
no_short_bool_cast [@Symfony, @PhpCsFixer]
使用双感叹号进行短布尔类型转换是不应该使用的。
no_short_echo_tag [@PhpCsFixer]
将短 echo 标签
<?=
替换为长格式<?php echo
语法。no_singleline_whitespace_before_semicolons [@Symfony, @PhpCsFixer]
关闭分号前的单行空白是禁止的。
no_spaces_after_function_name [@PSR2, @Symfony, @PhpCsFixer]
在调用方法或函数时,方法或函数名与开括号之间不得有空格。
no_spaces_around_offset [@Symfony, @PhpCsFixer]
在偏移量括号周围不得有空格。
配置选项
positions
(['inside', 'outside']
的子集):是否应在偏移量括号内和/或外修复间距;默认为['inside', 'outside']
no_spaces_inside_parenthesis [@PSR2, @Symfony, @PhpCsFixer]
开括号后不得有空格。关闭括号前不得有空格。
no_superfluous_elseif [@PhpCsFixer]
将多余的
elseif
替换为if
。no_superfluous_phpdoc_tags [@Symfony, @PhpCsFixer]
删除没有提供任何有用信息的
@param
、@return
和@var
标签。配置选项
allow_mixed
(bool
):是否允许没有描述的类型mixed
(true
)或视为多余的(false
);默认为false
allow_unused_params
(bool
):是否允许没有实际签名的param
注释(true
)或视为多余的(false
);默认为false
remove_inheritdoc
(bool
):删除@inheritDoc
标签;默认为false
no_trailing_comma_in_list_call [@Symfony, @PhpCsFixer]
删除列表函数调用中的尾随逗号。
no_trailing_comma_in_singleline_array [@Symfony, @PhpCsFixer]
PHP 单行数组不应有尾随逗号。
no_trailing_whitespace [@PSR2, @Symfony, @PhpCsFixer]
删除非空行末尾的尾随空格。
no_trailing_whitespace_in_comment [@PSR2, @Symfony, @PhpCsFixer]
在注释或 PHPDoc 中不得有尾随空格。
no_unneeded_control_parentheses [@Symfony, @PhpCsFixer]
删除控制语句周围不必要的括号。
配置选项
statements
(array
):要修复的控制语句列表;默认为['break', 'clone', 'continue', 'echo_print', 'return', 'switch_case', 'yield']
no_unneeded_curly_braces [@Symfony, @PhpCsFixer]
删除不必要的花括号,这些花括号是多余的,并且不是控制结构主体的一部分。
配置选项
namespaces
(bool
):从括号命名空间中删除不必要的花括号;默认为false
no_unneeded_final_method [@Symfony:risky, @PhpCsFixer:risky]
必须将
final
类不得有final
方法,且private
方法不得是final
。风险规则:当子类覆盖一个“private”方法时存在风险。
no_unreachable_default_argument_value [@PhpCsFixer:risky]
在函数参数中不得有在非默认参数之前的具有默认值的参数。
风险规则:修改函数签名;因此在使用依赖于这些的系统(例如一些 Symfony 组件)时存在风险。
no_unset_cast [@PhpCsFixer]
必须将变量设置为
null
,而不是使用(unset)
转换。no_unset_on_property [@PhpCsFixer:risky]
应将属性设置为
null
,而不是使用unset
。风险规则:将变量更改为“null”而不是取消设置它们,意味着它们在遍历类变量时仍然会出现。在PHP 7.4中,此规则可能会为不允许的类型声明引入“null”赋值。
no_unused_imports [@Symfony, @PhpCsFixer]
必须删除未使用的
use
语句。no_useless_else [@PhpCsFixer]
不应存在无用的
else
情况。no_useless_return [@PhpCsFixer]
函数末尾不应有空白的
return
语句。no_whitespace_before_comma_in_array [@Symfony, @PhpCsFixer]
在数组声明中,每个逗号之前不得有空格。
配置选项
after_heredoc
(bool
):是否移除heredoc结束和逗号之间的空白;默认为false
no_whitespace_in_blank_line [@Symfony, @PhpCsFixer]
请删除空白行末尾的尾随空格。
non_printable_character [@Symfony:risky, @PhpCsFixer:risky, @PHP70Migration:risky, @PHP71Migration:risky]
请删除零宽空格(ZWSP)、非换行空格(NBSP)和其他不可见的Unicode符号。
风险规则:当字符串包含有意不可见的字符时,是风险的。
配置选项
use_escape_sequences_in_strings
(bool
):是否应将字符替换为字符串中的转义序列;默认为false
normalize_index_brace [@Symfony, @PhpCsFixer]
数组索引应始终使用方括号编写。
not_operator_with_space
逻辑非运算符(
!
)应具有前导和尾随空格。not_operator_with_successor_space
逻辑非运算符(
!
)应有一个尾随空格。nullable_type_declaration_for_default_null_value
为具有默认
null
值的参数添加或删除类型声明前的?
。配置选项
use_nullable_type_declaration
(bool
):是否为具有默认null
值的参数添加或删除类型声明前的?
;默认为true
object_operator_without_whitespace [@Symfony, @PhpCsFixer]
不应在对象
T_OBJECT_OPERATOR
->
之前或之后有空格。ordered_class_elements [@PhpCsFixer]
对类/接口/特性中的元素进行排序。
配置选项
order
(['use_trait', 'public', 'protected', 'private', 'constant', 'constant_public', 'constant_protected', 'constant_private', 'property', 'property_static', 'property_public', 'property_protected', 'property_private', 'property_public_static', 'property_protected_static', 'property_private_static', 'method', 'method_static', 'method_public', 'method_protected', 'method_private', 'method_public_static', 'method_protected_static', 'method_private_static', 'construct', 'destruct', 'magic', 'phpunit']
的子集):定义元素顺序的字符串列表;默认为['use_trait', 'constant_public', 'constant_protected', 'constant_private', 'property_public', 'property_protected', 'property_private', 'construct', 'destruct', 'magic', 'phpunit', 'method_public', 'method_protected', 'method_private']
sortAlgorithm
('alpha'
,'none'
):相同类型语句的多次出现应如何排序;默认为'none'
ordered_imports [@Symfony, @PhpCsFixer]
排序
use
语句。配置选项
imports_order
(array
,null
):定义导入类型的顺序;默认为null
;已弃用的别名:importsOrder
sort_algorithm
('alpha'
,'length'
,'none'
):语句是否应按字母顺序排序或按长度排序,或不排序;默认为'alpha'
;已弃用的别名:sortAlgorithm
ordered_interfaces
在
implements
或interface extends
子句中排序接口。风险规则:当指定接口及其父接口时,在“implements”中是风险的,因为PHP不中断“parent, child”,而是中断“child, parent”。
配置选项
direction
('ascend'
,'descend'
):接口应按哪个方向排序;默认为'ascend'
order
('alpha'
,'length'
):接口应如何排序;默认为'alpha'
php_unit_construct [@Symfony:risky, @PhpCsFixer:risky]
应使用专用方法(如
->assertTrue($foo)
)而不是像->assertSame(true, $foo)
这样的PHPUnit断言方法调用。风险规则:如果覆盖了PHPUnit的本地方法,修复程序可能是风险的。
配置选项
断言
(['assertSame', 'assertEquals', 'assertNotEquals', 'assertNotSame']
的子集):需要修复的断言方法列表;默认为['assertEquals', 'assertSame', 'assertNotEquals', 'assertNotSame']
php_unit_dedicate_assert [@PHPUnit30Migration:risky, @PHPUnit32Migration:risky, @PHPUnit35Migration:risky, @PHPUnit43Migration:risky, @PHPUnit48Migration:risky, @PHPUnit50Migration:risky, @PHPUnit52Migration:risky, @PHPUnit54Migration:risky, @PHPUnit55Migration:risky, @PHPUnit56Migration:risky, @PHPUnit57Migration:risky, @PHPUnit60Migration:risky, @PHPUnit75Migration:risky]
应使用 PHPUnit 断言如
assertInternalType
、assertFileExists
,而不是使用assertTrue
。风险规则:如果覆盖了PHPUnit的本地方法,修复程序可能是风险的。
配置选项
函数
(['array_key_exists', 'empty', 'file_exists', 'is_array', 'is_bool', 'is_callable', 'is_double', 'is_float', 'is_infinite', 'is_int', 'is_integer', 'is_long', 'is_nan', 'is_null', 'is_numeric', 'is_object', 'is_real', 'is_resource', 'is_scalar', 'is_string']
的子集,null
):要修复的断言列表(覆盖target
);默认为null
。已弃用:使用选项target
代替target
('3.0'
,'3.5'
,'5.0'
,'5.6'
,'newest'
):PHPUnit 的目标版本;默认为'5.0'
php_unit_dedicate_assert_internal_type [@PHPUnit75Migration:risky]
应使用 PHPUnit 断言如
assertIsArray
,而不是使用assertInternalType
。风险规则:当 PHPUnit 方法被覆盖或项目存在 PHPUnit 不兼容性时为风险。
配置选项
target
('7.5'
,'newest'
):PHPUnit 的目标版本;默认为'newest'
php_unit_expectation [@PHPUnit52Migration:risky, @PHPUnit54Migration:risky, @PHPUnit55Migration:risky, @PHPUnit56Migration:risky, @PHPUnit57Migration:risky, @PHPUnit60Migration:risky, @PHPUnit75Migration:risky]
->setExpectedException*
方法的使用必须替换为->expectException*
方法。风险规则:当 PHPUnit 类被覆盖或不可访问,或项目存在 PHPUnit 不兼容性时为风险。
配置选项
target
('5.2'
,'5.6'
,'newest'
):PHPUnit 的目标版本;默认为'newest'
php_unit_fqcn_annotation [@Symfony, @PhpCsFixer]
PHPUnit 注释应包含根命名空间的全限定类名。
php_unit_internal_class [@PhpCsFixer]
所有 PHPUnit 测试类应被标记为内部。
配置选项
类型
(['normal', 'final', 'abstract']
的子集):要标记为内部的类类型;默认为['normal', 'final']
php_unit_method_casing [@PhpCsFixer]
强制执行 PHPUnit 测试方法的驼峰(或蛇形)大小写,遵循配置。
配置选项
case
('camel_case'
,'snake_case'
):应用于测试方法的驼峰或蛇形大小写;默认为'camel_case'
php_unit_mock [@PHPUnit54Migration:risky, @PHPUnit55Migration:risky, @PHPUnit56Migration:risky, @PHPUnit57Migration:risky, @PHPUnit60Migration:risky, @PHPUnit75Migration:risky]
->getMock
和->getMockWithoutInvokingTheOriginalConstructor
方法的使用必须替换为->createMock
或->createPartialMock
方法。风险规则:当 PHPUnit 类被覆盖或不可访问,或项目存在 PHPUnit 不兼容性时为风险。
配置选项
target
('5.4'
,'5.5'
,'newest'
):PHPUnit 的目标版本;默认为'newest'
php_unit_mock_short_will_return [@Symfony:risky, @PhpCsFixer:risky]
使用 PHPUnit 模拟的示例(如
->will($this->returnValue(..))
必须替换为其较短的等效形式,如->willReturn(...)
。风险规则:当 PHPUnit 类被覆盖或不可访问,或项目存在 PHPUnit 不兼容性时为风险。
php_unit_namespaced [@PHPUnit48Migration:risky, @PHPUnit50Migration:risky, @PHPUnit52Migration:risky, @PHPUnit54Migration:risky, @PHPUnit55Migration:risky, @PHPUnit56Migration:risky, @PHPUnit57Migration:risky, @PHPUnit60Migration:risky, @PHPUnit75Migration:risky]
PHPUnit 类必须使用命名空间版本,例如使用
\PHPUnit\Framework\TestCase
而不是\PHPUnit_Framework_TestCase
。风险规则:当 PHPUnit 类被覆盖或不可访问,或项目存在 PHPUnit 不兼容性时为风险。
配置选项
target
('4.8'
,'5.7'
,'6.0'
,'newest'
):PHPUnit 的目标版本;默认为'newest'
php_unit_no_expectation_annotation [@PHPUnit32Migration:risky, @PHPUnit35Migration:risky, @PHPUnit43Migration:risky, @PHPUnit48Migration:risky, @PHPUnit50Migration:risky, @PHPUnit52Migration:risky, @PHPUnit54Migration:risky, @PHPUnit55Migration:risky, @PHPUnit56Migration:risky, @PHPUnit57Migration:risky, @PHPUnit60Migration:risky, @PHPUnit75Migration:risky]
@expectedException*
注释的使用必须替换为->setExpectedException*
方法。风险规则:当 PHPUnit 类被覆盖或不可访问,或项目存在 PHPUnit 不兼容性时为风险。
配置选项
target
('3.2'
,'4.3'
,'newest'
):PHPUnit 的目标版本;默认为'newest'
use_class_const
(bool
):使用 ::class 表示法;默认为true
php_unit_ordered_covers [@PhpCsFixer]
对 PHPUnit 测试的
@covers
注释进行排序。php_unit_set_up_tear_down_visibility [@PhpCsFixer:risky]
将 PHPUnit 的
setUp()
和tearDown()
函数的可见性更改为protected
,以匹配 PHPUnit 的 TestCase。风险规则:当类被错误地视为 PHPUnit 测试时,此修复器可能会更改名称为 `setUp()` 或 `tearDown()` 的函数,位于 PHPUnit 测试之外。
php_unit_size_class
所有 PHPUnit 测试用例都应该有
@small
、@medium
或@large
注释以启用运行时限制。配置选项
group
('large'
、'medium'
、'small'
):定义在未使用任何组时使用的特定组;默认为'small'
php_unit_strict [@PhpCsFixer:risky]
应使用 PHPUnit 方法
assertSame
而不是assertEquals
。风险规则:当任何函数被覆盖或测试对象相等性时,存在风险。
配置选项
assertions
(['assertAttributeEquals', 'assertAttributeNotEquals', 'assertEquals', 'assertNotEquals']
的子集):要修复的断言方法列表;默认为['assertAttributeEquals', 'assertAttributeNotEquals', 'assertEquals', 'assertNotEquals']
php_unit_test_annotation [@PhpCsFixer:risky]
根据配置添加或删除测试中的 @test 注释。
风险规则:此修复器可能会更改测试的名称,并可能导致与抽象类或接口不兼容。
配置选项
case
('camel'
、'snake'
):在添加测试前缀时使用 camel 或 snake 案;默认为'camel'
。已弃用:请使用php_unit_method_casing
修复器代替style
('annotation'
、'prefix'
):是否使用 @test 注释;默认为'prefix'
php_unit_test_case_static_method_calls [@PhpCsFixer:risky]
对
PHPUnit\Framework\TestCase
静态方法的调用必须都是同一类型,即$this-
、self::
或static::
。风险规则:当 PHPUnit 方法被覆盖或不可访问,或项目存在 PHPUnit 不兼容性时,存在风险。
配置选项
call_type
('self'
、'static'
、'this'
):用于引用 PHPUnit 方法的调用类型;默认为'static'
methods
(array
):字典method
=>call_type
值,与默认策略不同;默认为[]
php_unit_test_class_requires_covers [@PhpCsFixer]
为没有
@covers*
注释的 PHPUnit 测试类添加默认的@coversNothing
注释。phpdoc_add_missing_param_annotation [@PhpCsFixer]
PHPDoc 应包含所有参数的
@param
。配置选项
only_untyped
(bool
):是否只为未类型化的参数添加缺失的@param
注释;默认为true
phpdoc_align [@Symfony, @PhpCsFixer]
给定 phpdoc 标签的所有项必须都是左对齐的,或者(默认)垂直对齐。
配置选项
align
('left'
、'vertical'
):对齐注释;默认为'vertical'
tags
(['param', 'property', 'property-read', 'property-write', 'return', 'throws', 'type', 'var', 'method']
的子集):应对齐的标签;默认为['param', 'return', 'throws', 'type', 'var']
phpdoc_annotation_without_dot [@Symfony, @PhpCsFixer]
PHPDoc 注释描述不应该是句子。
phpdoc_indent [@Symfony, @PhpCsFixer]
Docblocks 的缩进应与文档化的主题相同。
phpdoc_inline_tag [@Symfony, @PhpCsFixer]
修复 PHPDoc 内联标签,使
@inheritdoc
总是内联。phpdoc_line_span
将文档块从单行更改为多行,或相反。仅适用于类常量、属性和方法。
配置选项
const
('multi'
,'single'
):确定const块是单行还是多行;默认为'multi'
method
('multi'
,'single'
):确定方法文档块是单行还是多行;默认为'multi'
property
('multi'
,'single'
):确定属性文档块是单行还是多行;默认为'multi'
phpdoc_no_access [@Symfony, @PhpCsFixer]
应从PHPDoc中省略
@access
注解。phpdoc_no_alias_tag [@Symfony, @PhpCsFixer]
不应使用别名PHPDoc标签。
配置选项
replacements
(array
):替换注解与新注解之间的映射;默认为['property-read' => 'property', 'property-write' => 'property', 'type' => 'var', 'link' => 'see']
phpdoc_no_empty_return [@PhpCsFixer]
应从PHPDoc中省略
@return void
和@return null
注解。phpdoc_no_package [@Symfony, @PhpCsFixer]
应从PHPDoc中省略
@package
和@subpackage
注解。phpdoc_no_useless_inheritdoc [@Symfony, @PhpCsFixer]
不继承的类不得有
@inheritdoc
标签。phpdoc_order [@PhpCsFixer]
PHPDoc中的注解应按顺序排列,首先为
@param
注解,然后为@throws
注解,最后为@return
注解。phpdoc_return_self_reference [@Symfony, @PhpCsFixer]
返回自身引用的方法的
@return
注解的类型必须配置为相应的类型。配置选项
replacements
(array
):替换返回类型与新类型之间的映射;默认为['this' => '$this', '@this' => '$this', '$self' => 'self', '@self' => 'self', '$static' => 'static', '@static' => 'static']
phpdoc_scalar [@Symfony, @PhpCsFixer]
标量类型应始终以相同的形式书写。
int
不是integer
,bool
不是boolean
,float
不是real
或double
。配置选项
types
(一个子集为['boolean', 'callback', 'double', 'integer', 'real', 'str']
):要修复的类型映射;默认为['boolean', 'double', 'integer', 'real', 'str']
phpdoc_separation [@Symfony, @PhpCsFixer]
PHPDoc中的注解应分组在一起,使得相同类型的注解紧随其后,不同类型的注解之间用空行隔开。
phpdoc_single_line_var_spacing [@Symfony, @PhpCsFixer]
单行
@var
PHPDoc应具有适当的间距。phpdoc_summary [@Symfony, @PhpCsFixer]
PHPDoc摘要应以句号、感叹号或问号结尾。
phpdoc_to_comment [@Symfony, @PhpCsFixer]
仅应在结构元素上使用文档块。
phpdoc_to_param_type
实验性:处理非混合类型的
@param
注解,并相应地调整函数签名。需要PHP >= 7.0。风险规则:[1] 此规则是实验性的,不保证向后兼容。[2] ``@param``注解对于修复器进行更改是强制性的,没有它的方法签名(没有文档,继承文档)将不会被修复。[3] 如果继承的签名没有正确记录,则需要手动操作。
配置选项
scalar_types
(bool
):修复标量类型;由于PHP类型强制系统不佳,可能存在意外行为;默认为true
phpdoc_to_return_type
实验性:处理非混合类型的
@return
注解,并相应地调整函数签名。需要PHP >= 7.0。风险规则:[1] 此规则是实验性的,不保证向后兼容。[2] ``@return``注解对于修复器进行更改是强制性的,没有它的方法签名(没有文档,继承文档)将不会被修复。[3] 如果继承的签名没有正确记录,则需要手动操作。[4] ``@inheritdocs``支持正在建设中。
配置选项
scalar_types
(bool
):修复标量类型;由于PHP类型强制系统不佳,可能存在意外行为;默认为true
phpdoc_trim [@Symfony, @PhpCsFixer]
PHPDoc应该以内容开始和结束,不包括docblocks的第一个和最后一行。
phpdoc_trim_consecutive_blank_line_separation [@Symfony, @PhpCsFixer]
删除PHPDoc中的摘要和描述之后的额外空行。
phpdoc_types [@Symfony, @PhpCsFixer]
PHPDoc中必须使用正确的拼写标准PHP类型。
配置选项
groups
(['simple', 'alias', 'meta']
的子集):要修复的类型组;默认为['simple', 'alias', 'meta']
phpdoc_types_order [@Symfony, @PhpCsFixer]
排序PHPDoc类型。
配置选项
null_adjustment
('always_first'
,'always_last'
,'none'
):强制null
的位置(覆盖sort_algorithm
);默认为'always_first'
sort_algorithm
('alpha'
,'none'
):要应用的排序算法;默认为'alpha'
phpdoc_var_annotation_correct_order [@PhpCsFixer]
@var
和@type
注释的类型和名称必须以正确的顺序排列。phpdoc_var_without_name [@Symfony, @PhpCsFixer]
类属性的
@var
和@type
注释不应包含名称。pow_to_exponentiation [@PHP56Migration:risky, @PHP70Migration:risky, @PHP71Migration:risky]
将
pow
转换为**
运算符。风险规则:当函数`pow`被覆盖时存在风险。
pre_increment
如果可能,应使用前增量/减量。已弃用:请改用
increment_style
。protected_to_private [@PhpCsFixer]
尽可能将
protected
变量和方法转换为private
。psr0
类必须位于与它们的命名空间匹配的路径中,至少有一个命名空间深度,并且类名应与文件名匹配。
风险规则:此修复程序可能会更改您的类名,这将破坏依赖于旧名称的代码。
配置选项
dir
(string
):放置项目代码的目录;默认为''
psr4 [@Symfony:risky, @PhpCsFixer:risky]
类名应与文件名匹配。
风险规则:此修复程序可能会更改您的类名,这将破坏依赖于旧名称的代码。
random_api_migration [@PHP70Migration:risky, @PHP71Migration:risky]
将
rand
、srand
、getrandmax
函数调用替换为其mt_*
类似物。风险规则:当配置的函数被覆盖时存在风险。
配置选项
replacements
(array
):替换函数与新函数之间的映射;默认为['getrandmax' => 'mt_getrandmax', 'rand' => 'mt_rand', 'srand' => 'mt_srand']
return_assignment [@PhpCsFixer]
本地、动态和直接引用的变量不应由函数或方法分配和直接返回。
return_type_declaration [@Symfony, @PhpCsFixer]
根据配置,在返回类型声明中应该在冒号前有一个或没有空格,在冒号后有一个空格。
配置选项
space_before
('none'
,'one'
):在冒号前应用的间隔;默认为'none'
self_accessor [@Symfony:risky, @PhpCsFixer:risky]
在类或接口元素内部应优先使用
self
而不是类名本身。风险规则:在使用动态调用(如get_called_class()或晚期静态绑定)时存在风险。
self_static_accessor
在
final
类或匿名类内部应优先使用self
而不是static
。semicolon_after_instruction [@Symfony, @PhpCsFixer]
指令必须以分号结尾。
set_type_to_cast [@Symfony:risky, @PhpCsFixer:risky]
应使用类型转换,而不是
settype
。风险规则:当`settype`函数被覆盖或作为`for`循环中的第2或第3个表达式使用时存在风险。
short_scalar_cast [@Symfony, @PhpCsFixer]
将
(boolean)
和(integer)
写成(bool)
和(int)
,将(double)
和(real)
写成(float)
,将(binary)
写成(string)
。silenced_deprecation_error
确保静默弃用通知。已弃用:请改用
error_suppression
。风险规则:静默弃用错误可能会改变代码行为。
simple_to_complex_string_variable [@PhpCsFixer]
将双引号字符串和heredoc语法中的显式变量从简单格式转换为复杂格式(从
${
到{$
)。simplified_null_return
希望返回
void
的返回语句不应该返回null
。single_blank_line_at_eof [@PSR2, @Symfony, @PhpCsFixer]
没有结束标签的PHP文件必须始终以一个空行结束。
single_blank_line_before_namespace [@Symfony, @PhpCsFixer]
在命名空间声明之前应恰好有一个空行。
single_class_element_per_statement [@PSR2, @Symfony, @PhpCsFixer]
每条语句中必须不能有超过一个属性或常量声明。
配置选项
elements
(['const', 'property']
的子集):需要修改的元素字符串列表;默认为['const', 'property']
single_import_per_statement [@PSR2, @Symfony, @PhpCsFixer]
每个声明必须有一个use关键字。
single_line_after_imports [@PSR2, @Symfony, @PhpCsFixer]
每个命名空间使用必须单独一行,并且use语句块之后必须有一个空行。
single_line_comment_style [@Symfony, @PhpCsFixer]
单行注释和多行注释(仅有一行实际内容)应使用
//
语法。配置选项
comment_types
(['asterisk', 'hash']
的子集):要修复的注释类型列表;默认为['asterisk', 'hash']
single_line_throw [@Symfony]
抛出异常必须在一行内完成。
single_quote [@Symfony, @PhpCsFixer]
对于简单字符串,将双引号转换为单引号。
配置选项
strings_containing_single_quote_chars
(bool
):是否修复包含单引号的双引号字符串;默认为false
single_trait_insert_per_statement [@Symfony, @PhpCsFixer]
每个trait
use
必须作为一个单独的语句完成。space_after_semicolon [@Symfony, @PhpCsFixer]
修复分号后的空格。
配置选项
remove_in_empty_for_expressions
(bool
):是否应该从空for
表达式移除空格;默认为false
standardize_increment [@Symfony, @PhpCsFixer]
如果可能,应使用增量或减量运算符。
standardize_not_equals [@Symfony, @PhpCsFixer]
将所有
<>
替换为!=
。static_lambda
不(间接)引用
$this
的lambdas必须声明为static
。风险规则:当在未引用
$this
的lambdas上使用``>bindTo``时存在风险。strict_comparison [@PhpCsFixer:risky]
比较应该是严格的。
风险规则:将比较更改为严格可能会改变代码行为。
strict_param [@PhpCsFixer:risky]
函数应使用
$strict
参数设置为true
使用。风险规则:当固定的函数被覆盖或代码依赖于非严格使用时存在风险。
string_line_ending [@PhpCsFixer:risky]
所有多行字符串都必须使用正确的行结束符。
风险规则:更改多行字符串的行结束可能会影响字符串比较和输出。
switch_case_semicolon_to_colon [@PSR2, @Symfony, @PhpCsFixer]
case后应跟冒号而不是分号。
switch_case_space [@PSR2, @Symfony, @PhpCsFixer]
删除冒号和case值之间的额外空格。
ternary_operator_spaces [@Symfony, @PhpCsFixer]
标准化三目运算符周围的空格。
ternary_to_null_coalescing [@PHP70Migration, @PHP71Migration, @PHP73Migration]
尽可能使用
null
合并运算符??
。需要PHP >= 7.0。trailing_comma_in_multiline_array [@Symfony, @PhpCsFixer]
PHP多行数组应有一个尾随逗号。
配置选项
after_heredoc
(bool
): 是否在heredoc结束符后也放置逗号;默认为false
trim_array_spaces [@Symfony, @PhpCsFixer]
数组应该像函数/方法参数一样格式化,没有前导或尾随的单行空格。
unary_operator_spaces [@Symfony, @PhpCsFixer]
一元运算符应紧挨着它们的操作数。
visibility_required [@PSR2, @Symfony, @PhpCsFixer, @PHP71Migration, @PHP73Migration]
所有属性和方法都必须声明可见性;必须在可见性之前声明
abstract
和final
;必须在可见性之后声明static
。配置选项
elements
(一组['property', 'method', 'const']
):需要修复的结构性元素(需要PHP >= 7.1以支持const
);默认为['property', 'method']
void_return [@PHP71Migration:risky]
向缺少或空返回语句的函数添加
void
返回类型,但优先级高于@return
注释。需要PHP >= 7.1。有风险规则:修改函数签名。
whitespace_after_comma_in_array [@Symfony, @PhpCsFixer]
在数组声明中,必须在每个逗号后有一个空白。
yoda_style [@Symfony, @PhpCsFixer]
根据配置,以Yoda风格(
true
)、非Yoda风格(false
)或忽略这些条件(null
)编写条件。配置选项
always_move_variable
(bool
):在应用Yoda风格时,变量是否始终位于不可赋值的一侧;默认为false
equal
(bool
,null
):等于(==
,!=
)语句的风格;默认为true
identical
(bool
,null
):相同(===
,!==
)语句的风格;默认为true
less_and_greater
(bool
,null
):小于和大于(<
,<=
,>
,>=
)语句的风格;默认为null
使用--dry-run
选项将显示需要修复的文件,但不会实际修改它们。
$ php php-cs-fixer.phar fix /path/to/code --dry-run
配置文件
您可以使用命令行选项来定制规则,而不是使用配置文件,您可以将项目配置保存到项目根目录中的.php_cs.dist
文件中。该文件必须返回一个PhpCsFixer\ConfigInterface的实例,该实例允许您配置规则、需要分析的文件和目录。您还可以创建一个.php_cs
文件,这是本地配置,将用于代替项目配置。将此文件添加到.gitignore
文件是一个好习惯。使用--config
选项,您可以指定.php_cs
文件的路径。
以下示例将向默认的PSR2规则列表中添加两个规则
<?php $finder = PhpCsFixer\Finder::create() ->exclude('somedir') ->notPath('src/Symfony/Component/Translation/Tests/fixtures/resources.php') ->in(__DIR__) ; return PhpCsFixer\Config::create() ->setRules([ '@PSR2' => true, 'strict_param' => true, 'array_syntax' => ['syntax' => 'short'], ]) ->setFinder($finder) ;
注意:exclude
只对目录有效,所以如果您需要排除文件,请尝试使用notPath
。这两个方法都只接受使用in
方法定义的相对路径。
有关其他Finder方法,请参阅Symfony\Finder的在线文档。
您还可以使用规则的黑名单而不是上述的白色名单方法。以下示例显示了如何使用所有Symfony
规则,但排除full_opening_tag
规则。
<?php $finder = PhpCsFixer\Finder::create() ->exclude('somedir') ->in(__DIR__) ; return PhpCsFixer\Config::create() ->setRules([ '@Symfony' => true, 'full_opening_tag' => false, ]) ->setFinder($finder) ;
您可能想在项目中使用非Linux空白字符。那么,您需要在配置文件中配置它们。
<?php return PhpCsFixer\Config::create() ->setIndent("\t") ->setLineEnding("\r\n") ;
通过使用带有yes
或no
的--using-cache
选项,您可以设置是否应使用缓存机制。
缓存
缓存机制默认启用。这将通过只修复上次运行后发生更改的文件来加速后续运行。如果工具版本已更改或规则列表已更改,则工具将修复所有文件。仅支持将工具作为phar文件下载或通过composer安装时使用的缓存。
可以通过--using-cache
选项或配置文件禁用缓存
<?php return PhpCsFixer\Config::create() ->setUsingCache(false) ;
可以通过--cache-file
选项或配置文件指定缓存文件
<?php return PhpCsFixer\Config::create() ->setCacheFile(__DIR__.'/.php_cs.cache') ;
在持续集成中使用PHP CS Fixer
将friendsofphp/php-cs-fixer
作为dev
依赖项要求
$ ./composer.phar require --dev friendsofphp/php-cs-fixer
然后,将以下命令添加到您的持续集成中
$ IFS=' $ ' $ CHANGED_FILES=$(git diff --name-only --diff-filter=ACMRTUXB "${COMMIT_RANGE}") $ if ! echo "${CHANGED_FILES}" | grep -qE "^(\\.php_cs(\\.dist)?|composer\\.lock)$"; then EXTRA_ARGS=$(printf -- '--path-mode=intersection\n--\n%s' "${CHANGED_FILES}"); else EXTRA_ARGS=''; fi $ vendor/bin/php-cs-fixer fix --config=.php_cs.dist -v --dry-run --stop-on-violation --using-cache=no ${EXTRA_ARGS}
其中$COMMIT_RANGE
是您的提交范围,例如$TRAVIS_COMMIT_RANGE
或HEAD~..HEAD
。
退出代码
退出代码是使用以下位标志构建的
- 0 - 正常。
- 1 - 通用错误(或未满足PHP最小要求)。
- 4 - 一些文件具有无效的语法(仅在dry-run模式下)。
- 8 - 一些文件需要修复(仅在dry-run模式下)。
- 16 - 应用程序的配置错误。
- 32 - Fixer的配置错误。
- 64 - 应用程序内部抛出异常。
(仅适用于fix
命令的退出代码)
辅助工具
为以下内容存在专门的插件
贡献
该工具包含相当多的内置修复程序,但每个人都欢迎贡献更多。
修复程序
修复程序是一个尝试修复一个CS问题(Fixer
类必须实现FixerInterface
)的类。
配置
配置了解CS规则以及工具在运行在项目目录时必须扫描的文件和目录。对于遵循已知目录结构的项目(例如Symfony项目)非常有用。