-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EnforceIteratorToArrayPreserveKeysRule (#154)
- Loading branch information
Showing
5 changed files
with
137 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
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,57 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ShipMonk\PHPStan\Rule; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PhpParser\Node\Name; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleError; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use function count; | ||
|
||
/** | ||
* @implements Rule<FuncCall> | ||
*/ | ||
class EnforceIteratorToArrayPreserveKeysRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return FuncCall::class; | ||
} | ||
|
||
/** | ||
* @param FuncCall $node | ||
* @return list<RuleError> | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$node->name instanceof Name) { | ||
return []; | ||
} | ||
|
||
if ($node->name->toString() !== 'iterator_to_array') { | ||
return []; | ||
} | ||
|
||
if (count($node->getArgs()) >= 2) { | ||
return []; | ||
} | ||
|
||
if (count($node->getArgs()) === 0) { | ||
return []; | ||
} | ||
|
||
if ($node->getArgs()[0]->unpack) { | ||
return []; // not trying to analyse what is being unpacked as this is very non-standard approach here | ||
} | ||
|
||
return [RuleErrorBuilder::message('Calling iterator_to_array without 2nd parameter $preserve_keys. Default value true might cause failures or data loss.') | ||
->line($node->getLine()) | ||
->identifier('iteratorToArrayWithoutPreserveKeys') | ||
->build()]; | ||
} | ||
|
||
} |
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,25 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Rule; | ||
|
||
use PHPStan\Rules\Rule; | ||
use ShipMonk\PHPStan\Rule\EnforceIteratorToArrayPreserveKeysRule; | ||
use ShipMonk\PHPStan\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<EnforceIteratorToArrayPreserveKeysRule> | ||
*/ | ||
class EnforceIteratorToArrayPreserveKeysRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new EnforceIteratorToArrayPreserveKeysRule(); | ||
} | ||
|
||
public function test(): void | ||
{ | ||
$this->analyseFile(__DIR__ . '/data/EnforceIteratorToArrayPreserveKeysRule/code.php'); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
tests/Rule/data/EnforceIteratorToArrayPreserveKeysRule/code.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,31 @@ | ||
<?php | ||
|
||
namespace EnforceIteratorToArrayPreserveKeysRule; | ||
|
||
$objectAsKey = function () { | ||
yield new \stdClass => 1; | ||
}; | ||
|
||
$dataLoss = function () { | ||
yield 1 => 1; | ||
yield 1 => 2; | ||
}; | ||
|
||
$noKeys = function () { | ||
yield 1; | ||
yield 2; | ||
}; | ||
|
||
|
||
iterator_to_array($objectAsKey()); // error: Calling iterator_to_array without 2nd parameter $preserve_keys. Default value true might cause failures or data loss. | ||
iterator_to_array($dataLoss()); // error: Calling iterator_to_array without 2nd parameter $preserve_keys. Default value true might cause failures or data loss. | ||
iterator_to_array($noKeys()); // error: Calling iterator_to_array without 2nd parameter $preserve_keys. Default value true might cause failures or data loss. | ||
|
||
iterator_to_array($objectAsKey(), false); | ||
iterator_to_array($dataLoss(), false); | ||
iterator_to_array($noKeys(), true); | ||
iterator_to_array(... [$noKeys(), true]); | ||
iterator_to_array( | ||
preserve_keys: true, | ||
iterator: $noKeys() | ||
); |