-
Notifications
You must be signed in to change notification settings - Fork 540
Report float and null offset based on PHP version #4406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\DependencyInjection\RegisteredRule; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Rules\RuleLevelHelper; | ||
|
@@ -22,6 +23,7 @@ final class InvalidKeyInArrayItemRule implements Rule | |
|
||
public function __construct( | ||
private RuleLevelHelper $ruleLevelHelper, | ||
private PhpVersion $phpVersion, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. More modern approach is to get PhpVersions from Scope (a separate class). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does it means that all the method from PhpVersion should be deprecated/moved into PhpVersions ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not yet |
||
) | ||
{ | ||
} | ||
|
@@ -37,17 +39,18 @@ public function processNode(Node $node, Scope $scope): array | |
return []; | ||
} | ||
|
||
$phpVersion = $this->phpVersion; | ||
$dimensionType = $this->ruleLevelHelper->findTypeToCheck( | ||
$scope, | ||
$node->key, | ||
'', | ||
static fn (Type $dimType): bool => AllowedArrayKeysTypes::getType()->isSuperTypeOf($dimType)->yes(), | ||
static fn (Type $dimType): bool => AllowedArrayKeysTypes::getType($phpVersion)->isSuperTypeOf($dimType)->yes(), | ||
)->getType(); | ||
if ($dimensionType instanceof ErrorType) { | ||
return []; | ||
} | ||
|
||
$isSuperType = AllowedArrayKeysTypes::getType()->isSuperTypeOf($dimensionType); | ||
$isSuperType = AllowedArrayKeysTypes::getType($phpVersion)->isSuperTypeOf($dimensionType); | ||
if ($isSuperType->yes()) { | ||
return []; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,18 +3,18 @@ | |
namespace InvalidKeyArrayDimFetch; | ||
|
||
$a = []; | ||
$foo = $a[null]; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I moved PHP-related errors to the end for an easier handling in tests. |
||
|
||
$foo = $a[new \DateTimeImmutable()]; | ||
$a[[]] = $foo; | ||
$a[1]; | ||
$a[1.0]; | ||
|
||
$a['1']; | ||
$a[true]; | ||
$a[false]; | ||
|
||
/** @var string|null $stringOrNull */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I changed for a valid union here and move the invalid string|null to the end |
||
$stringOrNull = doFoo(); | ||
$a[$stringOrNull]; | ||
/** @var string|int $stringOrInt */ | ||
$stringOrInt = doFoo(); | ||
$a[$stringOrInt]; | ||
|
||
$obj = new \SplObjectStorage(); | ||
$obj[new \stdClass()] = 1; | ||
|
@@ -46,3 +46,11 @@ | |
$array[5][new \DateTimeImmutable()]; | ||
$array[new \stdClass()][new \DateTimeImmutable()]; | ||
$array[new \DateTimeImmutable()][] = 5; | ||
|
||
// Php version dependant | ||
$a[1.0]; | ||
$foo = $a[null]; | ||
|
||
/** @var string|null $stringOrNull */ | ||
$stringOrNull = doFoo(); | ||
$a[$stringOrNull]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why optional and nullable?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's used in ArrayType and ConstantArrayType
phpstan-src/src/Type/ArrayType.php
Line 273 in 2548dbb
and I don't have a PHPVersion here