- Checks for missing typehints in case they can be declared natively.
- Reports useless
@varannotation (or whole documentation comment) because the type of constant is always clear.
Sniff provides the following settings:
enableNativeTypeHint: enforces native typehint. It's on by default if you're on PHP 8.3+fixableNativeTypeHint: (default:yes) allows fixing native type hints. Usenoto disable fixing, orprivateto fix only private constants (safer for inheritance/interface compatibility).
Enforces having declare(strict_types = 1) at the top of each PHP file. Allows configuring how many newlines should be between the <?php opening tag and the declare statement.
Sniff provides the following settings:
declareOnFirstLine: requiresdeclareon the first line right after<?phplinesCountBeforeDeclare: allows to set 0 to N lines to be betweendeclareand previous statement. This option is ignored whendeclareOnFirstLineis enabled.linesCountAfterDeclare: allows to set 0 to N lines to be betweendeclareand next statementspacesCountAroundEqualsSign: allows to set number of required spaces around the=operator
Disallows usage of array type hint syntax (e.g. int[], bool[][]) in phpDocs in favour of generic type hint syntax (eg. array<int>, array<array<bool>>).
Sniff provides the following settings:
traversableTypeHints: helps fixer detect traversable type hints so\Traversable|int[]can be converted to\Traversable<int>.
Disallows usage of "mixed" type hint in phpDocs.
Checks format of DNF 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.withSpacesAroundOperators:yesrequires spaces around|and&,norequires no space around|and&. None is set by default so both are enabled.withSpacesInsideParentheses:yesrequires spaces inside parentheses,norequires no spaces inside parentheses. None is set by default so both are enabled.shortNullable:yesrequires usage of?for nullable type hint,nodisallows it. None is set by default so both are enabled.nullPosition:firstrequiresnullon first position in the type hint,lastrequires last position. None is set by default sonullcan be everywhere.
Enforces using shorthand scalar typehint variants in phpDocs: int instead of integer and bool instead of boolean. This is for consistency with native scalar typehints which also allow shorthand variants only.
Enforces null type hint on last position in annotations.
Checks whether the nullablity ? symbol is present before each nullable and optional parameter (which are marked as = null):
function foo(
int $foo = null, // ? missing
?int $bar = null // correct
) {
}- 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
@paramannotations. 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 withphpcbf. - Forces to specify what's in traversable types like
array,iterableand\Traversable.
Sniff provides the following settings:
enableObjectTypeHint: enforces to transform@param objectinto nativeobjecttypehint. It's on by default if you're on PHP 7.2+enableMixedTypeHint: enforces to transform@param mixedinto nativemixedtypehint. It's on by default if you're on PHP 8.0+enableUnionTypeHint: enforces to transform@param string|intinto nativestring|inttypehint. It's on by default if you're on PHP 8.0+enableIntersectionTypeHint: enforces to transform@param Foo&Barinto nativeFoo&Bartypehint. It's on by default if you're on PHP 8.1+enableStandaloneNullTrueFalseTypeHints: enforces to transform@param true,@param falseor@param nullinto native typehints. It's on by default if you're on PHP 8.2+traversableTypeHints: enforces which typehints must have specified contained type. E.g. if you set this to\Doctrine\Common\Collections\Collection, then\Doctrine\Common\Collections\Collectionmust 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 #[Override] attribute or @phpcsSuppress SlevomatCodingStandard.TypeHints.ParameterTypeHint.MissingNativeTypeHint annotation to the method to have this sniff skip it.
- Checks that there's a single space between a typehint and a parameter name:
Foo $foo - Checks that there's no whitespace between a nullability symbol and a typehint:
?Foo
- 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
@varannotations. 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 withphpcbf. - Forces to specify what's in traversable types like
array,iterableand\Traversable.
Sniff provides the following settings:
enableNativeTypeHint: enforces to transform@var intinto nativeinttypehint. It's on by default if you're on PHP 7.4+enableMixedTypeHint: enforces to transform@var mixedinto nativemixedtypehint. It's on by default if you're on PHP 8.0+. It can be enabled only whenenableNativeTypeHintis enabled too.enableUnionTypeHint: enforces to transform@var string|intinto nativestring|inttypehint. It's on by default if you're on PHP 8.0+. It can be enabled only whenenableNativeTypeHintis enabled too.enableIntersectionTypeHint: enforces to transform@var Foo&Barinto nativeFoo&Bartypehint. It's on by default if you're on PHP 8.1+. It can be enabled only whenenableNativeTypeHintis enabled too.enableStandaloneNullTrueFalseTypeHints: enforces to transform@var true,@var falseor@var nullinto native typehints. It's on by default if you're on PHP 8.2+. It can be enabled only whenenableNativeTypeHintis 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\Collectionmust 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 #[Override] attribute if you are using PHP 8.5+ or add @phpcsSuppress SlevomatCodingStandard.TypeHints.PropertyTypeHint.MissingNativeTypeHint annotation to the property to have this sniff skip it.
- 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
@returnannotations. 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 withphpcbf. - Forces to specify what's in traversable types like
array,iterableand\Traversable.
Sniff provides the following settings:
enableObjectTypeHint: enforces to transform@return objectinto nativeobjecttypehint. It's on by default if you're on PHP 7.2+enableStaticTypeHint: enforces to transform@return staticinto nativestatictypehint. It's on by default if you're on PHP 8.0+enableMixedTypeHint: enforces to transform@return mixedinto nativemixedtypehint. It's on by default if you're on PHP 8.0+enableUnionTypeHint: enforces to transform@return string|intinto nativestring|inttypehint. It's on by default if you're on PHP 8.0+.enableIntersectionTypeHint: enforces to transform@return Foo&Barinto nativeFoo&Bartypehint. It's on by default if you're on PHP 8.1+.enableNeverTypeHint: enforces to transform@return neverinto nativenevertypehint. It's on by default if you're on PHP 8.1+.enableStandaloneNullTrueFalseTypeHints: enforces to transform@return true,@return falseor@return nullinto native typehints. It's on by default if you're on PHP 8.2+.traversableTypeHints: enforces which typehints must have specified contained type. E.g. if you set this to\Doctrine\Common\Collections\Collection, then\Doctrine\Common\Collections\Collectionmust always be supplied with the contained type:\Doctrine\Common\Collections\Collection|Foo[].
You can add @phpcsSuppress SlevomatCodingStandard.TypeHints.ReturnTypeHint.MissingNativeTypeHint annotation to the method to skip the check.
Enforces consistent formatting of return typehints, like this:
function foo(): ?intSniff provides the following settings:
spacesCountBeforeColon: the number of spaces expected between closing brace and colon.
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:yesrequires spaces around|,norequires no space around|. None is set by default so both are enabled.shortNullable:yesrequires usage of?for nullable type hint,nodisallows it. None is set by default so both are enabled.nullPosition:firstrequiresnullon first position in the type hint,lastrequires last position. None is set by default sonullcan be everywhere.
Reports useless @var annotation (or whole documentation comment) for constants because the type of constant is always clear.