Skip to content
Closed
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
10 changes: 10 additions & 0 deletions src/Php/PhpVersion.php
Original file line number Diff line number Diff line change
Expand Up @@ -414,4 +414,14 @@ public function hasPDOSubclasses(): bool
return $this->versionId >= 80400;
}

public function deprecatesImplicitlyFloatConversionToInt(): bool
{
return $this->versionId >= 80100;
}

public function deprecatesNullArrayOffset(): bool
{
return $this->versionId >= 80500;
}

}
18 changes: 13 additions & 5 deletions src/Rules/Arrays/AllowedArrayKeysTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Rules\Arrays;

use PHPStan\Php\PhpVersion;
use PHPStan\Type\ArrayType;
use PHPStan\Type\BooleanType;
use PHPStan\Type\Constant\ConstantBooleanType;
Expand All @@ -21,15 +22,22 @@
final class AllowedArrayKeysTypes
{

public static function getType(): Type
public static function getType(PhpVersion $phpVersion): Type
{
return new UnionType([
$allowedTypes = [
new IntegerType(),
new StringType(),
new FloatType(),
new BooleanType(),
new NullType(),
]);
];

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

return new UnionType($allowedTypes);
}

public static function narrowOffsetKeyType(Type $varType, Type $keyType): ?Type
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 @@ -6,6 +6,7 @@
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\AutowiredParameter;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\Rules\RuleLevelHelper;
Expand All @@ -23,6 +24,7 @@ final class InvalidKeyInArrayDimFetchRule implements Rule

public function __construct(
private RuleLevelHelper $ruleLevelHelper,
private PhpVersion $phpVersion,
#[AutowiredParameter]
private bool $reportMaybes,
)
Expand Down Expand Up @@ -56,17 +58,18 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

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

$isSuperType = AllowedArrayKeysTypes::getType()->isSuperTypeOf($dimensionType);
$isSuperType = AllowedArrayKeysTypes::getType($phpVersion)->isSuperTypeOf($dimensionType);
if ($isSuperType->yes() || ($isSuperType->maybe() && !$this->reportMaybes)) {
return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Rules\Arrays;

use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleLevelHelper;
use PHPStan\Testing\RuleTestCase;
Expand All @@ -16,7 +17,11 @@ class InvalidKeyInArrayDimFetchRuleTest extends RuleTestCase
protected function getRule(): Rule
{
$ruleLevelHelper = new RuleLevelHelper(self::createReflectionProvider(), true, false, true, true, true, false, true);
return new InvalidKeyInArrayDimFetchRule($ruleLevelHelper, true);
return new InvalidKeyInArrayDimFetchRule(
$ruleLevelHelper,
self::getContainer()->getByType(PhpVersion::class),
true
);
}

public function testInvalidKey(): void
Expand Down
Loading