RameshT 33e22a421c | ||
---|---|---|
.. | ||
SlevomatCodingStandard | ||
doc | ||
.editorconfig | ||
.typos.toml | ||
CODE_OF_CONDUCT.md | ||
LICENSE.md | ||
README.md | ||
autoload-bootstrap.php | ||
composer.json |
README.md
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
🚧 = 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.