-
Notifications
You must be signed in to change notification settings - Fork 478
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
InvalidKeyInArray: report float deprecation since PHP 8.1 #3220
base: 1.11.x
Are you sure you want to change the base?
Changes from all commits
1bae940
c1e2e03
1bef993
2394d7f
b7f6862
6105d23
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 |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Rules\RuleLevelHelper; | ||
|
@@ -22,6 +23,7 @@ class InvalidKeyInArrayDimFetchRule implements Rule | |
public function __construct( | ||
private RuleLevelHelper $ruleLevelHelper, | ||
private bool $reportMaybes, | ||
private PhpVersion $phpVersion, | ||
) | ||
{ | ||
} | ||
|
@@ -54,15 +56,34 @@ public function processNode(Node $node, Scope $scope): array | |
} | ||
|
||
$isSuperType = AllowedArrayKeysTypes::getType()->isSuperTypeOf($dimensionType); | ||
if ($isSuperType->yes() || ($isSuperType->maybe() && !$this->reportMaybes)) { | ||
return []; | ||
$isOk = $isSuperType->yes() || ($isSuperType->maybe() && !$this->reportMaybes); | ||
if (!$isOk) { | ||
return [ | ||
RuleErrorBuilder::message( | ||
sprintf('%s array key type %s.', $isSuperType->no() ? 'Invalid' : 'Possibly invalid', $dimensionType->describe(VerbosityLevel::typeOnly())), | ||
)->identifier('offsetAccess.invalidOffset')->build(), | ||
]; | ||
} | ||
|
||
if ($this->phpVersion->getVersionId() >= 80100) { | ||
$isFloat = $dimensionType->isFloat(); | ||
|
||
if ($isFloat->yes()) { | ||
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. We need to use RuleLevelHelper::findTypeToCheck here, it's going to help us on level 9 and also with benevolent union types. Case in point: |
||
return [ | ||
RuleErrorBuilder::message( | ||
'Float used as array key, this emits deprecation notice.', | ||
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. Message format to be used: |
||
)->identifier('array.invalidOffset')->build(), | ||
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.
|
||
]; | ||
} elseif ($this->reportMaybes && $isFloat->maybe()) { | ||
return [ | ||
RuleErrorBuilder::message( | ||
'Float possibly used as array key, this emits deprecation notice.', | ||
)->identifier('array.invalidOffset')->build(), | ||
]; | ||
} | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message( | ||
sprintf('%s array key type %s.', $isSuperType->no() ? 'Invalid' : 'Possibly invalid', $dimensionType->describe(VerbosityLevel::typeOnly())), | ||
)->identifier('offsetAccess.invalidOffset')->build(), | ||
]; | ||
return []; | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Php\PhpVersion; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\MixedType; | ||
|
@@ -16,7 +17,10 @@ | |
class InvalidKeyInArrayItemRule implements Rule | ||
{ | ||
|
||
public function __construct(private bool $reportMaybes) | ||
public function __construct( | ||
private bool $reportMaybes, | ||
private PhpVersion $phpVersion, | ||
) | ||
{ | ||
} | ||
|
||
|
@@ -47,6 +51,24 @@ public function processNode(Node $node, Scope $scope): array | |
]; | ||
} | ||
|
||
if ($this->phpVersion->getVersionId() >= 80100) { | ||
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. All the same points as in the other place :) |
||
$isFloat = $dimensionType->isFloat(); | ||
|
||
if ($isFloat->yes()) { | ||
return [ | ||
RuleErrorBuilder::message( | ||
'Float used as array key, this emits deprecation notice.', | ||
)->identifier('array.invalidKey')->build(), | ||
]; | ||
} elseif ($this->reportMaybes && $isFloat->maybe() && !$dimensionType instanceof MixedType) { | ||
return [ | ||
RuleErrorBuilder::message( | ||
'Float possibly used as array key, this emits deprecation notice.', | ||
)->identifier('array.invalidKey')->build(), | ||
]; | ||
} | ||
} | ||
|
||
return []; | ||
} | ||
|
||
|
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.
Instead of condition like this, we need a new method on PhpVersion that would be used here.