Skip to content

Commit e083ac0

Browse files
committed
Fixed "TypeHintDeclaration with disabled enableNullableTypeHints does not work with optional parameters"
1 parent 0d4fdd9 commit e083ac0

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

SlevomatCodingStandard/Sniffs/TypeHints/TypeHintDeclarationSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ private function checkParametersTypeHints(\PHP_CodeSniffer_File $phpcsFile, int
246246
$phpcsFile->fixer->addContentBefore($beforeParameterPointer, sprintf('%s%s ', ($nullableParameterTypeHint ? '?' : ''), $parameterTypeHint));
247247
} else {
248248
$phpcsFile->fixer->addContentBefore($beforeParameterPointer, sprintf('%s ', $parameterTypeHint));
249-
if ($nullableParameterTypeHint) {
249+
if ($nullableParameterTypeHint && $tokens[TokenHelper::findNextEffective($phpcsFile, $parameterPointer + 1)]['code'] !== T_EQUAL) {
250250
$phpcsFile->fixer->addContent($parameterPointer, ' = null');
251251
}
252252
}

tests/Sniffs/TypeHints/data/fixableParameterTypeHintsWithDisabledNullableTypeHints.fixed.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ function oneWithoutTypeHintAndWithAnnotationWithoutParameterName(string $a, bool
5656
{
5757
}
5858

59+
/**
60+
* @param string|null $a
61+
*/
62+
function optionalWithoutTypeHint(string $a = null)
63+
{
64+
65+
}
66+
5967
class Foo
6068
{
6169

@@ -111,4 +119,12 @@ public function oneWithoutTypeHintAndWithAnnotationWithoutParameterName(string $
111119
{
112120
}
113121

122+
/**
123+
* @param string|null $a
124+
*/
125+
public function optionalWithoutTypeHint(string $a = null)
126+
{
127+
128+
}
129+
114130
}

tests/Sniffs/TypeHints/data/fixableParameterTypeHintsWithDisabledNullableTypeHints.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ function oneWithoutTypeHintAndWithAnnotationWithoutParameterName(string $a, $b,
5656
{
5757
}
5858

59+
/**
60+
* @param string|null $a
61+
*/
62+
function optionalWithoutTypeHint($a = null)
63+
{
64+
65+
}
66+
5967
class Foo
6068
{
6169

@@ -111,4 +119,12 @@ public function oneWithoutTypeHintAndWithAnnotationWithoutParameterName(string $
111119
{
112120
}
113121

122+
/**
123+
* @param string|null $a
124+
*/
125+
public function optionalWithoutTypeHint($a = null)
126+
{
127+
128+
}
129+
114130
}

0 commit comments

Comments
 (0)