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
27 changes: 22 additions & 5 deletions src/Analyser/ExprHandler/AssignHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\Expr\ExistingArrayDimFetch;
use PHPStan\Node\Expr\GetOffsetValueTypeExpr;
use PHPStan\Node\Expr\IntertwinedVariableByReferenceWithExpr;
use PHPStan\Node\Expr\OriginalPropertyTypeExpr;
use PHPStan\Node\Expr\SetExistingOffsetValueTypeExpr;
use PHPStan\Node\Expr\SetOffsetValueTypeExpr;
use PHPStan\Node\Expr\TypeExpr;
Expand All @@ -52,6 +50,7 @@
use PHPStan\Node\VariableAssignNode;
use PHPStan\Node\VirtualNode;
use PHPStan\Php\PhpVersion;
use PHPStan\Rules\Properties\PropertyReflectionFinder;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Accessory\AccessoryArrayListType;
Expand All @@ -73,6 +72,7 @@
use PHPStan\Type\Type;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\UnionType;
use TypeError;
use function array_key_last;
use function array_merge;
Expand All @@ -97,6 +97,7 @@
private ExprPrinter $exprPrinter,
private MatchHandler $matchHandler,
private ExpressionResultFactory $expressionResultFactory,
private PropertyReflectionFinder $propertyReflectionFinder,
)
{
}
Expand Down Expand Up @@ -550,7 +551,7 @@
while ($var instanceof ArrayDimFetch) {
$varForSetOffsetValue = $var->var;
if ($varForSetOffsetValue instanceof PropertyFetch || $varForSetOffsetValue instanceof StaticPropertyFetch) {
$varForSetOffsetValue = new OriginalPropertyTypeExpr($varForSetOffsetValue);
$varForSetOffsetValue = new TypeExpr($this->getOriginalPropertyType($varForSetOffsetValue, $scope));
}

if (
Expand Down Expand Up @@ -962,7 +963,7 @@
} else {
$dimExpr = $arrayItem->key;
}
$getOffsetValueTypeExpr = new GetOffsetValueTypeExpr($assignedExpr, $dimExpr);
$getOffsetValueTypeExpr = new TypeExpr($scope->getType($assignedExpr)->getOffsetValueType($scope->getType($dimExpr)));
$result = $this->processAssignVar(
$nodeScopeResolver,
$scope,
Expand All @@ -987,7 +988,7 @@
while ($var instanceof ExistingArrayDimFetch) {
$varForSetOffsetValue = $var->getVar();
if ($varForSetOffsetValue instanceof PropertyFetch || $varForSetOffsetValue instanceof StaticPropertyFetch) {
$varForSetOffsetValue = new OriginalPropertyTypeExpr($varForSetOffsetValue);
$varForSetOffsetValue = new TypeExpr($this->getOriginalPropertyType($varForSetOffsetValue, $scope));
}
$assignedPropertyExpr = new SetExistingOffsetValueTypeExpr(
$varForSetOffsetValue,
Expand Down Expand Up @@ -1645,4 +1646,20 @@
return false;
}

/**
* Returns the property's readable (declared) type, filtered down to the union
* members that are not disjoint from the currently narrowed property type.
*/
private function getOriginalPropertyType(PropertyFetch|StaticPropertyFetch $propertyFetch, MutatingScope $scope): Type
{
$propertyReflection = $this->propertyReflectionFinder->findPropertyReflectionFromNode($propertyFetch, $scope);
$originalPropertyType = $propertyReflection !== null ? $propertyReflection->getReadableType() : new ErrorType();
if ($originalPropertyType instanceof UnionType) {
$currentPropertyType = $scope->getType($propertyFetch);
$originalPropertyType = $originalPropertyType->filterTypes(static fn (Type $innerType) => !$innerType->isSuperTypeOf($currentPropertyType)->no());

Check warning on line 1659 in src/Analyser/ExprHandler/AssignHandler.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.4, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ $originalPropertyType = $propertyReflection !== null ? $propertyReflection->getReadableType() : new ErrorType(); if ($originalPropertyType instanceof UnionType) { $currentPropertyType = $scope->getType($propertyFetch); - $originalPropertyType = $originalPropertyType->filterTypes(static fn (Type $innerType) => !$innerType->isSuperTypeOf($currentPropertyType)->no()); + $originalPropertyType = $originalPropertyType->filterTypes(static fn (Type $innerType) => !$currentPropertyType->isSuperTypeOf($innerType)->no()); } return $originalPropertyType;

Check warning on line 1659 in src/Analyser/ExprHandler/AssignHandler.php

View workflow job for this annotation

GitHub Actions / Mutation Testing (8.3, ubuntu-latest)

Escaped Mutant for Mutator "PHPStan\Infection\IsSuperTypeOfCalleeAndArgumentMutator": @@ @@ $originalPropertyType = $propertyReflection !== null ? $propertyReflection->getReadableType() : new ErrorType(); if ($originalPropertyType instanceof UnionType) { $currentPropertyType = $scope->getType($propertyFetch); - $originalPropertyType = $originalPropertyType->filterTypes(static fn (Type $innerType) => !$innerType->isSuperTypeOf($currentPropertyType)->no()); + $originalPropertyType = $originalPropertyType->filterTypes(static fn (Type $innerType) => !$currentPropertyType->isSuperTypeOf($innerType)->no()); } return $originalPropertyType;
}

return $originalPropertyType;
}

}
64 changes: 0 additions & 64 deletions src/Analyser/ExprHandler/Virtual/GetIterableKeyTypeExprHandler.php

This file was deleted.

This file was deleted.

64 changes: 0 additions & 64 deletions src/Analyser/ExprHandler/Virtual/GetOffsetValueTypeExprHandler.php

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@
use PHPStan\Analyser\TypeSpecifier;
use PHPStan\Analyser\TypeSpecifierContext;
use PHPStan\DependencyInjection\AutowiredService;
use PHPStan\Node\Expr\OriginalPropertyTypeExpr;
use PHPStan\Node\Expr\SetExistingOffsetValueTypeExpr;
use PHPStan\Type\Type;
use PHPStan\Type\UnionType;

/**
* @implements ExprHandler<SetExistingOffsetValueTypeExpr>
Expand Down Expand Up @@ -55,14 +53,7 @@ public function processExpr(NodeScopeResolver $nodeScopeResolver, Stmt $stmt, Ex

public function resolveType(MutatingScope $scope, Expr $expr): Type
{
$varNode = $expr->getVar();
$varType = $scope->getType($varNode);
if ($varNode instanceof OriginalPropertyTypeExpr) {
$currentPropertyType = $scope->getType($varNode->getPropertyFetch());
if ($varType instanceof UnionType) {
$varType = $varType->filterTypes(static fn (Type $innerType) => !$innerType->isSuperTypeOf($currentPropertyType)->no());
}
}
$varType = $scope->getType($expr->getVar());
return $varType->setExistingOffsetValueType(
$scope->getType($expr->getDim()),
$scope->getType($expr->getValue()),
Expand Down
Loading
Loading