Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Introduce allowFloatBoolNullAsArrayKey parameter #4012

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

Open
VincentLanglet wants to merge 3 commits into phpstan:2.1.x
base: 2.1.x
Choose a base branch
Loading
from VincentLanglet:reportCastedArrayKey
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
1 change: 1 addition & 0 deletions conf/config.neon
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ parameters:
reportStaticMethodSignatures: false
reportWrongPhpDocTypeInVarTag: false
reportAnyTypeWideningInVarTag: false
allowFloatBoolNullAsArrayKey: true
reportPossiblyNonexistentGeneralArrayOffset: false
reportPossiblyNonexistentConstantArrayOffset: false
checkMissingOverrideMethodAttribute: false
Expand Down
1 change: 1 addition & 0 deletions conf/parametersSchema.neon
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ parametersSchema:
reportStaticMethodSignatures: bool()
reportWrongPhpDocTypeInVarTag: bool()
reportAnyTypeWideningInVarTag: bool()
allowFloatBoolNullAsArrayKey: bool()
reportPossiblyNonexistentGeneralArrayOffset: bool()
reportPossiblyNonexistentConstantArrayOffset: bool()
checkMissingOverrideMethodAttribute: bool()
Expand Down
17 changes: 10 additions & 7 deletions src/Rules/Arrays/AllowedArrayKeysTypes.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,22 @@
final class AllowedArrayKeysTypes
{

public static function getType(?PhpVersion $phpVersion = null): Type
public static function getType(?PhpVersion $phpVersion = null, bool $allowFloatBoolNull = true): Type
{
$types = [
new IntegerType(),
new StringType(),
new BooleanType(),
];

if ($phpVersion === null || !$phpVersion->deprecatesImplicitlyFloatConversionToInt()) {
$types[] = new FloatType();
}
if ($phpVersion === null || !$phpVersion->deprecatesNullArrayOffset()) {
$types[] = new NullType();
if ($allowFloatBoolNull) {
$types[] = new BooleanType();

if ($phpVersion === null || !$phpVersion->deprecatesImplicitlyFloatConversionToInt()) {
$types[] = new FloatType();
}
if ($phpVersion === null || !$phpVersion->deprecatesNullArrayOffset()) {
$types[] = new NullType();
}
}

return new UnionType($types);
Expand Down
4 changes: 3 additions & 1 deletion src/Rules/Arrays/InvalidKeyInArrayDimFetchRule.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public function __construct(
private PhpVersion $phpVersion,
#[AutowiredParameter]
private bool $reportMaybes,
#[AutowiredParameter]
private bool $allowFloatBoolNullAsArrayKey,
)
{
}
Expand Down Expand Up @@ -58,7 +60,7 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$allowedArrayKeys = AllowedArrayKeysTypes::getType($this->phpVersion);
$allowedArrayKeys = AllowedArrayKeysTypes::getType($this->phpVersion, $this->allowFloatBoolNullAsArrayKey);
$dimensionType = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$node->dim,
Expand Down
5 changes: 4 additions & 1 deletion src/Rules/Arrays/InvalidKeyInArrayItemRule.php
View file Open in desktop
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\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
Expand All @@ -24,6 +25,8 @@ final class InvalidKeyInArrayItemRule implements Rule
public function __construct(
private RuleLevelHelper $ruleLevelHelper,
private PhpVersion $phpVersion,
#[AutowiredParameter]
private bool $allowFloatBoolNullAsArrayKey,
)
{
}
Expand All @@ -39,7 +42,7 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$allowedArrayKeys = AllowedArrayKeysTypes::getType($this->phpVersion);
$allowedArrayKeys = AllowedArrayKeysTypes::getType($this->phpVersion, $this->allowFloatBoolNullAsArrayKey);
$dimensionType = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$node->key,
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/Arrays/InvalidKeyInArrayDimFetchRuleTest.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
class InvalidKeyInArrayDimFetchRuleTest extends RuleTestCase
{

private bool $allowFloatBoolNullAsArrayKey = true;

protected function getRule(): Rule
{
$ruleLevelHelper = new RuleLevelHelper(self::createReflectionProvider(), true, false, true, true, true, false, true);
return new InvalidKeyInArrayDimFetchRule(
$ruleLevelHelper,
self::getContainer()->getByType(PhpVersion::class),
true,
$this->allowFloatBoolNullAsArrayKey,
);
}

Expand Down Expand Up @@ -163,4 +166,20 @@ public function testBug12981(): void
]);
}

public function testUnsetFalseKey(): void
{
$this->allowFloatBoolNullAsArrayKey = false;

$this->analyse([__DIR__ . '/data/unset-false-key.php'], [
[
'Invalid array key type false.',
6,
],
[
'Invalid array key type false.',
13,
],
]);
}

}
35 changes: 35 additions & 0 deletions tests/PHPStan/Rules/Arrays/InvalidKeyInArrayItemRuleTest.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
class InvalidKeyInArrayItemRuleTest extends RuleTestCase
{

private bool $allowFloatBoolNullAsArrayKey = true;

private bool $checkExplicitMixed = false;

private bool $checkImplicitMixed = false;
Expand All @@ -26,6 +28,7 @@ protected function getRule(): Rule
return new InvalidKeyInArrayItemRule(
$ruleLevelHelper,
self::getContainer()->getByType(PhpVersion::class),
$this->allowFloatBoolNullAsArrayKey,
);
}

Expand Down Expand Up @@ -102,6 +105,38 @@ public function testInvalidMixedKey(): void
$this->analyse([__DIR__ . '/data/invalid-key-array-item.php'], $errors);
}

public function testInvalidKeyReportingCastedArrayKey(): void
{
$this->allowFloatBoolNullAsArrayKey = false;

$this->analyse([__DIR__ . '/data/invalid-key-array-item.php'], [
[
'Invalid array key type DateTimeImmutable.',
12,
],
[
'Invalid array key type array.',
13,
],
[
'Possibly invalid array key type stdClass|string.',
14,
],
[
'Invalid array key type float.',
26,
],
[
'Invalid array key type null.',
27,
],
[
'Invalid array key type false.',
31,
],
]);
}

public function testInvalidKeyInList(): void
{
$this->analyse([__DIR__ . '/data/invalid-key-list.php'], [
Expand Down
4 changes: 4 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/invalid-key-array-item.php
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,7 @@
1.0 => 'aaa',
null => 'aaa',
];

$d = [
false => 'aaa',
];
16 changes: 16 additions & 0 deletions tests/PHPStan/Rules/Arrays/data/unset-false-key.php
View file Open in desktop
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php declare(strict_types=1);

namespace UnsetFalseKey;

/** @var array<int, int> $data */
unset($data[false]);

function test_remove_element(): void {
$modified = [1, 4, 6, 8];

// this would happen in the SUT
unset($modified[array_search(4, $modified, true)]);
unset($modified[array_search(5, $modified, true)]); // bug is here - will unset key `0` by accident

assert([1, 6, 8] === $modified); // actually is [6, 8]
}
Loading

AltStyle によって変換されたページ (->オリジナル) /