Componette

Componette

slevomat

slevomat / coding-standard 8.15.0

Slevomat Coding Standard for PHP_CodeSniffer provides many useful sniffs

download-cloud-line composer require slevomat/coding-standard

Slevomat Coding Standard

Latest version Downloads Build status Code coverage PHPStan

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

  1. Alphabetical list of sniffs
  2. Installation
  3. How to run the sniffs
  1. Fixing errors automatically
  2. Suppressing sniffs locally
  3. Contributing

Alphabetical list of sniffs

๐Ÿ”ง = Automatic errors fixing

๐Ÿšง = Sniff check can be suppressed locally

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.

  • 8.15.0 8.15.0

    ๐Ÿ”ง Improvements

    • Speedup of sniffs working with use
    • Removed for a long time deprecated FunctionLength sniff in Files namespace

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.Classes.ClassConstantVisibility: Fixed error message for typed constants
    • SlevomatCodingStandard.Namespaces.UnusedUses: Fixed false positive thanks to PHPCS upgrade
    • SlevomatCodingStandard.Namespaces.UnusedUses: Fix class detection in double-quoted strings and heredoc (thanks to @c01l)
    • SlevomatCodingStandard.Exceptions.RequireNonCapturingCatch: Fixed false positives
    • SlevomatCodingStandard.Functions.RequireTrailingCommaInCall: Fixed missing report for missing trailing comma after arrow function
    • SlevomatCodingStandard.Commenting.UselessFunctionDocComment: It should report simple array as useless
    • Fixed internal error in CommentHelper
  • 8.14.1 8.14.1

    ๐Ÿ› Fixes

  • 8.14.0 8.14.0

    ๐Ÿ†• New sniffs

    • SlevomatCodingStandard.Functions.NamedArgumentSpacing: Checks spacing in named argument (thanks to @mzk)

    ๐Ÿ”ง Improvements

    • SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses: If the file contains a group use then ignore the file completely (thanks to @jonathan1055)
    • SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameInAnnotation: New option ignoredAnnotationNames (thanks to @gemal)

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.ControlStructures.AssignmentInCondition: Prevent error during live coding (thanks to @jrfnl)
    • SlevomatCodingStandard.ControlStructures.RequireSingleLineCondition: Prevent error during live coding (thanks to @jrfnl)
    • SlevomatCodingStandard.PHP.UselessParentheses: Prevent error during live coding (thanks to @jrfnl)
    • SlevomatCodingStandard.Arrays.DisallowImplicitArrayCreation: Recognize global statements (thanks to @jrfnl)
    • SlevomatCodingStandard.Namespaces.FullyQualifiedGlobalConstants: Don't treat a group use as a constant (thanks to @asispts)
    • SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly: Fixed false positives
    • SlevomatCodingStandard.Classes.ConstantSpacing: Fixed internal error
    • SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration: Fixed false positive for final constant
    • SlevomatCodingStandard.Namespaces.UnusedUses: Ignores uses in annotations with multi lines string arguments (thanks to @mathroc)
    • NamespaceHelper::getAllNamespacesPointers(): Allow for namespace tokens used as operator (thanks to @jrfnl)
  • 8.13.4 8.13.4

    ๐Ÿ› Fixes

    • Fixed detection of {@inheritdoc}
  • 8.13.3 8.13.3

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.Commenting.DocCommentSpacing: Fixed internal error for invalid doccomment
  • 8.13.2 8.13.2

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.Commenting.UselessFunctionDocComment: Fixed false positives
    • SlevomatCodingStandard.Commenting.DocCommentSpacing: Fixed false positives
    • SlevomatCodingStandard.ControlStructures.JumpStatementsSpacing: Fix TypeError when return is on the first line of the file (thanks to @herndlm)
  • 8.13.1 8.13.1

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.Commenting.DocCommentSpacing: Fixed fixer
    • SlevomatCodingStandard.Commenting.UselessFunctionDocComment: Fixed false positive
    • Don't parse invalid doccomments
  • 8.13.0 8.13.0

    ๐Ÿ”ง Improvements

    • Refactored documentation comments parsing

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.TypeHints.ParameterTypeHint: callable is not valid type for property promotion
  • 8.12.1 8.12.1

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly: Fixed false positive
  • 8.12.0 8.12.0

    ๐Ÿ†• New sniffs

    • SlevomatCodingStandard.Arrays.ArrayAccess: Disallow whitespace between array access operator and the variable, or between array access operators (thanks to @kamil-zacek)

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.Classes.ClassStructure: Fixed fixer
    • SlevomatCodingStandard.ControlStructures.RequireNullSafeObjectOperator: Fixed false positive
    • SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly: Fixes
    • SlevomatCodingStandard.Arrays.DisallowPartiallyKeyed: Fixed false positive
  • 8.11.1 8.11.1

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.Commenting.AnnotationName: Fixed false positive
    • SlevomatCodingStandard.Strings.DisallowVariableParsing: Fixed false positive
  • 8.11.0 8.11.0

    ๐Ÿ†• New sniffs

    • SlevomatCodingStandard.Commenting.AnnotationName: Checks incorrect annotation names

    ๐Ÿ”ง Improvements

    • SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses: Don't fix when multiple uses are in one statement (thanks to @jonathan1055)
    • Support for phpstan/phpdoc-parser 1.20

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.Strings.DisallowVariableParsing: Fixed false positive
    • SlevomatCodingStandard.Arrays.DisallowPartiallyKeyed: Fixed false positive with array unpacking
  • 8.10.0 8.10.0

    ๐Ÿ†• New sniffs

    • SlevomatCodingStandard.Classes.DisallowStringExpressionPropertyFetch: Disallows string expression property fetch $object->{'foo'}
    • SlevomatCodingStandard.Strings.DisallowVariableParsing: Disallows variable parsing inside strings (thanks to @Ekimik)
    • SlevomatCodingStandard.Arrays.DisallowPartiallyKeyed: Disallows partially keyed arrays (thanks to @bkdotcom)

    ๐Ÿ”ง Improvements

    • Object shapes support
    • Improved support for arrays in array() style (thanks to @bkdotcom)
    • Support for phpstan/phpdoc-parser 1.18
  • 8.9.2 8.9.2

    ๐Ÿ”ง Improvements

    • Support for phpstan/phpdoc-parser 1.17

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.Classes.ParentCallSpacing: Fixed false positives with parent call used in match()
  • 8.9.1 8.9.1

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.ControlStructures.NewWithParentheses: Fixed false positives for anonymous class with attributes
    • SlevomatCodingStandard.Classes.RequireSelfReference: Fixed false positives for anonymous classes with extends or implements
  • 8.9.0 8.9.0

    ๐Ÿ†• New sniffs

    • SlevomatCodingStandard.Classes.EnumCaseSpacing: Checks that there is a certain number of blank lines between enum cases
    • SlevomatCodingStandard.Classes.RequireSelfReference: Requires self for local reference
    • SlevomatCodingStandard.Variables.DisallowVariableVariable: Disallows use of variable variables
    • SlevomatCodingStandard.ControlStructures.DisallowTrailingMultiLineTernaryOperator: Ternary operator has to be reformatted when the operator is not leading the line (thanks to @esserj)
    • SlevomatCodingStandard.Arrays.AlphabeticallySortedByKeys: Enforces natural sorting of array definitions by key in multi-line arrays (thanks to @bkdotcom)

    ๐Ÿ”ง Improvements

    • Support for phpstan/phpdoc-parser 1.16
    • @phpcsSuppress can be with comment
    • Support for non-falsy-string, list and non-empty-list type hints
    • SlevomatCodingStandard.Arrays.TrailingArrayComma: Support for "legacy" array() syntax (thanks to @bkdotcom)
    • SlevomatCodingStandard.Complexity.Cognitive: Deprecate maxComplexity and implement warningThreshold and errorThreshold options (thanks to @bkdotcom)
    • SlevomatCodingStandard.Attributes.AttributeAndTargetSpacing: New option allowOnSameLine (thanks to @stlrnz)
    • SlevomatCodingStandard.Classes.RequireMultiLineMethodSignature: New option minParameterCount (thanks to @janedbal)

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses: Fixer should not remove doc comments
    • SlevomatCodingStandard.ControlStructures.EarlyExit: Fixed detection of useless else
    • SlevomatCodingStandard.Variables.UselessVariable: Fixed false positive
    • SlevomatCodingStandard.PHP.OptimizedFunctionsWithoutUnpacking: Fixed false positive with first class callable
    • SlevomatCodingStandard.Classes.RequireConstructorPropertyPromotion: Fixes using prefixed var phpdoc (thanks to @franmomu)
  • 8.8.0 8.8.0

    ๐Ÿ”ง Improvements

    • Support for unsealed array shapes
    • Allow for the 1.0.0 version of the Composer PHPCS plugin (thanks to @jrfnl)

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.Classes.RequireAbstractOrFinal: Fixed false positive for readonly classes
    • Fix typo in exception message (thanks to @jslmorrison)
  • 8.7.1 8.7.1

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.TypeHints.ParameterTypeHintSpacing: Fixed false positive when parameter has attribute but no type hint
  • 8.7.0 8.7.0

    ๐Ÿ”ง Improvements

    • Support for phpstan/phpdoc-parser 1.15
    • Support for phpstan/phpdoc-parser 1.14
    • SlevomatCodingStandard.Attributes.AttributesOrder: New option orderAlphabetically
    • SlevomatCodingStandard.Attributes.AttributesOrder: Attributes could be ordered by mask (thanks to @alexndlm)

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.Attributes.AttributeAndTargetSpacing: Fixed fixer
  • 8.6.4 8.6.4

    ๐Ÿ”ง Improvements

    • Improved annotations parsing
    • SlevomatCodingStandard.Classes.ClassConstantVisibility: Added support for traits with constants
    • SlevomatCodingStandard.Classes.ConstantSpacing: Added support for traits with constants

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.Attributes.DisallowAttributesJoining: Fix for attribute with trailing comma (thanks to @javer)
  • 8.6.3 8.6.3

    ๐Ÿ› Fixes

    • Slevomat.Namespaces.ReferenceUsedNamesOnly: Fixed fixer when there's conflict with Slevomat.Namespaces.UnusedUses
    • SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameInAnnotation: Fixed false positives for int<0, max> and int<min, 100>
    • SlevomatCodingStandard.Attributes.AttributeAndTargetSpacing: Fixed false positive for comment after attribute
  • 8.6.2 8.6.2

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameInAnnotation: Fixed false positive with self::CONSTANT
  • 8.6.1 8.6.1

    ๐Ÿ”ง Improvements

    • Support of phpstan/phpdoc-parser 1.12.0

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameInAnnotation: Fixed false positives for global constants
    • SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration: Fixed false positives for constants
  • 8.6.0 8.6.0

    ๐Ÿ†• New sniffs

    • Added SlevomatCodingStandard.Attributes.AttributesOrder
    • Added SlevomatCodingStandard.Attributes.AttributeAndTargetSpacing
    • Added SlevomatCodingStandard.Attributes.DisallowMultipleAttributesPerLine
    • Added SlevomatCodingStandard.Attributes.DisallowAttributesJoining (thanks to @michnovka)
    • Added SlevomatCodingStandard.Attributes.RequireAttributeAfterDocComment

    ๐Ÿ”ง Improvements

    • Support of phpstan/phpdoc-parser 1.11.0
    • Support for @phpstan-self-out/@phpstan-this-out
    • Support for @param-out annotation`
    • Support for @template with default value
    • Add dev Composer keyword (thanks to @GaryJones)

    ๐Ÿ› Fixes

    • Improved detection of references in double quotes strings
  • 8.5.2 8.5.2

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.TypeHints.PropertyTypeHint: Fixed false positives when enableUnionTypeHint is disabled and enableIntersectionTypeHint is enabled
    • SlevomatCodingStandard.TypeHints.ParameterTypeHint: Fixed false positives when enableUnionTypeHint is disabled and enableIntersectionTypeHint is enabled
    • SlevomatCodingStandard.TypeHints.ReturnTypeHint: Fixed false positives when enableUnionTypeHint is disabled and enableIntersectionTypeHint is enabled
  • 8.5.1 8.5.1

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.PHP.RequireExplicitAssertion: Fixed broken fixer when enableAdvancedStringTypes is enabled
  • 8.5.0 8.5.0

    ๐Ÿ”ง Improvements

    • PHP 8.2: Support for standalone null, true and false type hints
    • SlevomatCodingStandard.PHP.RequireExplicitAssertion: Improved support for native simple types
    • SlevomatCodingStandard.PHP.RequireExplicitAssertion: New option enableIntegerRanges
    • SlevomatCodingStandard.PHP.RequireExplicitAssertion: New option enableAdvancedStringTypes
    • Support of phpstan/phpdoc-parser 1.8.0

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.Classes.PropertyDeclaration: Fixed false positive
    • SlevomatCodingStandard.PHP.RequireExplicitAssertion: Fixed false positive
    • SlevomatCodingStandard.ControlStructures.DisallowYodaComparison/RequireYodaComparison: Fixed support for condition in arrow function
    • SlevomatCodingStandard.Classes.DisallowMultiPropertyDefinition: Fixed false positives for old array definition style
    • SlevomatCodingStandard.Variables.UselessVariable: Fixed false positives
  • 8.4.0 8.4.0

    ๐Ÿ”ง Improvements

    • Support of phpstan/phpdoc-parser 1.7.0

    ๐Ÿ› Fixes

    • Fixed detection of some PHP 8.1 types
    • SlevomatCodingStandard.PHP.RequireNowdoc: Accepts escaped sequences (thanks to @dg)
    • SlevomatCodingStandard.Functions.RequireSingleLineCall: Skip calls with multi-line double-quoted string (thanks to @schlndh)
    • SlevomatCodingStandard.PHP.UselessParentheses: Fixed false positive with xor
    • SlevomatCodingStandard.Operators.RequireCombinedAssignmentOperator: Try to ignore string offsets
  • 8.3.0 8.3.0

    ๐Ÿ†• New sniffs

    • Added SlevomatCodingStandard.Complexity.Cognitive (thanks to @bkdotcom)
    • Added SlevomatCodingStandard.Files.FileLength (thanks to @bkdotcom)
    • Added SlevomatCodingStandard.Classes.ClassLength (thanks to @bkdotcom)

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.PHP.RequireExplicitAssertion: Do not throw away static type (thanks to @simPod)
  • 8.2.0 8.2.0

    ๐Ÿ†• New sniffs

    • Added SlevomatCodingStandard.Classes.BackedEnumTypeSpacing

    ๐Ÿ”ง Improvements

    • SlevomatCodingStandard.TypeHints.ParameterTypeHint: MissingTraversableTypeHintSpecification is not reported when promoted property has @var annotation
bar-chart-fill

Statistics

download-cloud-fill
64595550
star-fill
1351
bug-fill
83
flashlight-fill
41d
price-tag-2-line

Badges

guide-fill

Dependencies

Componette Componette felix@nette.org