-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1ef74b7
commit 601460c
Showing
7 changed files
with
228 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Variables; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\IssetCheck; | ||
use PHPStan\Type\Constant\ConstantBooleanType; | ||
use PHPStan\Type\ErrorType; | ||
use PHPStan\Type\NullType; | ||
use PHPStan\Type\Type; | ||
|
||
/** | ||
* @implements \PHPStan\Rules\Rule<Node\Expr\Empty_> | ||
*/ | ||
class EmptyRule implements \PHPStan\Rules\Rule | ||
{ | ||
|
||
private IssetCheck $issetCheck; | ||
|
||
public function __construct(IssetCheck $issetCheck) | ||
{ | ||
$this->issetCheck = $issetCheck; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\Empty_::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$error = $this->issetCheck->check($node->expr, $scope, 'in empty()', static function (Type $type): ?string { | ||
$isNull = (new NullType())->isSuperTypeOf($type); | ||
$isFalsey = (new ConstantBooleanType(false))->isSuperTypeOf($type->toBoolean()); | ||
if ($isNull->maybe()) { | ||
return null; | ||
} | ||
if ($isFalsey->maybe()) { | ||
return null; | ||
} | ||
|
||
if ($isNull->yes()) { | ||
if ($isFalsey->yes()) { | ||
return 'is always falsy'; | ||
} | ||
if ($isFalsey->no()) { | ||
return 'is not falsy'; | ||
} | ||
|
||
return 'is always null'; | ||
} | ||
|
||
if ($isFalsey->yes()) { | ||
return 'is always falsy'; | ||
} | ||
|
||
if ($isFalsey->no()) { | ||
return 'is not falsy'; | ||
} | ||
|
||
return 'is not nullable'; | ||
}); | ||
if ($error === null) { | ||
return []; | ||
} | ||
|
||
$exprType = $scope->getType($node->expr); | ||
$exprBooleanType = $exprType->toBoolean(); | ||
$isFalse = (new ConstantBooleanType(false))->isSuperTypeOf($exprBooleanType); | ||
if (!$exprType instanceof ErrorType && $isFalse->maybe()) { | ||
return []; | ||
} | ||
|
||
return [$error]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Variables; | ||
|
||
use PHPStan\Rules\IssetCheck; | ||
use PHPStan\Rules\Properties\PropertyDescriptor; | ||
use PHPStan\Rules\Properties\PropertyReflectionFinder; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<EmptyRule> | ||
*/ | ||
class EmptyRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): \PHPStan\Rules\Rule | ||
{ | ||
return new EmptyRule(new IssetCheck(new PropertyDescriptor(), new PropertyReflectionFinder())); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/empty-rule.php'], [ | ||
[ | ||
'Offset \'nonexistent\' on array(?0 => bool, ?1 => false, 2 => bool, 3 => false, 4 => true) in empty() does not exist.', | ||
22, | ||
], | ||
[ | ||
'Offset 3 on array(?0 => bool, ?1 => false, 2 => bool, 3 => false, 4 => true) in empty() always exists and is always falsy.', | ||
24, | ||
], | ||
[ | ||
'Offset 4 on array(?0 => bool, ?1 => false, 2 => bool, 3 => false, 4 => true) in empty() always exists and is not falsy.', | ||
25, | ||
], | ||
[ | ||
'Offset 0 on array(\'\', \'0\', \'foo\', \'\'|\'foo\') in empty() always exists and is always falsy.', | ||
36, | ||
], | ||
[ | ||
'Offset 1 on array(\'\', \'0\', \'foo\', \'\'|\'foo\') in empty() always exists and is always falsy.', | ||
37, | ||
], | ||
[ | ||
'Offset 2 on array(\'\', \'0\', \'foo\', \'\'|\'foo\') in empty() always exists and is not falsy.', | ||
38, | ||
], | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?php | ||
|
||
namespace EmptyRule; | ||
|
||
class Foo | ||
{ | ||
|
||
public function doFoo() | ||
{ | ||
$a = []; | ||
if (rand(0, 1)) { | ||
$a[0] = rand(0,1) ? true : false; | ||
$a[1] = false; | ||
} | ||
|
||
$a[2] = rand(0,1) ? true : false; | ||
$a[3] = false; | ||
$a[4] = true; | ||
|
||
empty($a[0]); | ||
empty($a[1]); | ||
empty($a['nonexistent']); | ||
empty($a[2]); | ||
empty($a[3]); | ||
empty($a[4]); | ||
} | ||
|
||
public function doBar() | ||
{ | ||
$a = [ | ||
'', | ||
'0', | ||
'foo', | ||
rand(0, 1) ? '' : 'foo', | ||
]; | ||
empty($a[0]); | ||
empty($a[1]); | ||
empty($a[2]); | ||
empty($a[3]); | ||
} | ||
|
||
} |