Slevomat Coding Standard
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
- 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.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
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
/**
* @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.