diff --git a/conf/bleedingEdge.neon b/conf/bleedingEdge.neon index cde18d2a329..d0bd30b4789 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 51450cddef4..d0f8e774e65 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 2ce812ccfdf..bbafd84855c 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/Rules/Arrays/ArrayUnpackingRule.php b/src/Rules/Arrays/ArrayUnpackingRule.php new file mode 100644 index 00000000000..8e8577b3204 --- /dev/null +++ b/src/Rules/Arrays/ArrayUnpackingRule.php @@ -0,0 +1,43 @@ + + */ +class ArrayUnpackingRule implements Rule +{ + + public function __construct(private PhpVersion $phpVersion) + { + } + + public function getNodeType(): string + { + return ArrayItem::class; + } + + public function processNode(Node $node, Scope $scope): array + { + if ($node->unpack === false || $this->phpVersion->supportsArrayUnpackingWithStringKeys()) { + return []; + } + + $valueType = $scope->getType($node->value); + + if ((new StringType())->isSuperTypeOf($valueType->getIterableKeyType())->no()) { + return []; + } + + return [RuleErrorBuilder::message('Array unpacking cannot be used on array that potentially has string keys.')->build()]; + } + +} diff --git a/tests/PHPStan/Rules/Arrays/ArrayUnpackingRuleTest.php b/tests/PHPStan/Rules/Arrays/ArrayUnpackingRuleTest.php new file mode 100644 index 00000000000..5d095037fff --- /dev/null +++ b/tests/PHPStan/Rules/Arrays/ArrayUnpackingRuleTest.php @@ -0,0 +1,68 @@ + + */ +class ArrayUnpackingRuleTest extends RuleTestCase +{ + + protected function getRule(): Rule + { + return new ArrayUnpackingRule(self::getContainer()->getByType(PhpVersion::class)); + } + + public function testRule(): void + { + if (PHP_VERSION_ID >= 80100) { + $this->markTestSkipped('Test requires PHP version <= 8.0'); + } + + $this->analyse([__DIR__ . '/data/array-unpacking.php'], [ + [ + 'Array unpacking cannot be used on array that potentially has string keys.', + 7, + ], + [ + 'Array unpacking cannot be used on array that potentially has string keys.', + 18, + ], + [ + 'Array unpacking cannot be used on array that potentially has string keys.', + 24, + ], + [ + 'Array unpacking cannot be used on array that potentially has string keys.', + 29, + ], + [ + 'Array unpacking cannot be used on array that potentially has string keys.', + 40, + ], + [ + 'Array unpacking cannot be used on array that potentially has string keys.', + 52, + ], + [ + 'Array unpacking cannot be used on array that potentially has string keys.', + 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 00000000000..7e1afcf9db5 --- /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, + ]; +}