Skip to content
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

Open
wants to merge 6 commits into
base: 1.11.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 8 additions & 5 deletions src/Rules/Arrays/DuplicateKeysInLiteralArraysRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use function array_keys;
use function count;
use function implode;
use function is_float;
use function key;
use function max;
use function sprintf;
use function var_export;
Expand Down Expand Up @@ -85,23 +87,24 @@ public function processNode(Node $node, Scope $scope): array
}

$value = $keyType->getValue();
$index = is_float($value) ? (int) $value : key([$value => null]);
$printedValue = $key !== null
? $this->exprPrinter->printExpr($key)
: $value;

$printedValues[$value][] = $printedValue;
$printedValues[$index][] = $printedValue;

if (!isset($valueLines[$value])) {
$valueLines[$value] = $item->getStartLine();
if (!isset($valueLines[$index])) {
$valueLines[$index] = $item->getStartLine();
}

$previousCount = count($values);
$values[$value] = $printedValue;
$values[$index] = $printedValue;
if ($previousCount !== count($values)) {
continue;
}

$duplicateKeys[$value] = true;
$duplicateKeys[$index] = true;
}

$messages = [];
Expand Down
35 changes: 28 additions & 7 deletions src/Rules/Arrays/InvalidKeyInArrayDimFetchRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,6 +23,7 @@ class InvalidKeyInArrayDimFetchRule implements Rule
public function __construct(
private RuleLevelHelper $ruleLevelHelper,
private bool $reportMaybes,
private PhpVersion $phpVersion,
)
{
}
Expand Down Expand Up @@ -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) {
Copy link
Member

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.

$isFloat = $dimensionType->isFloat();

if ($isFloat->yes()) {
Copy link
Member

Choose a reason for hiding this comment

The 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: $mixed - 1 is already benevolent union type so this problem would already be solved: https://phpstan.org/r/5af2741f-b958-42f7-9248-435416d1b584

return [
RuleErrorBuilder::message(
'Float used as array key, this emits deprecation notice.',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Message format to be used: Deprecated in PHP 8.1: Float can no longer be used as array key, %s given.

)->identifier('array.invalidOffset')->build(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

array.floatKey might be better?

];
} 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 [];
}

}
24 changes: 23 additions & 1 deletion src/Rules/Arrays/InvalidKeyInArrayItemRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,7 +17,10 @@
class InvalidKeyInArrayItemRule implements Rule
{

public function __construct(private bool $reportMaybes)
public function __construct(
private bool $reportMaybes,
private PhpVersion $phpVersion,
)
{
}

Expand Down Expand Up @@ -47,6 +51,24 @@ public function processNode(Node $node, Scope $scope): array
];
}

if ($this->phpVersion->getVersionId() >= 80100) {
Copy link
Member

Choose a reason for hiding this comment

The 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 [];
}

Expand Down
21 changes: 18 additions & 3 deletions tests/PHPStan/Rules/Arrays/InvalidKeyInArrayDimFetchRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@

namespace PHPStan\Rules\Arrays;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Testing\RuleTestCase;
use function array_filter;
use function array_values;
use const PHP_VERSION_ID;

/**
Expand All @@ -16,12 +19,12 @@ class InvalidKeyInArrayDimFetchRuleTest extends RuleTestCase
protected function getRule(): Rule
{
$ruleLevelHelper = new RuleLevelHelper($this->createReflectionProvider(), true, false, true, false, false, true, false);
return new InvalidKeyInArrayDimFetchRule($ruleLevelHelper, true);
return new InvalidKeyInArrayDimFetchRule($ruleLevelHelper, true, new PhpVersion(PHP_VERSION_ID));
}

public function testInvalidKey(): void
{
$this->analyse([__DIR__ . '/data/invalid-key-array-dim-fetch.php'], [
$this->analyse([__DIR__ . '/data/invalid-key-array-dim-fetch.php'], array_values(array_filter([
[
'Invalid array key type DateTimeImmutable.',
7,
Expand All @@ -30,6 +33,12 @@ public function testInvalidKey(): void
'Invalid array key type array.',
8,
],
PHP_VERSION_ID >= 80100
? [
'Float used as array key, this emits deprecation notice.',
10,
]
: null,
[
'Possibly invalid array key type stdClass|string.',
24,
Expand Down Expand Up @@ -58,7 +67,13 @@ public function testInvalidKey(): void
'Invalid array key type DateTimeImmutable.',
48,
],
]);
PHP_VERSION_ID >= 80100
? [
'Float possibly used as array key, this emits deprecation notice.',
52,
]
: null,
])));
}

public function testBug6315(): void
Expand Down
21 changes: 18 additions & 3 deletions tests/PHPStan/Rules/Arrays/InvalidKeyInArrayItemRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace PHPStan\Rules\Arrays;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;
use function array_filter;
use function array_values;
use const PHP_VERSION_ID;

/**
Expand All @@ -14,12 +17,12 @@ class InvalidKeyInArrayItemRuleTest extends RuleTestCase

protected function getRule(): Rule
{
return new InvalidKeyInArrayItemRule(true);
return new InvalidKeyInArrayItemRule(true, new PhpVersion(PHP_VERSION_ID));
}

public function testInvalidKey(): void
{
$this->analyse([__DIR__ . '/data/invalid-key-array-item.php'], [
$this->analyse([__DIR__ . '/data/invalid-key-array-item.php'], array_values(array_filter([
[
'Invalid array key type DateTimeImmutable.',
13,
Expand All @@ -32,7 +35,19 @@ public function testInvalidKey(): void
'Possibly invalid array key type stdClass|string.',
15,
],
]);
PHP_VERSION_ID >= 80100
? [
'Float used as array key, this emits deprecation notice.',
16,
]
: null,
PHP_VERSION_ID >= 80100
? [
'Float possibly used as array key, this emits deprecation notice.',
24,
]
: null,
])));
}

public function testInvalidKeyInList(): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@
$array[5][new \DateTimeImmutable()];
$array[new \stdClass()][new \DateTimeImmutable()];
$array[new \DateTimeImmutable()][] = 5;

/** @var float|null $floatOrNull */
$floatOrNull = null;
$a[$floatOrNull];
$a[$mixed];
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/invalid-key-array-item.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,13 @@
new \DateTimeImmutable() => 'aaa',
[] => 'bbb',
$stringOrObject => 'aaa',
1.0 => 'aaa',
$mixed => 'aaa',
];

/** @var float|null $floatOrNull */
$floatOrNull = null;

$b = [
$floatOrNull => 'aaa',
];
Loading