Skip to content
Merged
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
17 changes: 7 additions & 10 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@
use PHPStan\Type\ObjectType;
use PHPStan\Type\ObjectWithoutClassType;
use PHPStan\Type\StaticType;
use PHPStan\Type\StaticTypeFactory;
use PHPStan\Type\StringType;
use PHPStan\Type\ThisType;
use PHPStan\Type\Type;
Expand Down Expand Up @@ -3349,26 +3350,22 @@ public function specifyExpressionType(Expr $expr, Type $type, Type $nativeType,
if ($dimType->isInteger()->yes() || $dimType->isString()->yes()) {
$exprVarType = $scope->getType($expr->var);
if (!$exprVarType instanceof MixedType && !$exprVarType->isArray()->no()) {
$types = [
new ArrayType(new MixedType(), new MixedType()),
new ObjectType(ArrayAccess::class),
new NullType(),
];
if ($dimType->isInteger()->yes()) {
$types[] = new StringType();
$varType = TypeCombinator::intersect($exprVarType, StaticTypeFactory::intOffsetAccessibleType());
} else {
$varType = TypeCombinator::intersect($exprVarType, StaticTypeFactory::generalOffsetAccessibleType());
}
$offsetValueType = TypeCombinator::intersect($exprVarType, TypeCombinator::union(...$types));

if ($dimType instanceof ConstantIntegerType || $dimType instanceof ConstantStringType) {
$offsetValueType = TypeCombinator::intersect(
$offsetValueType,
$varType = TypeCombinator::intersect(
$varType,
new HasOffsetValueType($dimType, $type),
);
}

$scope = $scope->specifyExpressionType(
$expr->var,
$offsetValueType,
$varType,
$scope->getNativeType($expr->var),
$certainty,
);
Expand Down
19 changes: 2 additions & 17 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,6 @@
/** @var array<string, MutatingScope|null> */
private array $calledMethodResults = [];

private ?Type $nonIntKeyOffsetValueType = null;

private ?Type $intKeyOffsetValueType = null;

/**
* @param string[][] $earlyTerminatingMethodCalls className(string) => methods(string[])
* @param array<int, string> $earlyTerminatingFunctionCalls
Expand Down Expand Up @@ -2192,7 +2188,7 @@
$hasYield = false;
$throwPoints = [];
$impurePoints = [];
foreach ($stmt->consts as $const) {

Check warning on line 2191 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / Check for typos

"consts" should be "constants".
$this->callNodeCallback($nodeCallback, $const, $scope, $storage);
$constResult = $this->processExprNode($stmt, $const->value, $scope, $storage, $nodeCallback, ExpressionContext::createDeep());
$impurePoints = array_merge($impurePoints, $constResult->getImpurePoints());
Expand All @@ -2208,7 +2204,7 @@
$throwPoints = [];
$impurePoints = [];
$this->processAttributeGroups($stmt, $stmt->attrGroups, $scope, $storage, $nodeCallback);
foreach ($stmt->consts as $const) {

Check warning on line 2207 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / Check for typos

"consts" should be "constants".
$this->callNodeCallback($nodeCallback, $const, $scope, $storage);
$constResult = $this->processExprNode($stmt, $const->value, $scope, $storage, $nodeCallback, ExpressionContext::createDeep());
$impurePoints = array_merge($impurePoints, $constResult->getImpurePoints());
Expand Down Expand Up @@ -6610,21 +6606,10 @@
!$offsetValueType instanceof MixedType
&& !$offsetValueType->isArray()->yes()
) {
$this->nonIntKeyOffsetValueType ??= TypeCombinator::union(
new ArrayType(new MixedType(), new MixedType()),
new ObjectType(ArrayAccess::class),
new NullType(),
);

if ($offsetType !== null && $offsetType->isInteger()->yes()) {
$this->intKeyOffsetValueType ??= TypeCombinator::union(
$this->nonIntKeyOffsetValueType,
new StringType(),
);

$offsetValueType = TypeCombinator::intersect($offsetValueType, $this->intKeyOffsetValueType);
$offsetValueType = TypeCombinator::intersect($offsetValueType, StaticTypeFactory::intOffsetAccessibleType());
} else {
$offsetValueType = TypeCombinator::intersect($offsetValueType, $this->nonIntKeyOffsetValueType);
$offsetValueType = TypeCombinator::intersect($offsetValueType, StaticTypeFactory::generalOffsetAccessibleType());
}
}

Expand Down
34 changes: 32 additions & 2 deletions src/Type/StaticTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace PHPStan\Type;

use ArrayAccess;
use PHPStan\Type\Constant\ConstantArrayType;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\Constant\ConstantFloatType;
Expand All @@ -16,15 +17,15 @@ public static function falsey(): Type
static $falsey;

if ($falsey === null) {
$falsey = new UnionType([
$falsey = TypeCombinator::union(
new NullType(),
new ConstantBooleanType(false),
new ConstantIntegerType(0),
new ConstantFloatType(0.0),
new ConstantStringType(''),
new ConstantStringType('0'),
new ConstantArrayType([], []),
]);
);
}

return $falsey;
Expand All @@ -41,4 +42,33 @@ public static function truthy(): Type
return $truthy;
}

public static function generalOffsetAccessibleType(): Type
{
static $generalOffsetAccessible;

if ($generalOffsetAccessible === null) {
$generalOffsetAccessible = TypeCombinator::union(
new ArrayType(new MixedType(), new MixedType()),
new ObjectType(ArrayAccess::class),
new NullType(),
);
}

return $generalOffsetAccessible;
}

public static function intOffsetAccessibleType(): Type
{
static $intOffsetAccessible;

if ($intOffsetAccessible === null) {
$intOffsetAccessible = TypeCombinator::union(
self::generalOffsetAccessibleType(),
new StringType(),
);
}

return $intOffsetAccessible;
}

}
Loading