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
6 changed files
with
187 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
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,43 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Arrays; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\ArrayItem; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\StringType; | ||
|
||
/** | ||
* @implements Rule<ArrayItem> | ||
*/ | ||
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()]; | ||
} | ||
|
||
} |
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,68 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Arrays; | ||
|
||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
* @extends RuleTestCase<ArrayUnpackingRule> | ||
*/ | ||
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'], []); | ||
} | ||
|
||
} |
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,66 @@ | ||
<?php // lint >= 7.4 | ||
|
||
namespace ArrayUnpacking; | ||
|
||
$foo = ['foo' => 'bar', 1, 2, 3]; | ||
|
||
$bar = [...$foo]; | ||
|
||
/** @param array<int, string> $bar */ | ||
function intKeyedArray(array $bar) | ||
{ | ||
$baz = [...$bar]; | ||
} | ||
|
||
/** @param array<string, string> $bar */ | ||
function stringKeyedArray(array $bar) | ||
{ | ||
$baz = [...$bar]; | ||
} | ||
|
||
/** @param array<array-key, string> $bar */ | ||
function unionKeyedArray(array $bar) | ||
{ | ||
$baz = [...$bar]; | ||
} | ||
|
||
function mixedKeyedArray(array $bar) | ||
{ | ||
$baz = [...$bar]; | ||
} | ||
|
||
/** | ||
* @param array<mixed, string> $foo | ||
* @param array<int, string> $bar | ||
*/ | ||
function multipleUnpacking(array $foo, array $bar) | ||
{ | ||
$baz = [ | ||
...$bar, | ||
...$foo, | ||
]; | ||
} | ||
|
||
/** | ||
* @param array<mixed, string> $foo | ||
* @param array<string, string> $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, | ||
]; | ||
} |