slevomat / coding-standard
Slevomat Coding Standard for PHP_CodeSniffer complements Consistence Coding Standard by providing sniffs with additional checks.
Installs: 80 916 374
Dependents: 1 524
Suggesters: 4
Security: 0
Stars: 1 394
Watchers: 28
Forks: 175
Open Issues: 106
Type:phpcodesniffer-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-build
- dev-dev-slevomat
- dev-slevomat
- dev-phpdoc-parser-2
- dev-add-typos
- dev-phpstan
This package is auto-updated.
Last update: 2024-11-08 15:15:18 UTC
README
Slevomat Coding Standard for PHP_CodeSniffer provides sniffs that fall into three categories:
- Functional - improving the safety and behaviour of code
- Cleaning - detecting dead code
- Formatting - rules for consistent code looks
Table of contents
Alphabetical list of sniffs
🔧 = Automatic errors fixing
🚧 = Sniff check can be suppressed locally
- 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 🔧
Installation
The recommended way to install Slevomat Coding Standard is through Composer.
{ "require-dev": { "slevomat/coding-standard": "~8.0" } }
It's also recommended to install php-parallel-lint/php-parallel-lint which checks source code for syntax errors. Sniffs count on the processed code to be syntactically valid (no parse errors), otherwise they can behave unexpectedly. It is advised to run PHP-Parallel-Lint
in your build tool before running PHP_CodeSniffer
and exiting the build process early if PHP-Parallel-Lint
fails.
How to run the sniffs
You can choose one of two ways to run only selected sniffs from the standard on your codebase:
Choose which sniffs to run
The recommended way is to write your own ruleset.xml by referencing only the selected sniffs. This is a sample 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>
Then run the phpcs
executable the usual way:
vendor/bin/phpcs --standard=ruleset.xml --extensions=php --tab-width=4 -sp src tests
Exclude sniffs you don't want to run
You can also mention Slevomat Coding Standard in your project's ruleset.xml
and exclude only some 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>
However it is not a recommended way to use Slevomat Coding Standard, because your build can break when moving between minor versions of the standard (which can happen if you use ^
or ~
version constraint in composer.json
). We regularly add new sniffs even in minor versions meaning your code won't most likely comply with new minor versions of the package.
Fixing errors automatically
Sniffs in this standard marked by the 🔧 symbol support automatic fixing of coding standard violations. To fix your code automatically, run phpcbf instead of phpcs:
vendor/bin/phpcbf --standard=ruleset.xml --extensions=php --tab-width=4 -sp src tests
Always remember to back up your code before performing automatic fixes and check the results with your own eyes as the automatic fixer can sometimes produce unwanted results.
Suppressing sniffs locally
Selected sniffs in this standard marked by the 🚧 symbol can be suppressed for a specific piece of code using an annotation. Consider the following example:
/** * @param int $max */ public function createProgressBar($max = 0): ProgressBar { }
The parameter $max
could have a native int
scalar typehint. But because the method in the parent class does not have this typehint, so this one cannot have it either. PHP_CodeSniffer shows a following error:
----------------------------------------------------------------------
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)
If we want to suppress this error instead of fixing it, we can take the error code (SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint
) and use it with a @phpcsSuppress
annotation like this:
/** * @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint * @param int $max */ public function createProgressBar($max = 0): ProgressBar { }
Contributing
To make this repository work on your machine, clone it and run these two commands in the root directory of the repository:
composer install
bin/phing
After writing some code and editing or adding unit tests, run phing again to check that everything is OK:
bin/phing
We are always looking forward to your bugreports, feature requests and pull requests. Thank you.
Code of Conduct
This project adheres to a Contributor Code of Conduct. By participating in this project and its community, you are expected to uphold this code.