Skip to content

Introduce Type->getConstantArrayKeys() #4171

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

Closed
wants to merge 2 commits into from
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
8 changes: 1 addition & 7 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ parameters:
-
message: '#^Doing instanceof PHPStan\\Type\\Constant\\ConstantStringType is error\-prone and deprecated\. Use Type\:\:getConstantStrings\(\) instead\.$#'
identifier: phpstanApi.instanceofType
count: 3
count: 2
path: src/Analyser/MutatingScope.php

-
Expand All @@ -48,12 +48,6 @@ parameters:
count: 2
path: src/Analyser/NodeScopeResolver.php

-
message: '#^Doing instanceof PHPStan\\Type\\Constant\\ConstantStringType is error\-prone and deprecated\. Use Type\:\:getConstantStrings\(\) instead\.$#'
identifier: phpstanApi.instanceofType
count: 1
path: src/Analyser/NodeScopeResolver.php

-
message: '#^Parameter \#2 \$node of method PHPStan\\BetterReflection\\SourceLocator\\Ast\\Strategy\\NodeToReflection\:\:__invoke\(\) expects PhpParser\\Node\\Expr\\ArrowFunction\|PhpParser\\Node\\Expr\\Closure\|PhpParser\\Node\\Expr\\FuncCall\|PhpParser\\Node\\Stmt\\Class_\|PhpParser\\Node\\Stmt\\Const_\|PhpParser\\Node\\Stmt\\Enum_\|PhpParser\\Node\\Stmt\\Function_\|PhpParser\\Node\\Stmt\\Interface_\|PhpParser\\Node\\Stmt\\Trait_, PhpParser\\Node\\Stmt\\ClassLike given\.$#'
identifier: argument.type
Expand Down
22 changes: 18 additions & 4 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -4229,29 +4229,43 @@ public function specifyExpressionType(Expr $expr, Type $type, Type $nativeType,

$scope = $this;
if ($expr instanceof Expr\ArrayDimFetch && $expr->dim !== null) {
$dimType = $scope->getType($expr->dim)->toArrayKey();
if ($dimType instanceof ConstantIntegerType || $dimType instanceof ConstantStringType) {
$dimTypes = $scope->getType($expr->dim)->getConstantArrayKeys();
Copy link
Contributor

Choose a reason for hiding this comment

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

Btw @staabm this change is wrong

toArrayKey does the cast
getConstantArrayKeys only return the constants.

So NullType->toArrayKey was ConstantString('')
And NullType::getConstantArrayKeys returns []

You will need

$scope->getType($expr->dim)->toArrayKey->getConstantArrayKeys();

Which make the method less useful. (it becomes the same than ->toArrayKey->getConstantScalarValues(), unless for static analysis).

Or maybe the method you need is toConstantArrayKeys...


if ($dimTypes !== []) {
$exprVarType = $scope->getType($expr->var);
if (!$exprVarType instanceof MixedType && !$exprVarType->isArray()->no()) {
$hasOffsets = [];
$containsInteger = false;
foreach ($dimTypes as $dimType) {
$hasOffsets[] = new HasOffsetValueType($dimType, $type);

if (!($dimType instanceof ConstantIntegerType)) {
continue;
}

$containsInteger = true;
}

$types = [
new ArrayType(new MixedType(), new MixedType()),
new ObjectType(ArrayAccess::class),
new NullType(),
];
if ($dimType instanceof ConstantIntegerType) {
if ($containsInteger) {
$types[] = new StringType();
}

$scope = $scope->specifyExpressionType(
$expr->var,
TypeCombinator::intersect(
TypeCombinator::intersect($exprVarType, TypeCombinator::union(...$types)),
new HasOffsetValueType($dimType, $type),
...$hasOffsets,
),
$scope->getNativeType($expr->var),
$certainty,
);
}

}
}

Expand Down
11 changes: 7 additions & 4 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@
use PHPStan\Type\Constant\ConstantArrayTypeBuilder;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantIntegerType;
use PHPStan\Type\Constant\ConstantStringType;
use PHPStan\Type\ErrorType;
use PHPStan\Type\FileTypeMapper;
use PHPStan\Type\GeneralizePrecision;
Expand Down Expand Up @@ -6053,15 +6052,19 @@ private function produceArrayDimFetchAssignValueToWrite(array $dimFetchStack, ar
&& $scope->hasExpressionType($arrayDimFetch)->yes()
) {
$hasOffsetType = null;
if ($offsetType instanceof ConstantStringType || $offsetType instanceof ConstantIntegerType) {
$hasOffsetType = new HasOffsetValueType($offsetType, $valueToWrite);
$offsetConstantArrayKeys = $offsetType->getConstantArrayKeys();
if ($offsetConstantArrayKeys !== []) {
$hasOffsetType = [];
foreach ($offsetConstantArrayKeys as $constantArrayKey) {
$hasOffsetType[] = new HasOffsetValueType($constantArrayKey, $valueToWrite);
}
}
$valueToWrite = $offsetValueType->setExistingOffsetValueType($offsetType, $valueToWrite);

if ($hasOffsetType !== null) {
$valueToWrite = TypeCombinator::intersect(
$valueToWrite,
$hasOffsetType,
...$hasOffsetType,
);
} elseif ($valueToWrite->isArray()->yes()) {
$valueToWrite = TypeCombinator::intersect(
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryArrayListType.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,11 @@ public function getConstantArrays(): array
return [];
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryLiteralStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public function getObjectClassReflections(): array
return [];
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryLowercaseStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public function getObjectClassReflections(): array
return [];
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNonEmptyStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ public function getObjectClassReflections(): array
return [];
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNonFalsyStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public function getObjectClassReflections(): array
return [];
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryNumericStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public function getObjectClassReflections(): array
return [];
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/AccessoryUppercaseStringType.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public function getObjectClassReflections(): array
return [];
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasOffsetType.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ public function getObjectClassReflections(): array
return [];
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasOffsetValueType.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ public function getObjectClassReflections(): array
return [];
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/HasPropertyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public function getObjectClassReflections(): array
return [];
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/NonEmptyArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ public function getConstantArrays(): array
return [];
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/Accessory/OversizedArrayType.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ public function getConstantArrays(): array
return [];
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/BooleanType.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public function getConstantStrings(): array
return [];
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantScalarTypes(): array
{
return [new ConstantBooleanType(true), new ConstantBooleanType(false)];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/CallableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ public function getObjectClassReflections(): array
return [];
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/ClosureType.php
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,11 @@ public function getConstant(string $constantName): ClassConstantReflection
return $this->objectType->getConstant($constantName);
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/FloatType.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ public function getObjectClassReflections(): array
return [];
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/IntegerRangeType.php
Original file line number Diff line number Diff line change
Expand Up @@ -693,6 +693,11 @@ public function exponentiate(Type $exponent): Type
return parent::exponentiate($exponent);
}

public function getConstantArrayKeys(): array
{
return $this->getFiniteTypes();
}

/**
* @return list<ConstantIntegerType>
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Type/IntegerType.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public function describe(VerbosityLevel $level): string
return 'int';
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
12 changes: 12 additions & 0 deletions src/Type/IntersectionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,18 @@ public function getConstantArrays(): array
return $constantArrays;
}

public function getConstantArrayKeys(): array
{
$arrayKeys = [];
foreach ($this->types as $type) {
foreach ($type->getConstantArrayKeys() as $string) {
$arrayKeys[] = $string;
}
}

return $arrayKeys;
}

public function getConstantStrings(): array
{
$strings = [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/IterableType.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ public function getObjectClassReflections(): array
return [];
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/MixedType.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public function getConstantArrays(): array
return [];
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/NeverType.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public function getObjectClassReflections(): array
return [];
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/NonexistentParentClassType.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ public function getConstant(string $constantName): ClassConstantReflection
throw new ShouldNotHappenException();
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
5 changes: 5 additions & 0 deletions src/Type/NullType.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public function getObjectClassReflections(): array
return [];
}

public function getConstantArrayKeys(): array
{
return [];
}

public function getConstantStrings(): array
{
return [];
Expand Down
Loading
Loading