-
Notifications
You must be signed in to change notification settings - Fork 477
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
293 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
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,111 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Functions; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\FuncCall; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Reflection\ReflectionProvider; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\NeverType; | ||
use PHPStan\Type\Php\ArrayColumnHelper; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function count; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<Node\Expr\FuncCall> | ||
*/ | ||
final class ArrayColumnRule implements Rule | ||
{ | ||
|
||
public function __construct( | ||
private readonly ReflectionProvider $reflectionProvider, | ||
private readonly bool $treatPhpDocTypesAsCertain, | ||
private bool $treatPhpDocTypesAsCertainTip, | ||
private ArrayColumnHelper $arrayColumnHelper, | ||
) | ||
{ | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return FuncCall::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!($node->name instanceof Node\Name)) { | ||
return []; | ||
} | ||
|
||
$args = $node->getArgs(); | ||
if (count($args) < 2) { | ||
return []; | ||
} | ||
|
||
if (!$this->reflectionProvider->hasFunction($node->name, $scope)) { | ||
return []; | ||
} | ||
|
||
$functionReflection = $this->reflectionProvider->getFunction($node->name, $scope); | ||
if ($functionReflection->getName() !== 'array_column') { | ||
return []; | ||
} | ||
|
||
$indexKeyType = null; | ||
if ($this->treatPhpDocTypesAsCertain) { | ||
$arrayType = $scope->getType($args[0]->value); | ||
$columnKeyType = $scope->getType($args[1]->value); | ||
if (count($args) >= 3) { | ||
$indexKeyType = $scope->getType($args[2]->value); | ||
} | ||
} else { | ||
$arrayType = $scope->getNativeType($args[0]->value); | ||
$columnKeyType = $scope->getNativeType($args[1]->value); | ||
if (count($args) >= 3) { | ||
$indexKeyType = $scope->getNativeType($args[2]->value); | ||
} | ||
} | ||
|
||
$errors = []; | ||
if ($columnKeyType->isNull()->no()) { | ||
[$returnValueType] = $this->arrayColumnHelper->getReturnValueType($arrayType, $columnKeyType, $scope); | ||
if ($returnValueType instanceof NeverType) { | ||
$errorBuilder = RuleErrorBuilder::message(sprintf( | ||
'Cannot access column %s on %s.', | ||
$columnKeyType->describe(VerbosityLevel::value()), | ||
$arrayType->getIterableValueType()->describe(VerbosityLevel::value()), | ||
))->identifier('arrayColumn.column'); | ||
|
||
if ($this->treatPhpDocTypesAsCertainTip) { | ||
$errorBuilder->treatPhpDocTypesAsCertainTip(); | ||
} | ||
|
||
$errors[] = $errorBuilder->build(); | ||
} | ||
} | ||
|
||
if ($indexKeyType !== null && $indexKeyType->isNull()->no()) { | ||
$returnValueType = $this->arrayColumnHelper->getReturnIndexType($arrayType, $indexKeyType, $scope); | ||
if ($returnValueType instanceof NeverType) { | ||
$errorBuilder = RuleErrorBuilder::message(sprintf( | ||
'Cannot access column %s on %s.', | ||
$indexKeyType->describe(VerbosityLevel::value()), | ||
$arrayType->getIterableValueType()->describe(VerbosityLevel::value()), | ||
))->identifier('arrayColumn.index'); | ||
|
||
$nativeArrayType = $scope->getNativeType($args[0]->value); | ||
if ($this->treatPhpDocTypesAsCertainTip) { | ||
$errorBuilder->treatPhpDocTypesAsCertainTip(); | ||
} | ||
|
||
$errors[] = $errorBuilder->build(); | ||
} | ||
} | ||
|
||
return $errors; | ||
} | ||
|
||
} |
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,130 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Functions; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use PHPStan\Type\Php\ArrayColumnHelper; | ||
use const PHP_VERSION_ID; | ||
|
||
/** | ||
* @extends RuleTestCase<ArrayColumnRule> | ||
*/ | ||
class ArrayColumnRuleTest extends RuleTestCase | ||
{ | ||
|
||
private bool $treatPhpDocTypesAsCertain = true; | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new ArrayColumnRule( | ||
$this->createReflectionProvider(), | ||
$this->treatPhpDocTypesAsCertain, | ||
true, | ||
self::getContainer()->getByType(ArrayColumnHelper::class), | ||
); | ||
} | ||
|
||
public function testFile(): void | ||
{ | ||
$expectedErrors = []; | ||
|
||
$this->analyse([__DIR__ . '/../../Analyser/nsrt/array-column-php7.php'], $expectedErrors); | ||
} | ||
|
||
public function testFilePhp82(): void | ||
{ | ||
if (PHP_VERSION_ID < 80200) { | ||
$this->markTestSkipped('Test requires PHP 8.2'); | ||
} | ||
|
||
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.'; | ||
$expectedErrors = [ | ||
[ | ||
"Cannot access column 'column' on *NEVER*.", | ||
30, | ||
$tipText, | ||
], | ||
[ | ||
"Cannot access column 'column' on *NEVER*.", | ||
31, | ||
$tipText, | ||
], | ||
[ | ||
"Cannot access column 'key' on *NEVER*.", | ||
31, | ||
$tipText, | ||
], | ||
[ | ||
"Cannot access column 'key' on *NEVER*.", | ||
32, | ||
$tipText, | ||
], | ||
[ | ||
"Cannot access column 'foo' on array{column: string, key: string}.", | ||
76, | ||
$tipText, | ||
], | ||
[ | ||
"Cannot access column 'foo' on array{column: string, key: string}.", | ||
77, | ||
$tipText, | ||
], | ||
[ | ||
"Cannot access column 'nodeName' on ArrayColumn82\Foo.", | ||
216, | ||
$tipText, | ||
], | ||
[ | ||
"Cannot access column 'nodeName' on ArrayColumn82\Foo.", | ||
217, | ||
$tipText, | ||
], | ||
[ | ||
"Cannot access column 'tagName' on ArrayColumn82\Foo.", | ||
217, | ||
$tipText, | ||
], | ||
]; | ||
|
||
$this->analyse([__DIR__ . '/../../Analyser/nsrt/array-column-php82.php'], $expectedErrors); | ||
} | ||
|
||
public function testBug5101(): void | ||
{ | ||
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.'; | ||
|
||
// in PHP < 8.2 dynamic properties can exist any time | ||
$expectedErrors = []; | ||
if (PHP_VERSION_ID >= 80200) { | ||
$expectedErrors = [ | ||
[ | ||
"Cannot access column 'y' on Bug5101\FinalFooBar.", | ||
22, | ||
$tipText, | ||
], | ||
]; | ||
} | ||
|
||
$this->analyse([__DIR__ . '/data/bug-5101.php'], $expectedErrors); | ||
} | ||
|
||
public function testBug12188(): void | ||
{ | ||
if (PHP_VERSION_ID < 80100) { | ||
$this->markTestSkipped('Test requires PHP 8.1'); | ||
} | ||
|
||
$tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.'; | ||
$expectedErrors = [ | ||
[ | ||
"Cannot access column 'value' on Bug12188\Foo::A|Bug12188\Foo::B.", | ||
14, | ||
$tipText, | ||
], | ||
]; | ||
|
||
$this->analyse([__DIR__ . '/data/bug-12188.php'], $expectedErrors); | ||
} | ||
|
||
} |
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,15 @@ | ||
<?php declare(strict_types = 1); // lint >= 8.1 | ||
|
||
namespace Bug12188; | ||
|
||
enum Foo | ||
{ | ||
case A; | ||
case B; | ||
} | ||
|
||
function doFoo() { | ||
$arr = [Foo::A, Foo::B]; | ||
|
||
var_dump(array_column($arr, 'value')); | ||
} |
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,23 @@ | ||
<?php declare(strict_types = 1); // lint >= 7.4 | ||
|
||
namespace Bug5101; | ||
|
||
class FooBar | ||
{ | ||
public $x; | ||
} | ||
|
||
final class FinalFooBar | ||
{ | ||
public $x; | ||
} | ||
|
||
/** @param array<FooBar> $arrClass */ | ||
function doFoo(array $arrClass) { | ||
$arrFinalClass = [new FinalFooBar()]; | ||
|
||
var_dump(array_column($arrClass, 'x')); | ||
var_dump(array_column($arrClass, 'y')); | ||
var_dump(array_column($arrFinalClass, 'x')); | ||
var_dump(array_column($arrFinalClass, 'y')); | ||
} |