Componette

Componette

slevomat

slevomat / coding-standard 8.6.4

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.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
  • 8.1.0 8.1.0

    ๐Ÿ”ง Improvements

    • SlevomatCodingStandard.Classes.PropertyDeclaration: New option checkPromoted to enable check of promoted properties
    • SlevomatCodingStandard.Classes.PropertyDeclaration: New option enableMultipleSpacesBetweenModifiersCheck to enable check of spaces between property modifiers
    • SlevomatCodingStandard.Classes.PropertyDeclaration: Improved error messages
    • SlevomatCodingStandard.Classes.ForbiddenPublicProperty: New option checkPromoted to enable check of promoted properties

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.TypeHints.PropertyTypeHint: Fix inconsistent enableIntersectionTypeHint (thanks to @schlndh)
  • 8.0.1 8.0.1

    ๐Ÿ› Fixes

    • Group use statements are ignored - we don't support them
    • SlevomatCodingStandard.PHP.UselessParentheses: Fixed false positive
    • SlevomatCodingStandard.TypeHints.ParameterTypeHint: Fixed internal error (thanks to @schlndh)
  • 8.0.0 8.0.0

    ๐Ÿ”ง Improvements

    • Support for intersection types
    • Support for readonly properties
    • Support for enums
    • Support for never type hint
    • Support for more unofficial type hints
    • SlevomatCodingStandard.Classes.PropertyDeclaration: Checks also order of modifiers
    • SlevomatCodingStandard.Classes.ClassStructure: Support for enum cases and readonly properties

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.Classes.PropertyDeclaration: Fixed missing support for only static property
    • SlevomatCodingStandard.TypeHints.PropertyTypeHint: Fixed missing support for only static property
    • SlevomatCodingStandard.Commenting.EmptyComment: Fixed internal error
    • SlevomatCodingStandard.Classes.ForbiddenPublicProperty: Fixed internal error
    • SlevomatCodingStandard.Namespaces.FullyQualifiedClassNameInAnnotation: Fixed false positives for @psalm-import-type in @psalm-var
    • SlevomatCodingStandard.PHP.RequireExplicitAssertion: Ignore unsupported unofficial type hints

    โš ๏ธBC breaks

    • SlevomatCodingStandard.TypeHints.PropertyTypeHintSpacing renamed to SlevomatCodingStandard.Classes.PropertyDeclaration
    • SlevomatCodingStandard.Classes.ClassStructure: Removed option enableFinalMethods
    • Removed error SlevomatCodingStandard.Namespaces.UnusedUses.MismatchingCaseSensitivity
  • 7.2.1 7.2.1

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly: Fixed false positives for colliding functions and constants
    • SlevomatCodingStandard.Commenting.DisallowCommentAfterCode: Don't break multi-line string during auto-fix (thanks to @schlndh)
    • Fixed detection of function object
  • 7.2.0 7.2.0

    ๐Ÿ†• New sniffs

    • Added SlevomatCodingStandard.Functions.DisallowTrailingCommaInClosureUse
    • Added SlevomatCodingStandard.Functions.RequireTrailingCommaInClosureUse

    ๐Ÿ”ง Improvements

    • Support for offset access type syntax in annotations
    • Support for @phpstan-assert/@psalm-assert annotations
    • SlevomatCodingStandard.Whitespaces.DuplicateSpacesSniff: New option ignoreSpacesInMatch
    • SlevomatCodingStandard.Functions.DisallowTrailingCommaInCall: New option onlySingleLine
    • SlevomatCodingStandard.Functions.DisallowTrailingCommaInDeclaration: New option onlySingleLine

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.TypeHints.ReturnTypeHint: Fixed internal error
    • SlevomatCodingStandard.ControlStructures.UselessIfConditionWithReturn: Fixed internal error
    • SlevomatCodingStandard.Functions.RequireSingleLineCall: Fixed false positive
    • SlevomatCodingStandard.Variables.UnusedVariable: Resolve false positives with increment/decrement operators (thanks to @aadmathijssen)
    • SlevomatCodingStandard.Variables.UnusedVariable: allow increment/decrement operators after yield (thanks to @aadmathijssen)
  • 7.1.0 7.1.0

    โš ๏ธ Removed support for PHP 7.1

    ๐Ÿ†• New sniffs

    • Added SlevomatCodingStandard.Classes.RequireAbstractOrFinal (thanks to @roslov)
    • Added SlevomatCodingStandard.Exceptions.DisallowNonCapturingCatch (thanks to @olsavmic)

    ๐Ÿ”ง Improvements

    • Support for conditional types in annotations
    • SlevomatCodingStandard.Numbers.RequireNumericLiteralSeparator: Added option ignoreOctalNumbers
    • SlevomatCodingStandard.Functions.FunctionLength: New options includeComments and includeWhitespace (thanks to @bkdotcom)
  • 7.0.20 7.0.20

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.TypeHints.ReturnTypeHint: Improved - Type alias should not be reported as "missing traversable type hint specification"
    • SlevomatCodingStandard.TypeHints.ParameterTypeHint: Improved - Type alias should not be reported as "missing traversable type hint specification"
    • SlevomatCodingStandard.TypeHints.PropertyTypeHint: Improved - Type alias should not be reported as "missing traversable type hint specification"
    • SlevomatCodingStandard.TypeHints.ReturnTypeHint: It was not possible to suppress some errors
    • SlevomatCodingStandard.Classes.ParentCallSpacing: Fixed false positive
  • 7.0.19 7.0.19

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.TypeHints.PropertyTypeHint: Type alias should not be reported as "missing traversable type hint specification"
    • SlevomatCodingStandard.TypeHints.ParameterTypeHint: Type alias should not be reported as "missing traversable type hint specification"
    • SlevomatCodingStandard.TypeHints.ReturnTypeHint: Type alias should not be reported as "missing traversable type hint specification"
    • SlevomatCodingStandard.ControlStructures.RequireYodaComparison: Fixed false positives with match
    • SlevomatCodingStandard.ControlStructures.DisallowYodaComparison: Fixed false positives with match
    • SlevomatCodingStandard.Commenting.DocCommentSpacing: Fixed internal error
    • SlevomatCodingStandard.Variables.UnusedVariable: Fixed false positive
    • SlevomatCodingStandard.Variables.UnusedVariable: Fixed missing report when option ignoreUnusedValuesWhenOnlyKeysAreUsedInForeach is enabled
    • SlevomatCodingStandard.Functions.RequireTrailingCommaInCall: Fixed false positive
    • ReferencedNameHelper: Fixed searching of class references in heredoc
    • Ignore invalid @template and @type annotations
  • 7.0.18 7.0.18

    ๐Ÿ› Fixes

    • Improved AnnotationTypeHelper::export() for callable in array
  • 7.0.17 7.0.17

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.Namespaces.UnusedUses: Improved detection of relative name in generic annotation
    • SlevomatCodingStandard.Classes.ClassConstantVisibility: Added support for final constants
    • SlevomatCodingStandard.Functions.RequireTrailingCommaInCall: Skip instances on same line as closing parenthesis (thanks to @donatj)
  • 7.0.16 7.0.16

    ๐Ÿ› Fixes

    • SlevomatCodingStandard.Classes.ModernClassNameReference: Don't flag method declarations (thanks to @jrfnl)
    • SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration: Fixed false positive
    • SlevomatCodingStandard.ControlStructures.NewWithParentheses: Fixed false positive
    • Appended logic in ReferencedNameHelper for find names of class inside double quotes (thanks to @AIlkiv)
  • 7.0.15 7.0.15

    ๐Ÿ› Fixes

    • UseSpacingSniff: Fixed false positive
    • TypeCastSniff: Fixed internal error
    • RequireConstructorPropertyPromotionSniff: Fixed false positives
    • ClassStructureSniff: Add missing magic methods (thanks to @jrfnl)
    • Updated phpstan/phpdoc-parser (thanks to @alexndlm)
  • 7.0.14 7.0.14

    ๐Ÿ› Fixes

    • InlineDocCommentDeclarationSniff: Don't report missing assigment for $this
    • ParameterTypeHintSniff, ReturnTypeHintSniff, PropertyTypeHintSniff: Short nullable is now reported as useless annotation too
    • Don't try to find references in double quoted string - it's difficult to parse it with PHPCS
  • 7.0.13 7.0.13

    ๐Ÿ› Fixes

    • RequireConstructorPropertyPromotionSniff: Fixed false positive
    • UnusedUsesSniff: Fixed false positive
  • 7.0.12 7.0.12

    ๐Ÿ› Fixes

    • RequireConstructorPropertyPromotionSniff: Fixed internal error
  • 7.0.10 7.0.10

    ๐Ÿ› Fixes

    • RequireNonCapturingCatchSniff: Fixed false positive
    • ReturnTypeHintSniff: It knows unofficial void type hints
    • ReturnTypeHintSniff: Method returning value with void annotation is reported only
    • ParentCallSpacingSniff: Fixed false positive
    • Fixed array shapes parsing
  • 7.0.9 7.0.9

    ๐Ÿ› Fixes

    • UselessInheritDocCommentSniff: Fixed false positive
    • RequireNullSafeObjectOperatorSniff: Fixed false positives
    • UnusedVariableSniff: Fixed false positive
  • 7.0.8 7.0.8

    ๐Ÿ› Fixes

    • UnusedUsesSniff: Fixed false positive
    • InlineDocCommentDeclarationSniff: Fixed false positive
    • Fix: resolve false positive in FunctionHelper::isMethod for functions defined within a class method (thanks to @aadmathijssen)
  • 7.0.7 7.0.7

    ๐Ÿ› Fixes

    • ReturnTypeHintSniff: Fixed fixer for void in union type hint
    • UnusedUsesSniff: Fixed false positive
    • FunctionLengthSniff: Moved to Functions namespace, old sniff still exists and is marked as deprecated (thanks to @50bhan)
    • FunctionLengthSniff: Resolve off-by-one error (thanks to @aadmathijssen)
bar-chart-fill

Statistics

download-cloud-fill
34946853
star-fill
1177
bug-fill
56
flashlight-fill
5d
price-tag-2-line

Badges

guide-fill

Dependencies

Componette Componette felix@nette.org