Skip to content

Commit e4df0d6

Browse files
committed
PEAR/ValidDefaultValue: handle FQN null
This commit fixes the sniff to handle "fully qualified null" as a default value, the same as unqualified null. Includes tests.
1 parent b33c2df commit e4df0d6

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

src/Standards/PEAR/Sniffs/Functions/ValidDefaultValueSniff.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,14 @@ public function process(File $phpcsFile, $stackPtr)
5454
}
5555

5656
if (array_key_exists('default', $param) === true) {
57-
$defaultFound = true;
57+
$defaultFound = true;
58+
$defaultValueLc = strtolower($param['default']);
5859
// Check if the arg is type hinted and using NULL for the default.
5960
// This does not make the argument optional - it just allows NULL
6061
// to be passed in.
61-
if ($param['type_hint'] !== '' && strtolower($param['default']) === 'null') {
62+
if ($param['type_hint'] !== ''
63+
&& ($defaultValueLc === 'null' || $defaultValueLc === '\null')
64+
) {
6265
$defaultFound = false;
6366
}
6467

src/Standards/PEAR/Tests/Functions/ValidDefaultValueUnitTest.1.inc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,3 +114,7 @@ class ConstructorPropertyPromotionMixedWithNormalParams {
114114
mixed $requiredParam,
115115
) {}
116116
}
117+
118+
// Safeguard correct handling of FQN null as default value.
119+
function foo(Foo $foo = \null, $bar) {}
120+
function foo(Foo $foo = \null, Foz $foz = \NULL, $bar = true, $baz) {}

src/Standards/PEAR/Tests/Functions/ValidDefaultValueUnitTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public function getErrorList($testFile='')
4646
101 => 1,
4747
106 => 1,
4848
114 => 1,
49+
120 => 1,
4950
];
5051

5152
default:

0 commit comments

Comments
 (0)