Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,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
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,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
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
7 changes: 5 additions & 2 deletions src/Rules/Arrays/InvalidKeyInArrayDimFetchRule.php
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 @@ -59,17 +61,18 @@ public function processNode(Node $node, Scope $scope): array
}

$phpVersion = $this->phpVersion;
$allowFloatBoolNullAsArrayKey = $this->allowFloatBoolNullAsArrayKey;
$dimensionType = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$node->dim,
'',
static fn (Type $dimType): bool => AllowedArrayKeysTypes::getType($phpVersion)->isSuperTypeOf($dimType)->yes(),
static fn (Type $dimType): bool => AllowedArrayKeysTypes::getType($phpVersion, $allowFloatBoolNullAsArrayKey)->isSuperTypeOf($dimType)->yes(),
)->getType();
if ($dimensionType instanceof ErrorType) {
return [];
}

$isSuperType = AllowedArrayKeysTypes::getType($phpVersion)->isSuperTypeOf($dimensionType);
$isSuperType = AllowedArrayKeysTypes::getType($phpVersion, $allowFloatBoolNullAsArrayKey)->isSuperTypeOf($dimensionType);
if ($isSuperType->yes() || ($isSuperType->maybe() && !$this->reportMaybes)) {
return [];
}
Expand Down
8 changes: 6 additions & 2 deletions 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\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 @@ -40,17 +43,18 @@ public function processNode(Node $node, Scope $scope): array
}

$phpVersion = $this->phpVersion;
$allowFloatBoolNullAsArrayKey = $this->allowFloatBoolNullAsArrayKey;
$dimensionType = $this->ruleLevelHelper->findTypeToCheck(
$scope,
$node->key,
'',
static fn (Type $dimType): bool => AllowedArrayKeysTypes::getType($phpVersion)->isSuperTypeOf($dimType)->yes(),
static fn (Type $dimType): bool => AllowedArrayKeysTypes::getType($phpVersion, $allowFloatBoolNullAsArrayKey)->isSuperTypeOf($dimType)->yes(),
)->getType();
if ($dimensionType instanceof ErrorType) {
return [];
}

$isSuperType = AllowedArrayKeysTypes::getType($phpVersion)->isSuperTypeOf($dimensionType);
$isSuperType = AllowedArrayKeysTypes::getType($phpVersion, $allowFloatBoolNullAsArrayKey)->isSuperTypeOf($dimensionType);
if ($isSuperType->yes()) {
return [];
}
Expand Down
19 changes: 19 additions & 0 deletions tests/PHPStan/Rules/Arrays/InvalidKeyInArrayDimFetchRuleTest.php
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
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
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
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