PHPackages                             slevomat70/coding-standard - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. slevomat70/coding-standard

ActivePhpcodesniffer-standard

slevomat70/coding-standard
==========================

PHP 7.0 support for: Slevomat Coding Standard for PHP\_CodeSniffer complements Consistence Coding Standard by providing sniffs with additional checks.

0641PHP

Since Apr 20Pushed 4y agoCompare

[ Source](https://github.com/slevomat70/coding-standard)[ Packagist](https://packagist.org/packages/slevomat70/coding-standard)[ RSS](/packages/slevomat70-coding-standard/feed)WikiDiscussions master Synced 1w ago

READMEChangelogDependenciesVersions (1)Used By (1)

PHP 7.0 support for: Slevomat Coding Standard
=============================================

[](#php-70-support-for-slevomat-coding-standard)

[![Latest version](https://camo.githubusercontent.com/5576d18c7a3c42f87acd7c983f464d9664f8e18bdd8b41971dd60f5a363b5bbe/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f736c65766f6d61742f636f64696e672d7374616e646172642e7376673f636f6c6f72423d303037454336)](https://packagist.org/packages/slevomat/coding-standard)[![Downloads](https://camo.githubusercontent.com/dfb657ac22daae3a4740fb62a4045ce99e09f948a0696a8107bd4763d473cc6a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f736c65766f6d61742f636f64696e672d7374616e646172642e7376673f636f6c6f72423d303037454336)](https://packagist.org/packages/slevomat/coding-standard)[![Build status](https://github.com/slevomat/coding-standard/workflows/Build/badge.svg?branch=master)](https://github.com/slevomat/coding-standard/actions?query=workflow%3ABuild+branch%3Amaster)[![Code coverage](https://camo.githubusercontent.com/c264670f88d4c301a4b31e7e2e2272e4e5d8fa0de707e3c9d18e64a91f5bb014/68747470733a2f2f636f6465636f762e696f2f67682f736c65766f6d61742f636f64696e672d7374616e646172642f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/slevomat/coding-standard)[![PHPStan](https://camo.githubusercontent.com/7502cadb433f75121bf841d00afe464568e203fe32514fc5d13e152e65379229/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374796c652d6c6576656c253230372d627269676874677265656e2e7376673f266c6162656c3d7068707374616e)](https://camo.githubusercontent.com/7502cadb433f75121bf841d00afe464568e203fe32514fc5d13e152e65379229/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7374796c652d6c6576656c253230372d627269676874677265656e2e7376673f266c6162656c3d7068707374616e)

Slevomat Coding Standard for [PHP\_CodeSniffer](https://github.com/squizlabs/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
-----------------

[](#table-of-contents)

1. [Sniffs included in this standard](#sniffs-included-in-this-standard)

- [Functional - improving the safety and behaviour of code](#functional---improving-the-safety-and-behaviour-of-code)
- [Cleaning - detecting dead code](#cleaning---detecting-dead-code)
- [Formatting - rules for consistent code looks](#formatting---rules-for-consistent-code-looks)

2. [Installation](#installation)
3. [How to run the sniffs](#how-to-run-the-sniffs)

- [Choose which sniffs to run](#choose-which-sniffs-to-run)
- [Exclude sniffs you don't want to run](#exclude-sniffs-you-dont-want-to-run)

4. [Fixing errors automatically](#fixing-errors-automatically)
5. [Suppressing sniffs locally](#suppressing-sniffs-locally)
6. [Contributing](#contributing)

Sniffs included in this standard
--------------------------------

[](#sniffs-included-in-this-standard)

🔧 = [Automatic errors fixing](#fixing-errors-automatically)

🚧 = [Sniff check can be suppressed locally](#suppressing-sniffs-locally)

### Functional - improving the safety and behaviour of code

[](#functional---improving-the-safety-and-behaviour-of-code)

#### SlevomatCodingStandard.TypeHints.ParameterTypeHint 🔧🚧

[](#slevomatcodingstandardtypehintsparametertypehint-)

- Checks for missing parameter typehints in case they can be declared natively. If the phpDoc contains something that can be written as a native PHP 7.0+ typehint, this sniff reports that.
- Checks for useless `@param` annotations. If the native method declaration contains everything and the phpDoc does not add anything useful, it's reported as useless and can optionally be automatically removed with `phpcbf`.
- Forces to specify what's in traversable types like `array`, `iterable` and `\Traversable`.

Sniff provides the following settings:

- `enableObjectTypeHint`: enforces to transform `@param object` into native `object` typehint. It's on by default if you're on PHP 7.2+
- `enableMixedTypeHint`: enforces to transform `@param mixed` into native `mixed` typehint. It's on by default if you're on PHP 8.0+
- `enableUnionTypeHint`: enforces to transform `@param string|int` into native `string|int` typehint. It's on by default if you're on PHP 8.0+
- `traversableTypeHints`: enforces which typehints must have specified contained type. E. g. if you set this to `\Doctrine\Common\Collections\Collection`, then `\Doctrine\Common\Collections\Collection` must always be supplied with the contained type: `\Doctrine\Common\Collections\Collection|Foo[]`.

This sniff can cause an error if you're overriding or implementing a parent method which does not have typehints. In such cases add `@phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint` annotation to the method to have this sniff skip it.

#### SlevomatCodingStandard.TypeHints.PropertyTypeHint 🔧🚧

[](#slevomatcodingstandardtypehintspropertytypehint-)

- Checks for missing property typehints in case they can be declared natively. If the phpDoc contains something that can be written as a native PHP 7.4+ typehint, this sniff reports that.
- Checks for useless `@var` annotations. If the native method declaration contains everything and the phpDoc does not add anything useful, it's reported as useless and can optionally be automatically removed with `phpcbf`.
- Forces to specify what's in traversable types like `array`, `iterable` and `\Traversable`.

Sniff provides the following settings:

- `enableNativeTypeHint`: enforces to transform `@var int` into native `int` typehint. It's on by default if you're on PHP 7.4+
- `enableMixedTypeHint`: enforces to transform `@var mixed` into native `mixed` typehint. It's on by default if you're on PHP 8.0+. It can be enabled only when `enableNativeTypeHint` is enabled too.
- `enableUnionTypeHint`: enforces to transform `@var string|int` into native `string|int` typehint. It's on by default if you're on PHP 8.0+. It can be enabled only when `enableNativeTypeHint` is enabled too.
- `traversableTypeHints`: enforces which typehints must have specified contained type. E. g. if you set this to `\Doctrine\Common\Collections\Collection`, then `\Doctrine\Common\Collections\Collection` must always be supplied with the contained type: `\Doctrine\Common\Collections\Collection|Foo[]`.

This sniff can cause an error if you're overriding parent property which does not have typehints. In such cases add `@phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint` annotation to the property to have this sniff skip it.

#### SlevomatCodingStandard.TypeHints.ReturnTypeHint 🔧🚧

[](#slevomatcodingstandardtypehintsreturntypehint-)

- Checks for missing return typehints in case they can be declared natively. If the phpDoc contains something that can be written as a native PHP 7.0+ typehint, this sniff reports that.
- Checks for useless `@return` annotations. If the native method declaration contains everything and the phpDoc does not add anything useful, it's reported as useless and can optionally be automatically removed with `phpcbf`.
- Forces to specify what's in traversable types like `array`, `iterable` and `\Traversable`.

Sniff provides the following settings:

- `enableObjectTypeHint`: enforces to transform `@return object` into native `object` typehint. It's on by default if you're on PHP 7.2+
- `enableStaticTypeHint`: enforces to transform `@return static` into native `static` typehint. It's on by default if you're on PHP 8.0+
- `enableMixedTypeHint`: enforces to transform `@return mixed` into native `mixed` typehint. It's on by default if you're on PHP 8.0+
- `enableUnionTypeHint`: enforces to transform `@return string|int` into native `string|int` typehint. It's on by default if you're on PHP 8.0+.
- `traversableTypeHints`: enforces which typehints must have specified contained type. E. g. if you set this to `\Doctrine\Common\Collections\Collection`, then `\Doctrine\Common\Collections\Collection` must always be supplied with the contained type: `\Doctrine\Common\Collections\Collection|Foo[]`.

This sniff can cause an error if you're overriding or implementing a parent method which does not have typehints. In such cases add `@phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint` annotation to the method to have this sniff skip it.

#### SlevomatCodingStandard.TypeHints.UselessConstantTypeHint 🔧

[](#slevomatcodingstandardtypehintsuselessconstanttypehint-)

Reports useless `@var` annotation (or whole documentation comment) for constants because the type of constant is always clear.

#### SlevomatCodingStandard.TypeHints.UnionTypeHintFormat 🔧

[](#slevomatcodingstandardtypehintsuniontypehintformat-)

Checks format of union type hints.

Sniff provides the following settings:

- `enable`: either to enable or not this sniff. By default, it is enabled for PHP versions 8.0 or higher.
- `withSpaces`: `yes` requires spaces around `|`, `no` requires no space around `|`. None is set by default so both is enabled.
- `shortNullable`: `yes` requires usage of `?` for nullable type hint, `no` disallows it. None is set by default so both are enabled.
- `nullPosition`: `first` requires `null` on first position in the type hint, `last` requires last position. None is set by default so `null` can be everywhere.

#### SlevomatCodingStandard.Exceptions.DisallowNonCapturingCatch

[](#slevomatcodingstandardexceptionsdisallownoncapturingcatch)

This sniff forbids use of non-capturing catch introduced in PHP 8.0 [PHP RFC: non-capturing catches](https://wiki.php.net/rfc/non-capturing_catches).

#### SlevomatCodingStandard.Exceptions.ReferenceThrowableOnly 🔧🚧

[](#slevomatcodingstandardexceptionsreferencethrowableonly-)

In PHP 7.0, a [`Throwable` interface was added](https://wiki.php.net/rfc/throwable-interface) that allows catching and handling errors in more cases than `Exception` previously allowed. So, if the catch statement contained `Exception` on PHP 5.x, it means it should probably be rewritten to reference `Throwable` on PHP 7.x. This sniff enforces that.

#### SlevomatCodingStandard.Exceptions.RequireNonCapturingCatch 🔧

[](#slevomatcodingstandardexceptionsrequirenoncapturingcatch-)

Sniff provides the following settings:

- `enable`: either to enable or not this sniff. By default, it is enabled for PHP versions 8.0 or higher.

It requires non-capturing catch when the variable with exception is not used.

#### SlevomatCodingStandard.TypeHints.DeclareStrictTypes 🔧

[](#slevomatcodingstandardtypehintsdeclarestricttypes-)

Enforces having `declare(strict_types = 1)` at the top of each PHP file. Allows configuring how many newlines should be between the `
