Skip to content

Commit

Permalink
Rule for disallowing implicit array creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 27, 2018
1 parent 8a269c8 commit 2dfbf80
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
1 change: 1 addition & 0 deletions rules.neon
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ rules:
- PHPStan\Rules\BooleansInConditions\BooleanInIfConditionRule
- PHPStan\Rules\BooleansInConditions\BooleanInTernaryOperatorRule
- PHPStan\Rules\DisallowedConstructs\DisallowedEmptyRule
- PHPStan\Rules\DisallowedConstructs\DisallowedImplicitArrayCreationRule
- PHPStan\Rules\Methods\WrongCaseOfInheritedMethodRule
- PHPStan\Rules\StrictCalls\DynamicCallOnStaticMethodsRule
- PHPStan\Rules\StrictCalls\StrictFunctionCallsRule
Expand Down
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 [];
}

}
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,
],
]);
}

}
21 changes: 21 additions & 0 deletions tests/Rules/DisallowedConstructs/data/array-creation.php
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';
}

}

0 comments on commit 2dfbf80

Please sign in to comment.