-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Isset rule for checking always-defined/never-defined properties, arra…
…y offsets etc. (level 4) - bleeding edge
- Loading branch information
1 parent
70ee0a4
commit 25b61d9
Showing
10 changed files
with
315 additions
and
12 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
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,42 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Variables; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\IssetCheck; | ||
|
||
/** | ||
* @implements \PHPStan\Rules\Rule<Node\Expr\Isset_> | ||
*/ | ||
class IssetRule implements \PHPStan\Rules\Rule | ||
{ | ||
|
||
/** @var IssetCheck */ | ||
private $issetCheck; | ||
|
||
public function __construct(IssetCheck $issetCheck) | ||
{ | ||
$this->issetCheck = $issetCheck; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\Isset_::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$messages = []; | ||
foreach ($node->vars as $var) { | ||
$error = $this->issetCheck->check($var, $scope, 'in isset()'); | ||
if ($error === null) { | ||
continue; | ||
} | ||
$messages[] = $error; | ||
} | ||
|
||
return $messages; | ||
} | ||
|
||
} |
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,84 @@ | ||
<?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\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<IssetRule> | ||
*/ | ||
class IssetRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new IssetRule(new IssetCheck(new PropertyDescriptor(), new PropertyReflectionFinder())); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/isset.php'], [ | ||
[ | ||
'Property IssetRule\FooCoalesce::$string (string) in isset() is not nullable.', | ||
32, | ||
], | ||
[ | ||
'Offset \'string\' on array(1, 2, 3) in isset() does not exist.', | ||
45, | ||
], | ||
[ | ||
'Offset \'string\' on array(array(1), array(2), array(3)) in isset() does not exist.', | ||
49, | ||
], | ||
[ | ||
'Offset \'dim\' on array(\'dim\' => 1, \'dim-null\' => 1|null, \'dim-null-offset\' => array(\'a\' => true|null), \'dim-empty\' => array()) in isset() always exists and is not nullable.', | ||
67, | ||
], | ||
[ | ||
'Offset \'b\' on array() in isset() does not exist.', | ||
79, | ||
], | ||
[ | ||
'Property IssetRule\FooCoalesce::$string (string) in isset() is not nullable.', | ||
85, | ||
], | ||
[ | ||
'Property IssetRule\FooCoalesce::$alwaysNull (null) in isset() is always null.', | ||
87, | ||
], | ||
[ | ||
'Property IssetRule\FooCoalesce::$string (string) in isset() is not nullable.', | ||
89, | ||
], | ||
[ | ||
'Static property IssetRule\FooCoalesce::$staticString (string) in isset() is not nullable.', | ||
95, | ||
], | ||
[ | ||
'Static property IssetRule\FooCoalesce::$staticAlwaysNull (null) in isset() is always null.', | ||
97, | ||
], | ||
[ | ||
'Property IssetRule\FooCoalesce::$string (string) in isset() is not nullable.', | ||
116, | ||
], | ||
[ | ||
'Property IssetRule\FooCoalesce::$alwaysNull (null) in isset() is always null.', | ||
118, | ||
], | ||
[ | ||
'Static property IssetRule\FooCoalesce::$staticAlwaysNull (null) in isset() is always null.', | ||
123, | ||
], | ||
[ | ||
'Static property IssetRule\FooCoalesce::$staticString (string) in isset() is not nullable.', | ||
124, | ||
], | ||
]); | ||
} | ||
|
||
} |
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
Oops, something went wrong.