Skip to content

Commit

Permalink
fix no null sniff
Browse files Browse the repository at this point in the history
  • Loading branch information
christopher-evans committed Aug 10, 2018
1 parent c8093e4 commit 0b3ed9a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 14 deletions.
16 changes: 2 additions & 14 deletions West/Sniffs/PHP/NoNullSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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';
Expand Down
23 changes: 23 additions & 0 deletions West/Tests/PHP/NoNullUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{

}
}
?>
1 change: 1 addition & 0 deletions West/Tests/PHP/NoNullUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function getErrorList()
return [
2 => 1,
20 => 1,
34 => 1
];
}

Expand Down

0 comments on commit 0b3ed9a

Please sign in to comment.