-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rule for disallowing implicit array creation
- Loading branch information
1 parent
8a269c8
commit 2dfbf80
Showing
4 changed files
with
108 additions
and
0 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
53 changes: 53 additions & 0 deletions
53
src/Rules/DisallowedConstructs/DisallowedImplicitArrayCreationRule.php
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,53 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\DisallowedConstructs; | ||
|
||
use PHPStan\Analyser\Scope; | ||
|
||
class DisallowedImplicitArrayCreationRule implements \PHPStan\Rules\Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return \PhpParser\Node\Expr\Assign::class; | ||
} | ||
|
||
/** | ||
* @param \PhpParser\Node\Expr\Assign $node | ||
* @param \PHPStan\Analyser\Scope $scope | ||
* @return string[] | ||
*/ | ||
public function processNode(\PhpParser\Node $node, Scope $scope): array | ||
{ | ||
if (!$node->var instanceof \PhpParser\Node\Expr\ArrayDimFetch) { | ||
return []; | ||
} | ||
|
||
$node = $node->var; | ||
while ($node instanceof \PhpParser\Node\Expr\ArrayDimFetch) { | ||
$node = $node->var; | ||
} | ||
|
||
if (!$node instanceof \PhpParser\Node\Expr\Variable) { | ||
return []; | ||
} | ||
|
||
if (!is_string($node->name)) { | ||
return []; | ||
} | ||
|
||
$certainty = $scope->hasVariableType($node->name); | ||
if ($certainty->no()) { | ||
return [ | ||
sprintf('Implicit array creation is not allowed - variable $%s does not exist.', $node->name), | ||
]; | ||
} elseif ($certainty->maybe()) { | ||
return [ | ||
sprintf('Implicit array creation is not allowed - variable $%s might not exist.', $node->name), | ||
]; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
} |
33 changes: 33 additions & 0 deletions
33
tests/Rules/DisallowedConstructs/DisallowedImplicitArrayCreationRuleTest.php
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,33 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\DisallowedConstructs; | ||
|
||
use PHPStan\Rules\Rule; | ||
|
||
class DisallowedImplicitArrayCreationRuleTest extends \PHPStan\Testing\RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new DisallowedImplicitArrayCreationRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/array-creation.php'], [ | ||
[ | ||
'Implicit array creation is not allowed - variable $b does not exist.', | ||
11, | ||
], | ||
[ | ||
'Implicit array creation is not allowed - variable $c might not exist.', | ||
17, | ||
], | ||
[ | ||
'Implicit array creation is not allowed - variable $d does not exist.', | ||
18, | ||
], | ||
]); | ||
} | ||
|
||
} |
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,21 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ImplicitArrayCreation; | ||
|
||
class Foo | ||
{ | ||
|
||
public function doFoo($a) | ||
{ | ||
$a['foo'] = 'test'; | ||
$b[] = 'test'; | ||
|
||
if (doFoo()) { | ||
$c = []; | ||
} | ||
|
||
$c['foo'] = 'test'; | ||
$d[][] = 'blabla'; | ||
} | ||
|
||
} |