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
: requiresdeclare
on the first line right after<?php
linesCountBeforeDeclare
: allows to set 0 to N lines to be betweendeclare
and previous statement. This option is ignored whendeclareOnFirstLine
is enabled.linesCountAfterDeclare
: allows to set 0 to N lines to be betweendeclare
and 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.
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
@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 withphpcbf
. - 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 nativeobject
typehint. It's on by default if you're on PHP 7.2+enableMixedTypeHint
: enforces to transform@param mixed
into nativemixed
typehint. It's on by default if you're on PHP 8.0+enableUnionTypeHint
: enforces to transform@param string|int
into nativestring|int
typehint. It's on by default if you're on PHP 8.0+enableIntersectionTypeHint
: enforces to transform@param Foo&Bar
into nativeFoo&Bar
typehint. It's on by default if you're on PHP 8.1+enableStandaloneNullTrueFalseTypeHints
: enforces to transform@param true
,@param false
or@param null
into 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\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.
- 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
@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 withphpcbf
. - 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 nativeint
typehint. It's on by default if you're on PHP 7.4+enableMixedTypeHint
: enforces to transform@var mixed
into nativemixed
typehint. It's on by default if you're on PHP 8.0+. It can be enabled only whenenableNativeTypeHint
is enabled too.enableUnionTypeHint
: enforces to transform@var string|int
into nativestring|int
typehint. It's on by default if you're on PHP 8.0+. It can be enabled only whenenableNativeTypeHint
is enabled too.enableIntersectionTypeHint
: enforces to transform@var Foo&Bar
into nativeFoo&Bar
typehint. It's on by default if you're on PHP 8.1+. It can be enabled only whenenableNativeTypeHint
is enabled too.enableStandaloneNullTrueFalseTypeHints
: enforces to transform@var true
,@var false
or@var null
into native typehints. It's on by default if you're on PHP 8.2+. It can be enabled only whenenableNativeTypeHint
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.
- 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 withphpcbf
. - 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 nativeobject
typehint. It's on by default if you're on PHP 7.2+enableStaticTypeHint
: enforces to transform@return static
into nativestatic
typehint. It's on by default if you're on PHP 8.0+enableMixedTypeHint
: enforces to transform@return mixed
into nativemixed
typehint. It's on by default if you're on PHP 8.0+enableUnionTypeHint
: enforces to transform@return string|int
into nativestring|int
typehint. It's on by default if you're on PHP 8.0+.enableIntersectionTypeHint
: enforces to transform@return Foo&Bar
into nativeFoo&Bar
typehint. It's on by default if you're on PHP 8.1+.enableNeverTypeHint
: enforces to transform@return never
into nativenever
typehint. It's on by default if you're on PHP 8.1+.enableStandaloneNullTrueFalseTypeHints
: enforces to transform@return true
,@return false
or@return null
into 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\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.
Enforces consistent formatting of return typehints, like this:
function foo(): ?int
Sniff 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
:yes
requires spaces around|
,no
requires no space around|
. None is set by default so both are enabled.shortNullable
:yes
requires usage of?
for nullable type hint,no
disallows it. None is set by default so both are enabled.nullPosition
:first
requiresnull
on first position in the type hint,last
requires last position. None is set by default sonull
can be everywhere.
Reports useless @var
annotation (or whole documentation comment) for constants because the type of constant is always clear.