slevomat / coding-standard
Slevomat Coding Standard for PHP_CodeSniffer 通过提供额外的检查,补充了 Consistence Coding Standard。
Requires
- php: ^7.2 || ^8.0
- dealerdirect/phpcodesniffer-composer-installer: ^0.6.2 || ^0.7 || ^1.0
- phpstan/phpdoc-parser: ^1.23.1
- squizlabs/php_codesniffer: ^3.9.0
Requires (Dev)
- phing/phing: 2.17.4
- php-parallel-lint/php-parallel-lint: 1.3.2
- phpstan/phpstan: 1.10.60
- phpstan/phpstan-deprecation-rules: 1.1.4
- phpstan/phpstan-phpunit: 1.3.16
- phpstan/phpstan-strict-rules: 1.5.2
- phpunit/phpunit: 8.5.21|9.6.8|10.5.11
- dev-master / 8.x-dev
- 8.15.0
- 8.14.1
- 8.14.0
- 8.13.4
- 8.13.3
- 8.13.2
- 8.13.1
- 8.13.0
- 8.12.1
- 8.12.0
- 8.11.1
- 8.11.0
- 8.10.0
- 8.9.2
- 8.9.1
- 8.9.0
- 8.8.0
- 8.7.1
- 8.7.0
- 8.6.4
- 8.6.3
- 8.6.2
- 8.6.1
- 8.6.0
- 8.5.2
- 8.5.1
- 8.5.0
- 8.4.0
- 8.3.0
- 8.2.0
- 8.1.0
- 8.0.1
- 8.0.0
- 7.2.1
- 7.2.0
- 7.1
- 7.0.20
- 7.0.19
- 7.0.18
- 7.0.17
- 7.0.16
- 7.0.15
- 7.0.14
- 7.0.13
- 7.0.12
- 7.0.11
- 7.0.10
- 7.0.9
- 7.0.8
- 7.0.7
- 7.0.6
- 7.0.5
- 7.0.4
- 7.0.3
- 7.0.2
- 7.0.1
- 7.0.0
- 6.4.1
- 6.4.0
- 6.3.11
- 6.3.10
- 6.3.9
- 6.3.8
- 6.3.7
- 6.3.6
- 6.3.5
- 6.3.4
- 6.3.3
- 6.3.2
- 6.3.1
- 6.3.0
- 6.2.0
- 6.1.5
- 6.1.4
- 6.1.3
- 6.1.2
- 6.1.1
- 6.1.0
- 6.0.8
- 6.0.7
- 6.0.6
- 6.0.5
- 6.0.4
- 6.0.3
- 6.0.2
- 6.0.1
- 6.0.0
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.8.7
- 4.8.6
- 4.8.5
- 4.8.4
- 4.8.3
- 4.8.2
- 4.8.1
- 4.8.0
- 4.7.3
- 4.7.2
- 4.7.1
- 4.7.0
- 4.6.3
- 4.6.2
- 4.6.1
- 4.6.0
- 4.5.2
- 4.5.1
- 4.5.0
- 4.4.8
- 4.4.7
- 4.4.6
- 4.4.5
- 4.4.4
- 4.4.3
- 4.4.2
- 4.4.1
- 4.4.0
- 4.3.2
- 4.3.1
- 4.3.0
- 4.2.1
- 4.2.0
- 4.1.0
- 4.0.0
- 3.3.0
- 3.2.1
- 3.2.0
- 3.1.2
- 3.1.1
- 3.1.0
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- 2.5.0
- 2.4.7
- 2.4.6
- 2.4.5
- 2.4.4
- 2.4.3
- 2.4.2
- 2.4.1
- 2.4.0
- 2.3.0
- 2.2.0
- 2.1.0
- 2.0.2
- 2.0.1
- 2.0
- 1.1.1
- 1.1
- 1.0.7
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- dev-phpdoc-parser-2
- dev-add-typos
- dev-phpstan
This package is auto-updated.
Last update: 2024-09-16 07:18:56 UTC
README
Slevomat Coding Standard for PHP_CodeSniffer 提供了三个类别的 sniffs
- 功能 - 提高代码的安全性和行为
- 清洁 - 检测死代码
- 格式化 - 规则以确保代码外观一致
目录
Sniffs 的字母表列表
🔧 = 自动错误修复
🚧 = 本地可抑制 sniff 检查
- SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys 🔧
- SlevomatCodingStandard.Arrays.DisallowImplicitArrayCreation
- SlevomatCodingStandard.Arrays.DisallowPartiallyKeyed 🚧
- SlevomatCodingStandard.Arrays.MultiLineArrayEndBracketPlacement 🔧
- SlevomatCodingStandard.Arrays.ArrayAccessSniff.php 🔧
- SlevomatCodingStandard.Arrays.SingleLineArrayWhitespace 🔧
- SlevomatCodingStandard.Arrays.TrailingArrayComma 🔧
- SlevomatCodingStandard.Attributes.AttributeAndTargetSpacing 🔧
- SlevomatCodingStandard.Attributes.AttributesOrder 🔧
- SlevomatCodingStandard.Attributes.DisallowAttributesJoining 🔧
- SlevomatCodingStandard.Attributes.DisallowMultipleAttributesPerLine 🔧
- SlevomatCodingStandard.Attributes.RequireAttributeAfterDocComment 🔧
- SlevomatCodingStandard.Classes.BackedEnumTypeSpacing 🔧
- SlevomatCodingStandard.Classes.ClassConstantVisibility 🔧
- SlevomatCodingStandard.Classes.ClassLength
- SlevomatCodingStandard.Classes.ClassMemberSpacing 🔧
- SlevomatCodingStandard.Classes.ClassStructure 🔧
- SlevomatCodingStandard.Classes.ConstantSpacing 🔧
- SlevomatCodingStandard.Classes.DisallowConstructorPropertyPromotion
- SlevomatCodingStandard.Classes.DisallowLateStaticBindingForConstants 🔧
- SlevomatCodingStandard.Classes.DisallowMultiConstantDefinition 🔧
- SlevomatCodingStandard.Classes.DisallowMultiPropertyDefinition 🔧
- SlevomatCodingStandard.Classes.DisallowStringExpressionPropertyFetch 🔧
- SlevomatCodingStandard.Classes.EmptyLinesAroundClassBraces 🔧
- SlevomatCodingStandard.Classes.EnumCaseSpacing 🔧
- SlevomatCodingStandard.Classes.ForbiddenPublicProperty
- SlevomatCodingStandard.Classes.MethodSpacing 🔧
- SlevomatCodingStandard.Classes.ModernClassNameReference 🔧
- SlevomatCodingStandard.Classes.ParentCallSpacing 🔧
- SlevomatCodingStandard.Classes.PropertyDeclaration 🔧
- SlevomatCodingStandard.Classes.PropertySpacing 🔧
- SlevomatCodingStandard.Classes.RequireAbstractOrFinal 🔧
- SlevomatCodingStandard.Classes.RequireConstructorPropertyPromotion 🔧
- SlevomatCodingStandard.Classes.RequireMultiLineMethodSignature 🔧
- SlevomatCodingStandard.Classes.RequireSelfReference 🔧
- SlevomatCodingStandard.Classes.RequireSingleLineMethodSignature 🔧
- SlevomatCodingStandard.Classes.SuperfluousAbstractClassNaming
- SlevomatCodingStandard.Classes.SuperfluousErrorNaming
- SlevomatCodingStandard.Classes.SuperfluousExceptionNaming
- SlevomatCodingStandard.Classes.SuperfluousInterfaceNaming
- SlevomatCodingStandard.Classes.SuperfluousTraitNaming
- SlevomatCodingStandard.Classes.TraitUseDeclaration 🔧
- SlevomatCodingStandard.Classes.TraitUseSpacing 🔧
- SlevomatCodingStandard.Classes.UselessLateStaticBinding 🔧
- SlevomatCodingStandard.Commenting.AnnotationName
- SlevomatCodingStandard.Commenting.DeprecatedAnnotationDeclaration
- SlevomatCodingStandard.Commenting.DisallowCommentAfterCode 🔧
- SlevomatCodingStandard.Commenting.DisallowOneLinePropertyDocComment 🔧
- SlevomatCodingStandard.Commenting.DocCommentSpacing 🔧
- SlevomatCodingStandard.Commenting.EmptyComment 🔧
- SlevomatCodingStandard.Commenting.ForbiddenAnnotations 🔧
- SlevomatCodingStandard.Commenting.ForbiddenComments 🔧
- SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration 🔧
- SlevomatCodingStandard.Commenting.RequireOneLineDocComment 🔧
- SlevomatCodingStandard.Commenting.RequireOneLinePropertyDocComment 🔧
- SlevomatCodingStandard.Commenting.UselessFunctionDocComment 🔧
- SlevomatCodingStandard.Commenting.UselessInheritDocComment 🔧
- SlevomatCodingStandard.Complexity.Cognitive
- SlevomatCodingStandard.ControlStructures.AssignmentInCondition
- SlevomatCodingStandard.ControlStructures.BlockControlStructureSpacing 🔧
- SlevomatCodingStandard.ControlStructures.DisallowContinueWithoutIntegerOperandInSwitch 🔧
- SlevomatCodingStandard.ControlStructures.DisallowEmpty
- SlevomatCodingStandard.ControlStructures.DisallowNullSafeObjectOperator
- SlevomatCodingStandard.ControlStructures.DisallowShortTernaryOperator 🔧
- SlevomatCodingStandard.ControlStructures.DisallowTrailingMultiLineTernaryOperatorSniff 🔧
- SlevomatCodingStandard.ControlStructures.DisallowYodaComparison 🔧
- SlevomatCodingStandard.ControlStructures.EarlyExit 🔧
- SlevomatCodingStandard.ControlStructures.JumpStatementsSpacing 🔧
- SlevomatCodingStandard.ControlStructures.LanguageConstructWithParentheses 🔧
- SlevomatCodingStandard.ControlStructures.NewWithParentheses 🔧
- SlevomatCodingStandard.ControlStructures.NewWithoutParentheses 🔧
- SlevomatCodingStandard.ControlStructures.RequireMultiLineCondition 🔧
- SlevomatCodingStandard.ControlStructures.RequireMultiLineTernaryOperator 🔧
- SlevomatCodingStandard.ControlStructures.RequireNullCoalesceEqualOperator 🔧
- SlevomatCodingStandard.ControlStructures.RequireNullCoalesceOperator 🔧
- SlevomatCodingStandard.ControlStructures.RequireNullSafeObjectOperator 🔧
- SlevomatCodingStandard.ControlStructures.RequireShortTernaryOperator 🔧
- SlevomatCodingStandard.ControlStructures.RequireSingleLineCondition 🔧
- SlevomatCodingStandard.ControlStructures.RequireTernaryOperator 🔧
- SlevomatCodingStandard.ControlStructures.RequireYodaComparison 🔧
- SlevomatCodingStandard.ControlStructures.UselessIfConditionWithReturn 🔧
- SlevomatCodingStandard.ControlStructures.UselessTernaryOperator 🔧
- SlevomatCodingStandard.Exceptions.DeadCatch
- SlevomatCodingStandard.Exceptions.DisallowNonCapturingCatch
- SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly 🔧🚧
- SlevomatCodingStandard.Exceptions.RequireNonCapturingCatch 🔧
- SlevomatCodingStandard.Files.FileLength
- SlevomatCodingStandard.Files.LineLength
- SlevomatCodingStandard.Files.TypeNameMatchesFileName
- SlevomatCodingStandard.Functions.ArrowFunctionDeclaration 🔧
- SlevomatCodingStandard.Functions.DisallowArrowFunction
- SlevomatCodingStandard.Functions.DisallowEmptyFunction
- SlevomatCodingStandard.Functions.DisallowNamedArguments
- SlevomatCodingStandard.Functions.DisallowTrailingCommaInCall 🔧
- SlevomatCodingStandard.Functions.DisallowTrailingCommaInClosureUse 🔧
- SlevomatCodingStandard.Functions.DisallowTrailingCommaInDeclaration 🔧
- SlevomatCodingStandard.Functions.FunctionLength
- SlevomatCodingStandard.Functions.NamedArgumentSpacing 🔧
- SlevomatCodingStandard.Functions.RequireArrowFunction 🔧
- SlevomatCodingStandard.Functions.RequireMultiLineCall 🔧
- SlevomatCodingStandard.Functions.RequireSingleLineCall 🔧
- SlevomatCodingStandard.Functions.RequireTrailingCommaInCall 🔧
- SlevomatCodingStandard.Functions.RequireTrailingCommaInClosureUse 🔧
- SlevomatCodingStandard.Functions.RequireTrailingCommaInDeclaration 🔧
- SlevomatCodingStandard.Functions.StaticClosure 🔧
- SlevomatCodingStandard.Functions.StrictCall
- SlevomatCodingStandard.Functions.UnusedInheritedVariablePassedToClosure 🔧
- SlevomatCodingStandard.Functions.UnusedParameter 🚧
- SlevomatCodingStandard.Functions.UselessParameterDefaultValue 🚧
- SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses 🔧
- SlevomatCodingStandard.Namespaces.DisallowGroupUse
- SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameInAnnotation 🔧
- SlevomatCodingStandard.Namespaces.FullyQualifiedExceptions 🔧
- SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalConstants 🔧
- SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalFunctions 🔧
- SlevomatCodingStandard.Namespaces.MultipleUsesPerLine
- SlevomatCodingStandard.Namespaces.NamespaceDeclaration 🔧
- SlevomatCodingStandard.Namespaces.NamespaceSpacing 🔧
- SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly 🔧
- SlevomatCodingStandard.Namespaces.RequireOneNamespaceInFile
- SlevomatCodingStandard.Namespaces.UnusedUses 🔧
- SlevomatCodingStandard.Namespaces.UseDoesNotStartWithBackslash 🔧
- SlevomatCodingStandard.Namespaces.UseFromSameNamespace 🔧
- SlevomatCodingStandard.Namespaces.UseOnlyWhitelistedNamespaces
- SlevomatCodingStandard.Namespaces.UseSpacing 🔧
- SlevomatCodingStandard.Namespaces.UselessAlias 🔧
- SlevomatCodingStandard.Numbers.DisallowNumericLiteralSeparator 🔧
- SlevomatCodingStandard.Numbers.RequireNumericLiteralSeparator
- SlevomatCodingStandard.Operators.DisallowEqualOperators 🔧
- SlevomatCodingStandard.Operators.DisallowIncrementAndDecrementOperators
- SlevomatCodingStandard.Operators.NegationOperatorSpacing 🔧
- SlevomatCodingStandard.Operators.RequireCombinedAssignmentOperator 🔧
- SlevomatCodingStandard.Operators.RequireOnlyStandaloneIncrementAndDecrementOperators
- SlevomatCodingStandard.Operators.SpreadOperatorSpacing 🔧
- SlevomatCodingStandard.PHP.DisallowDirectMagicInvokeCall 🔧
- SlevomatCodingStandard.PHP.DisallowReference
- SlevomatCodingStandard.PHP.ForbiddenClasses 🔧
- SlevomatCodingStandard.PHP.OptimizedFunctionsWithoutUnpacking
- SlevomatCodingStandard.PHP.ReferenceSpacing 🔧
- SlevomatCodingStandard.PHP.RequireExplicitAssertion 🔧
- SlevomatCodingStandard.PHP.RequireNowdoc 🔧
- SlevomatCodingStandard.PHP.ShortList 🔧
- SlevomatCodingStandard.PHP.TypeCast 🔧
- SlevomatCodingStandard.PHP.UselessParentheses 🔧
- SlevomatCodingStandard.PHP.UselessSemicolon 🔧
- SlevomatCodingStandard.Strings.DisallowVariableParsing
- SlevomatCodingStandard.TypeHints.DeclareStrictTypes 🔧
- SlevomatCodingStandard.TypeHints.DisallowArrayTypeHintSyntax 🔧
- SlevomatCodingStandard.TypeHints.DisallowMixedTypeHint
- SlevomatCodingStandard.TypeHints.LongTypeHints 🔧
- SlevomatCodingStandard.TypeHints.NullTypeHintOnLastPosition 🔧
- SlevomatCodingStandard.TypeHints.NullableTypeForNullDefaultValue 🔧🚧
- SlevomatCodingStandard.TypeHints.ParameterTypeHint 🔧🚧
- SlevomatCodingStandard.TypeHints.ParameterTypeHintSpacing 🔧
- SlevomatCodingStandard.TypeHints.PropertyTypeHint 🔧🚧
- SlevomatCodingStandard.TypeHints.ReturnTypeHint 🔧🚧
- SlevomatCodingStandard.TypeHints.ReturnTypeHintSpacing 🔧
- SlevomatCodingStandard.TypeHints.UnionTypeHintFormat 🔧
- SlevomatCodingStandard.TypeHints.UselessConstantTypeHint 🔧
- SlevomatCodingStandard.Variables.DisallowVariableVariable
- SlevomatCodingStandard.Variables.DisallowSuperGlobalVariable
- SlevomatCodingStandard.Variables.DuplicateAssignmentToVariable
- SlevomatCodingStandard.Variables.UnusedVariable
- SlevomatCodingStandard.Variables.UselessVariable 🔧
- SlevomatCodingStandard.Whitespaces.DuplicateSpaces 🔧
安装
推荐安装Slevomat编码标准的最佳方式是通过Composer。
{ "require-dev": { "slevomat/coding-standard": "~8.0" } }
还推荐安装php-parallel-lint/php-parallel-lint,该工具用于检查源代码中的语法错误。Sniffs假定处理后的代码在语法上是有效的(没有解析错误),否则它们可能会出现意外的行为。建议在运行PHP_CodeSniffer
之前运行PHP-Parallel-Lint
,并在PHP-Parallel-Lint
失败时提前退出构建过程。
如何运行 sniffs
您可以选择两种方式之一,仅从标准中选择部分Sniffs在您的代码库中运行
选择要运行的 sniffs
推荐的方式是通过仅引用所选的Sniffs来编写自己的ruleset.xml。这是一个示例ruleset.xml
<?xml version="1.0"?> <ruleset name="AcmeProject"> <config name="installed_paths" value="../../slevomat/coding-standard"/><!-- relative path from PHPCS source location --> <rule ref="SlevomatCodingStandard.Arrays.TrailingArrayComma"/> <!-- other sniffs to include --> </ruleset>
然后以通常的方式运行phpcs
可执行文件
vendor/bin/phpcs --standard=ruleset.xml --extensions=php --tab-width=4 -sp src tests
排除您不想运行的 sniffs
您还可以在项目的ruleset.xml
中提及Slevomat编码标准,并仅排除一些Sniffs
<?xml version="1.0"?> <ruleset name="AcmeProject"> <rule ref="vendor/slevomat/coding-standard/SlevomatCodingStandard/ruleset.xml"><!-- relative path to your ruleset.xml --> <!-- sniffs to exclude --> </rule> </ruleset>
然而,这不是推荐使用Slevomat编码标准的方式,因为当您在标准的次要版本之间移动时(如果您在composer.json
中使用^
或~
版本约束),您的构建可能会中断。我们定期在次要版本中添加新的Sniffs,这意味着您的代码很可能不符合新次要版本的包。
自动修复错误
带有 🔧 符号的此标准中的Sniffs支持自动修复编码标准违规。要自动修复代码,请运行phpcbf
而不是phpcs
vendor/bin/phpcbf --standard=ruleset.xml --extensions=php --tab-width=4 -sp src tests
请始终记得在执行自动修复之前备份您的代码,并用您自己的眼睛检查结果,因为自动修复器有时会产生不受欢迎的结果。
在本地抑制 sniffs
带有 🚧 符号的此标准中的所选Sniffs可以通过注解来抑制特定的代码片段。考虑以下示例
/** * @param int $max */ public function createProgressBar($max = 0): ProgressBar { }
参数$max
可以有一个本地的int
标量类型提示。但由于父类中的方法没有这个类型提示,所以这个方法也不能有。PHP_CodeSniffer显示以下错误
----------------------------------------------------------------------
FOUND 1 ERROR AFFECTING 1 LINE
----------------------------------------------------------------------
67 | ERROR | [x] Method ErrorsConsoleStyle::createProgressBar()
| | does not have native type hint for its parameter $max
| | but it should be possible to add it based on @param
| | annotation "int".
| | (SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint)
如果我们想抑制这个错误而不是修复它,我们可以使用错误代码(SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
)并使用它来与一个@phpcsSuppress
注解一起使用,如下所示
/** * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint * @param int $max */ public function createProgressBar($max = 0): ProgressBar { }
贡献
为了使此存储库在您的机器上工作,克隆它并在存储库的根目录中运行这两个命令
composer install
bin/phing
编写了一些代码并编辑或添加单元测试后,再次运行phing以检查一切正常
bin/phing
我们始终期待您的错误报告、功能请求和pull请求。谢谢。
行为准则
本项目遵循贡献者行为准则。通过参与本项目及其社区,您应遵守此准则。