Skip to content

Commit 1bae940

Browse files
committed
InvalidKeyInArray: report float deprecation since PHP 8.1
1 parent 66a7578 commit 1bae940

File tree

5 files changed

+55
-14
lines changed

5 files changed

+55
-14
lines changed

src/Rules/Arrays/InvalidKeyInArrayDimFetchRule.php

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\Php\PhpVersion;
78
use PHPStan\Rules\Rule;
89
use PHPStan\Rules\RuleErrorBuilder;
910
use PHPStan\Rules\RuleLevelHelper;
@@ -22,6 +23,7 @@ class InvalidKeyInArrayDimFetchRule implements Rule
2223
public function __construct(
2324
private RuleLevelHelper $ruleLevelHelper,
2425
private bool $reportMaybes,
26+
private PhpVersion $phpVersion,
2527
)
2628
{
2729
}
@@ -54,15 +56,24 @@ public function processNode(Node $node, Scope $scope): array
5456
}
5557

5658
$isSuperType = AllowedArrayKeysTypes::getType()->isSuperTypeOf($dimensionType);
57-
if ($isSuperType->yes() || ($isSuperType->maybe() && !$this->reportMaybes)) {
58-
return [];
59+
$isOk = $isSuperType->yes() || ($isSuperType->maybe() && !$this->reportMaybes);
60+
if (!$isOk) {
61+
return [
62+
RuleErrorBuilder::message(
63+
sprintf('%s array key type %s.', $isSuperType->no() ? 'Invalid' : 'Possibly invalid', $dimensionType->describe(VerbosityLevel::typeOnly())),
64+
)->identifier('offsetAccess.invalidOffset')->build(),
65+
];
66+
}
67+
68+
if ($this->phpVersion->getVersionId() >= 80_100 && !$dimensionType->isFloat()->no()) {
69+
return [
70+
RuleErrorBuilder::message(
71+
'Using float as array key emits deprecation notice.',
72+
)->identifier('array.invalidKey')->build(),
73+
];
5974
}
6075

61-
return [
62-
RuleErrorBuilder::message(
63-
sprintf('%s array key type %s.', $isSuperType->no() ? 'Invalid' : 'Possibly invalid', $dimensionType->describe(VerbosityLevel::typeOnly())),
64-
)->identifier('offsetAccess.invalidOffset')->build(),
65-
];
76+
return [];
6677
}
6778

6879
}

src/Rules/Arrays/InvalidKeyInArrayItemRule.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use PhpParser\Node;
66
use PHPStan\Analyser\Scope;
7+
use PHPStan\Php\PhpVersion;
78
use PHPStan\Rules\Rule;
89
use PHPStan\Rules\RuleErrorBuilder;
910
use PHPStan\Type\MixedType;
@@ -16,7 +17,10 @@
1617
class InvalidKeyInArrayItemRule implements Rule
1718
{
1819

19-
public function __construct(private bool $reportMaybes)
20+
public function __construct(
21+
private bool $reportMaybes,
22+
private PhpVersion $phpVersion,
23+
)
2024
{
2125
}
2226

@@ -47,6 +51,14 @@ public function processNode(Node $node, Scope $scope): array
4751
];
4852
}
4953

54+
if ($this->phpVersion->getVersionId() >= 80_100 && !$dimensionType->isFloat()->no()) {
55+
return [
56+
RuleErrorBuilder::message(
57+
'Using float as array key emits deprecation notice.',
58+
)->identifier('array.invalidKey')->build(),
59+
];
60+
}
61+
5062
return [];
5163
}
5264

tests/PHPStan/Rules/Arrays/InvalidKeyInArrayDimFetchRuleTest.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
namespace PHPStan\Rules\Arrays;
44

5+
use PHPStan\Php\PhpVersion;
56
use PHPStan\Rules\Rule;
67
use PHPStan\Rules\RuleLevelHelper;
78
use PHPStan\Testing\RuleTestCase;
9+
use function array_filter;
10+
use function array_values;
811
use const PHP_VERSION_ID;
912

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

2225
public function testInvalidKey(): void
2326
{
24-
$this->analyse([__DIR__ . '/data/invalid-key-array-dim-fetch.php'], [
27+
$this->analyse([__DIR__ . '/data/invalid-key-array-dim-fetch.php'], array_values(array_filter([
2528
[
2629
'Invalid array key type DateTimeImmutable.',
2730
7,
@@ -30,6 +33,12 @@ public function testInvalidKey(): void
3033
'Invalid array key type array.',
3134
8,
3235
],
36+
PHP_VERSION_ID >= 80_100
37+
? [
38+
'Using float as array key emits deprecation notice.',
39+
10,
40+
]
41+
: null,
3342
[
3443
'Possibly invalid array key type stdClass|string.',
3544
24,
@@ -58,7 +67,7 @@ public function testInvalidKey(): void
5867
'Invalid array key type DateTimeImmutable.',
5968
48,
6069
],
61-
]);
70+
])));
6271
}
6372

6473
public function testBug6315(): void

tests/PHPStan/Rules/Arrays/InvalidKeyInArrayItemRuleTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
namespace PHPStan\Rules\Arrays;
44

5+
use PHPStan\Php\PhpVersion;
56
use PHPStan\Rules\Rule;
67
use PHPStan\Testing\RuleTestCase;
8+
use function array_filter;
79
use const PHP_VERSION_ID;
810

911
/**
@@ -14,12 +16,12 @@ class InvalidKeyInArrayItemRuleTest extends RuleTestCase
1416

1517
protected function getRule(): Rule
1618
{
17-
return new InvalidKeyInArrayItemRule(true);
19+
return new InvalidKeyInArrayItemRule(true, new PhpVersion(PHP_VERSION_ID));
1820
}
1921

2022
public function testInvalidKey(): void
2123
{
22-
$this->analyse([__DIR__ . '/data/invalid-key-array-item.php'], [
24+
$this->analyse([__DIR__ . '/data/invalid-key-array-item.php'], array_filter([
2325
[
2426
'Invalid array key type DateTimeImmutable.',
2527
13,
@@ -32,7 +34,13 @@ public function testInvalidKey(): void
3234
'Possibly invalid array key type stdClass|string.',
3335
15,
3436
],
35-
]);
37+
PHP_VERSION_ID >= 80_100
38+
? [
39+
'Using float as array key emits deprecation notice.',
40+
16,
41+
]
42+
: null,
43+
]));
3644
}
3745

3846
public function testInvalidKeyInList(): void

tests/PHPStan/Rules/Arrays/data/invalid-key-array-item.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@
1313
new \DateTimeImmutable() => 'aaa',
1414
[] => 'bbb',
1515
$stringOrObject => 'aaa',
16+
1.0 => 'aaa',
1617
];

0 commit comments

Comments
 (0)