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
π§ = 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 π§
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.
You can choose one of two ways to run only selected sniffs from the standard on your codebase:
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
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.
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.
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
{
}
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.
This project adheres to a Contributor Code of Conduct. By participating in this project and its community, you are expected to uphold this code.