diff --git a/West/Sniffs/PHP/NoNullSniff.php b/West/Sniffs/PHP/NoNullSniff.php index f70df90..f680b23 100644 --- a/West/Sniffs/PHP/NoNullSniff.php +++ b/West/Sniffs/PHP/NoNullSniff.php @@ -47,19 +47,6 @@ public function process(File $phpcsFile, $stackPtr) { $tokens = $phpcsFile->getTokens(); - $statementStart = $phpcsFile->findStartOfStatement($stackPtr); - $token = $tokens[$statementStart]; - - if ($token['type'] !== 'T_VARIABLE') { - // this isn't a default value - // for a function - $error = 'Use of null is forbidden'; - $phpcsFile->addError($error, $stackPtr, 'NullUsed'); - $phpcsFile->recordMetric($stackPtr, 'No null members', 'no'); - - return; - } - if (isset($tokens[$stackPtr]['nested_parenthesis']) === false) { // this isn't a default value // for a function @@ -73,8 +60,9 @@ public function process(File $phpcsFile, $stackPtr) // Check to see if this including statement is within the parenthesis // of a function. foreach ($tokens[$stackPtr]['nested_parenthesis'] as $left => $right) { + $allowedPlacement = ['T_FUNCTION', 'T_IF', 'T_ELSEIF']; if (! isset($tokens[$left]['parenthesis_owner']) === true || - $tokens[$tokens[$left]['parenthesis_owner']]['type'] !== 'T_FUNCTION') { + ! in_array($tokens[$tokens[$left]['parenthesis_owner']]['type'], $allowedPlacement)) { // this isn't a default value // for a function $error = 'Use of null is forbidden'; diff --git a/West/Tests/PHP/NoNullUnitTest.inc b/West/Tests/PHP/NoNullUnitTest.inc index 1e4aa53..8a76e5f 100644 --- a/West/Tests/PHP/NoNullUnitTest.inc +++ b/West/Tests/PHP/NoNullUnitTest.inc @@ -18,4 +18,27 @@ $x = new X(); $x->y($z); $x->y(); $x->y(null); +interface S +{ + public function f(string $level, string $message, array $context = [], \DateTimeInterface $time = null); +} + +public function g(string $level, string $message, array $context = [], \DateTimeInterface $time = null) +{ + if (null === $time) { + $time = 1; + } elseif (null === $time) { + $time = 2; + } + + $x = null; + +} +final class D +{ + public function __construct(string $message = '', \Throwable $previous = null) + { + + } +} ?> diff --git a/West/Tests/PHP/NoNullUnitTest.php b/West/Tests/PHP/NoNullUnitTest.php index c7b9d65..b19ea54 100644 --- a/West/Tests/PHP/NoNullUnitTest.php +++ b/West/Tests/PHP/NoNullUnitTest.php @@ -25,6 +25,7 @@ public function getErrorList() return [ 2 => 1, 20 => 1, + 34 => 1 ]; }