-
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.
- Loading branch information
Showing
8 changed files
with
316 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace ShipMonk\PHPStan\Rule; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ArrayDimFetch; | ||
use PhpParser\Node\Expr\ArrayItem; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\IdentifierRuleError; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\IntegerType; | ||
use PHPStan\Type\MixedType; | ||
use PHPStan\Type\StringType; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeCombinator; | ||
use PHPStan\Type\VerbosityLevel; | ||
|
||
/** | ||
* @implements Rule<Node> | ||
*/ | ||
class ForbidUnsafeArrayKeyRule implements Rule | ||
{ | ||
|
||
private bool $reportMixed; | ||
|
||
private bool $reportInsideIsset; | ||
|
||
public function __construct( | ||
bool $reportMixed, | ||
bool $reportInsideIsset | ||
) | ||
{ | ||
$this->reportMixed = $reportMixed; | ||
$this->reportInsideIsset = $reportInsideIsset; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node::class; | ||
} | ||
|
||
/** | ||
* @return list<IdentifierRuleError> | ||
*/ | ||
public function processNode( | ||
Node $node, | ||
Scope $scope | ||
): array | ||
{ | ||
if ($node instanceof ArrayItem && $node->key !== null) { | ||
$keyType = $scope->getType($node->key); | ||
|
||
if (!$this->isArrayKey($keyType)) { | ||
return [ | ||
RuleErrorBuilder::message('Array key must be integer or string, but ' . $keyType->describe(VerbosityLevel::precise()) . ' given.') | ||
->identifier('shipmonk.unsafeArrayKey') | ||
->build(), | ||
]; | ||
} | ||
} | ||
|
||
if ( | ||
$node instanceof ArrayDimFetch | ||
&& $node->dim !== null | ||
&& !$scope->getType($node->var)->isArray()->no() | ||
) { | ||
if (!$this->reportInsideIsset && $scope->isUndefinedExpressionAllowed($node)) { | ||
return []; | ||
} | ||
|
||
$dimType = $scope->getType($node->dim); | ||
|
||
if (!$this->isArrayKey($dimType)) { | ||
return [ | ||
RuleErrorBuilder::message('Array key must be integer or string, but ' . $dimType->describe(VerbosityLevel::precise()) . ' given.') | ||
->identifier('shipmonk.unsafeArrayKey') | ||
->build(), | ||
]; | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
|
||
private function isArrayKey(Type $type): bool | ||
{ | ||
if (!$this->reportMixed && $type instanceof MixedType) { | ||
return true; | ||
} | ||
|
||
return $this->containsOnlyTypes($type, [new StringType(), new IntegerType()]); | ||
} | ||
|
||
/** | ||
* @param list<Type> $allowedTypes | ||
*/ | ||
private function containsOnlyTypes( | ||
Type $checkedType, | ||
array $allowedTypes | ||
): bool | ||
{ | ||
$allowedType = TypeCombinator::union(...$allowedTypes); | ||
return $allowedType->isSuperTypeOf($checkedType)->yes(); | ||
} | ||
|
||
} |
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,50 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Rule; | ||
|
||
use LogicException; | ||
use PHPStan\Rules\Rule; | ||
use ShipMonk\PHPStan\Rule\ForbidUnsafeArrayKeyRule; | ||
use ShipMonk\PHPStan\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<ForbidUnsafeArrayKeyRule> | ||
*/ | ||
class ForbidUnsafeArrayKeyRuleTest extends RuleTestCase | ||
{ | ||
|
||
private ?bool $checkMixed = null; | ||
|
||
private ?bool $checkInsideIsset = null; | ||
|
||
protected function getRule(): Rule | ||
{ | ||
if ($this->checkMixed === null) { | ||
throw new LogicException('Property checkMixed must be set'); | ||
} | ||
|
||
if ($this->checkInsideIsset === null) { | ||
throw new LogicException('Property checkInsideIsset must be set'); | ||
} | ||
|
||
return new ForbidUnsafeArrayKeyRule( | ||
$this->checkMixed, | ||
$this->checkInsideIsset, | ||
); | ||
} | ||
|
||
public function testStrict(): void | ||
{ | ||
$this->checkMixed = true; | ||
$this->checkInsideIsset = true; | ||
$this->analyseFile(__DIR__ . '/data/ForbidUnsafeArrayKeyRule/default.php'); | ||
} | ||
|
||
public function testLessStrict(): void | ||
{ | ||
$this->checkMixed = false; | ||
$this->checkInsideIsset = false; | ||
$this->analyseFile(__DIR__ . '/data/ForbidUnsafeArrayKeyRule/less-strict.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 ForbidUnsafeArrayKeyRule\MixedDisabled; | ||
|
||
|
||
class ArrayKey | ||
{ | ||
|
||
/** | ||
* @param array-key $arrayKey | ||
*/ | ||
public function test( | ||
int $int, | ||
string $string, | ||
float $float, | ||
$arrayKey, | ||
int|string $intOrString, | ||
int|float $intOrFloat, | ||
array $array, | ||
object $object, | ||
\WeakMap $weakMap, | ||
mixed $explicitMixed, | ||
$implicitMixed | ||
) { | ||
$array[$array] = ''; // error: Array key must be integer or string, but array given. | ||
$array[$int] = ''; | ||
$array[$string] = ''; | ||
$array[$float] = ''; // error: Array key must be integer or string, but float given. | ||
$array[$intOrFloat] = ''; // error: Array key must be integer or string, but float|int given. | ||
$array[$intOrString] = ''; | ||
$array[$explicitMixed] = ''; // error: Array key must be integer or string, but mixed given. | ||
$array[$implicitMixed] = ''; // error: Array key must be integer or string, but mixed given. | ||
$array[$arrayKey] = ''; | ||
$weakMap[$object] = ''; | ||
|
||
$a = $array[$float] ?? ''; // error: Array key must be integer or string, but float given. | ||
$b = isset($array[$float]); // error: Array key must be integer or string, but float given. | ||
$c = empty($array[$float]); // error: Array key must be integer or string, but float given. | ||
|
||
[ | ||
$int => $int, | ||
$string => $string, | ||
$float => $float, // error: Array key must be integer or string, but float given. | ||
$intOrFloat => $intOrFloat, // error: Array key must be integer or string, but float|int given. | ||
$intOrString => $intOrString, | ||
$explicitMixed => $explicitMixed, // error: Array key must be integer or string, but mixed given. | ||
$implicitMixed => $implicitMixed, // error: Array key must be integer or string, but mixed given. | ||
$arrayKey => $arrayKey, | ||
]; | ||
} | ||
} | ||
|
||
|
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 ForbidUnsafeArrayKeyRule\MixedEnabled; | ||
|
||
|
||
class ArrayKey | ||
{ | ||
|
||
/** | ||
* @param array-key $arrayKey | ||
*/ | ||
public function test( | ||
int $int, | ||
string $string, | ||
float $float, | ||
$arrayKey, | ||
int|string $intOrString, | ||
int|float $intOrFloat, | ||
array $array, | ||
object $object, | ||
\WeakMap $weakMap, | ||
mixed $explicitMixed, | ||
$implicitMixed | ||
) { | ||
$array[$array] = ''; // error: Array key must be integer or string, but array given. | ||
$array[$int] = ''; | ||
$array[$string] = ''; | ||
$array[$float] = ''; // error: Array key must be integer or string, but float given. | ||
$array[$intOrFloat] = ''; // error: Array key must be integer or string, but float|int given. | ||
$array[$intOrString] = ''; | ||
$array[$explicitMixed] = ''; | ||
$array[$implicitMixed] = ''; | ||
$array[$arrayKey] = ''; | ||
$weakMap[$object] = ''; | ||
|
||
$a = $array[$float] ?? ''; | ||
$b = isset($array[$float]); | ||
$c = empty($array[$float]); | ||
|
||
[ | ||
$int => $int, | ||
$string => $string, | ||
$float => $float, // error: Array key must be integer or string, but float given. | ||
$intOrFloat => $intOrFloat, // error: Array key must be integer or string, but float|int given. | ||
$intOrString => $intOrString, | ||
$explicitMixed => $explicitMixed, | ||
$implicitMixed => $implicitMixed, | ||
$arrayKey => $arrayKey, | ||
]; | ||
} | ||
} | ||
|
||
|
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