diff --git a/conf/bleedingEdge.neon b/conf/bleedingEdge.neon index cde18d2a32..d0bd30b478 100644 --- a/conf/bleedingEdge.neon +++ b/conf/bleedingEdge.neon @@ -4,5 +4,6 @@ parameters: skipCheckGenericClasses: [] explicitMixedInUnknownGenericNew: true arrayFilter: true + arrayUnpacking: true stubFiles: - ../stubs/bleedingEdge/Countable.stub diff --git a/conf/config.level3.neon b/conf/config.level3.neon index 51450cddef..d0f8e774e6 100644 --- a/conf/config.level3.neon +++ b/conf/config.level3.neon @@ -1,6 +1,10 @@ includes: - config.level2.neon +conditionalTags: + PHPStan\Rules\Arrays\ArrayUnpackingRule: + phpstan.rules.rule: %featureToggles.arrayUnpacking% + rules: - PHPStan\Rules\Arrays\ArrayDestructuringRule - PHPStan\Rules\Arrays\IterableInForeachRule @@ -74,3 +78,6 @@ services: reportMaybes: %reportMaybes% tags: - phpstan.rules.rule + + - + class: PHPStan\Rules\Arrays\ArrayUnpackingRule diff --git a/conf/config.neon b/conf/config.neon index 2ce812ccfd..bbafd84855 100644 --- a/conf/config.neon +++ b/conf/config.neon @@ -29,6 +29,7 @@ parameters: - RecursiveCallbackFilterIterator explicitMixedInUnknownGenericNew: false arrayFilter: false + arrayUnpacking: false fileExtensions: - php checkAdvancedIsset: false @@ -207,6 +208,7 @@ parametersSchema: skipCheckGenericClasses: listOf(string()), explicitMixedInUnknownGenericNew: bool(), arrayFilter: bool(), + arrayUnpacking: bool(), ]) fileExtensions: listOf(string()) checkAdvancedIsset: bool() diff --git a/src/Analyser/MutatingScope.php b/src/Analyser/MutatingScope.php index 7695725ed2..887cc838b0 100644 --- a/src/Analyser/MutatingScope.php +++ b/src/Analyser/MutatingScope.php @@ -31,6 +31,7 @@ use PhpParser\NodeFinder; use PhpParser\PrettyPrinter\Standard; use PHPStan\Node\ExecutionEndNode; +use PHPStan\Node\Expr\GetIterableKeyTypeExpr; use PHPStan\Node\Expr\GetIterableValueTypeExpr; use PHPStan\Node\Expr\GetOffsetValueTypeExpr; use PHPStan\Node\Expr\OriginalPropertyTypeExpr; @@ -544,6 +545,9 @@ public function getAnonymousFunctionReturnType(): ?Type /** @api */ public function getType(Expr $node): Type { + if ($node instanceof GetIterableKeyTypeExpr) { + return $this->getType($node->getExpr())->getIterableKeyType(); + } if ($node instanceof GetIterableValueTypeExpr) { return $this->getType($node->getExpr())->getIterableValueType(); } diff --git a/src/Node/Expr/GetIterableKeyTypeExpr.php b/src/Node/Expr/GetIterableKeyTypeExpr.php new file mode 100644 index 0000000000..3f0f9f7091 --- /dev/null +++ b/src/Node/Expr/GetIterableKeyTypeExpr.php @@ -0,0 +1,34 @@ +getAttributes()); + } + + public function getExpr(): Expr + { + return $this->expr; + } + + public function getType(): string + { + return 'PHPStan_Node_GetIterableKeyTypeExpr'; + } + + /** + * @return string[] + */ + public function getSubNodeNames(): array + { + return []; + } + +} diff --git a/src/Rules/Arrays/ArrayUnpackingRule.php b/src/Rules/Arrays/ArrayUnpackingRule.php new file mode 100644 index 0000000000..7888da7547 --- /dev/null +++ b/src/Rules/Arrays/ArrayUnpackingRule.php @@ -0,0 +1,67 @@ + + */ +class ArrayUnpackingRule implements Rule +{ + + public function __construct(private PhpVersion $phpVersion, private RuleLevelHelper $ruleLevelHelper) + { + } + + public function getNodeType(): string + { + return ArrayItem::class; + } + + public function processNode(Node $node, Scope $scope): array + { + if ($node->unpack === false || $this->phpVersion->supportsArrayUnpackingWithStringKeys()) { + return []; + } + + $stringType = new StringType(); + $typeResult = $this->ruleLevelHelper->findTypeToCheck( + $scope, + new GetIterableKeyTypeExpr($node->value), + '', + static fn (Type $type): bool => $stringType->isSuperTypeOf($type)->no(), + ); + + $keyType = $typeResult->getType(); + if ($keyType instanceof ErrorType) { + return $typeResult->getUnknownClassErrors(); + } + + $isString = $stringType->isSuperTypeOf($keyType); + if ($isString->no()) { + return []; + } + + return [ + RuleErrorBuilder::message(sprintf( + 'Array unpacking cannot be used on an array with %sstring keys: %s', + $isString->yes() ? '' : 'potential ', + $scope->getType($node->value)->describe(VerbosityLevel::value()), + ))->build(), + ]; + } + +} diff --git a/tests/PHPStan/Rules/Arrays/ArrayUnpackingRuleTest.php b/tests/PHPStan/Rules/Arrays/ArrayUnpackingRuleTest.php new file mode 100644 index 0000000000..4dfc9ff2df --- /dev/null +++ b/tests/PHPStan/Rules/Arrays/ArrayUnpackingRuleTest.php @@ -0,0 +1,94 @@ + + */ +class ArrayUnpackingRuleTest extends RuleTestCase +{ + + private bool $checkUnions; + + protected function getRule(): Rule + { + return new ArrayUnpackingRule( + self::getContainer()->getByType(PhpVersion::class), + new RuleLevelHelper($this->createReflectionProvider(), true, false, $this->checkUnions, false), + ); + } + + public function testRule(): void + { + if (PHP_VERSION_ID >= 80100) { + $this->markTestSkipped('Test requires PHP version <= 8.0'); + } + + $this->checkUnions = true; + $this->analyse([__DIR__ . '/data/array-unpacking.php'], [ + [ + 'Array unpacking cannot be used on an array with potential string keys: array{foo: \'bar\', 0: 1, 1: 2, 2: 3}', + 7, + ], + [ + 'Array unpacking cannot be used on an array with string keys: array', + 18, + ], + [ + 'Array unpacking cannot be used on an array with potential string keys: array', + 24, + ], + [ + 'Array unpacking cannot be used on an array with potential string keys: array', + 29, + ], + [ + 'Array unpacking cannot be used on an array with potential string keys: array', + 40, + ], + [ + 'Array unpacking cannot be used on an array with potential string keys: array', + 52, + ], + [ + 'Array unpacking cannot be used on an array with string keys: array{foo: string, bar: int}', + 63, + ], + ]); + } + + public function testRuleDoNotCheckUnions(): void + { + if (PHP_VERSION_ID >= 80100) { + $this->markTestSkipped('Test requires PHP version <= 8.0'); + } + + $this->checkUnions = false; + $this->analyse([__DIR__ . '/data/array-unpacking.php'], [ + [ + 'Array unpacking cannot be used on an array with string keys: array', + 18, + ], + [ + 'Array unpacking cannot be used on an array with string keys: array{foo: string, bar: int}', + 63, + ], + ]); + } + + public function testRuleOnPHP81(): void + { + if (PHP_VERSION_ID < 80100) { + $this->markTestSkipped('Test requires PHP 8.1+'); + } + + $this->analyse([__DIR__ . '/data/array-unpacking.php'], []); + } + +} diff --git a/tests/PHPStan/Rules/Arrays/data/array-unpacking.php b/tests/PHPStan/Rules/Arrays/data/array-unpacking.php new file mode 100644 index 0000000000..7e1afcf9db --- /dev/null +++ b/tests/PHPStan/Rules/Arrays/data/array-unpacking.php @@ -0,0 +1,66 @@ += 7.4 + +namespace ArrayUnpacking; + +$foo = ['foo' => 'bar', 1, 2, 3]; + +$bar = [...$foo]; + +/** @param array $bar */ +function intKeyedArray(array $bar) +{ + $baz = [...$bar]; +} + +/** @param array $bar */ +function stringKeyedArray(array $bar) +{ + $baz = [...$bar]; +} + +/** @param array $bar */ +function unionKeyedArray(array $bar) +{ + $baz = [...$bar]; +} + +function mixedKeyedArray(array $bar) +{ + $baz = [...$bar]; +} + +/** + * @param array $foo + * @param array $bar + */ +function multipleUnpacking(array $foo, array $bar) +{ + $baz = [ + ...$bar, + ...$foo, + ]; +} + +/** + * @param array $foo + * @param array $bar + */ +function foo(array $foo, array $bar) +{ + $baz = [ + $bar, + ...$foo + ]; +} + +/** + * @param array{foo: string, bar:int} $foo + * @param array{1, 2, 3, 4} $bar + */ +function unpackingArrayShapes(array $foo, array $bar) +{ + $baz = [ + ...$foo, + ...$bar, + ]; +}