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
22 changes: 19 additions & 3 deletions src/Analyser/TypeSpecifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2844,7 +2844,7 @@
if ($rightType instanceof ConstantStringType && $this->reflectionProvider->hasClass($rightType->getValue())) {
return $this->create(
$unwrappedLeftExpr->getArgs()[0]->value,
new ObjectType($rightType->getValue(), classReflection: $this->reflectionProvider->getClass($rightType->getValue())->asFinal()),
$this->determineExactClassType($scope, $unwrappedLeftExpr->getArgs()[0]->value, $rightType->getValue()),
$context,
$scope,
)->unionWith($this->create($leftExpr, $rightType, $context, $scope))->setRootExpr($expr);
Expand Down Expand Up @@ -2969,7 +2969,7 @@
if ($this->reflectionProvider->hasClass($rightType->getValue())) {
return $this->create(
$unwrappedLeftExpr->class,
new ObjectType($rightType->getValue(), classReflection: $this->reflectionProvider->getClass($rightType->getValue())->asFinal()),
$this->determineExactClassType($scope, $unwrappedLeftExpr->class, $rightType->getValue()),
$context,
$scope,
)->unionWith($this->create($leftExpr, $rightType, $context, $scope))->setRootExpr($expr);
Expand Down Expand Up @@ -3000,7 +3000,7 @@
if ($this->reflectionProvider->hasClass($leftType->getValue())) {
return $this->create(
$unwrappedRightExpr->class,
new ObjectType($leftType->getValue(), classReflection: $this->reflectionProvider->getClass($leftType->getValue())->asFinal()),
$this->determineExactClassType($scope, $unwrappedRightExpr->class, $leftType->getValue()),
$context,
$scope,
)->unionWith($this->create($rightExpr, $leftType, $context, $scope)->setRootExpr($expr));
Expand Down Expand Up @@ -3123,4 +3123,20 @@
return (new SpecifiedTypes([], []))->setRootExpr($expr);
}

private function determineExactClassType(Scope $scope, Expr $exprNode, string $className): Type
{
$exprType = $scope->getType($exprNode);
$classReflection = $this->reflectionProvider->getClass($className)->asFinal();
$asFinalType = new ObjectType($className, classReflection: $classReflection);

$plainType = new ObjectType($className);
$narrowed = TypeCombinator::intersect($exprType, $plainType);

if ($plainType->isSuperTypeOf($narrowed)->yes() && !$narrowed->isSuperTypeOf($plainType)->yes()) {

Check warning on line 3135 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $plainType = new ObjectType($className); $narrowed = TypeCombinator::intersect($exprType, $plainType); - if ($plainType->isSuperTypeOf($narrowed)->yes() && !$narrowed->isSuperTypeOf($plainType)->yes()) { + if (!$plainType->isSuperTypeOf($narrowed)->no() && !$narrowed->isSuperTypeOf($plainType)->yes()) { return $narrowed; }

Check warning on line 3135 in src/Analyser/TypeSpecifier.php

View workflow job for this annotation

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

Escaped Mutant for Mutator "PHPStan\Infection\TrinaryLogicMutator": @@ @@ $plainType = new ObjectType($className); $narrowed = TypeCombinator::intersect($exprType, $plainType); - if ($plainType->isSuperTypeOf($narrowed)->yes() && !$narrowed->isSuperTypeOf($plainType)->yes()) { + if (!$plainType->isSuperTypeOf($narrowed)->no() && !$narrowed->isSuperTypeOf($plainType)->yes()) { return $narrowed; }
return $narrowed;
}

return $asFinalType;
}

}
76 changes: 76 additions & 0 deletions tests/PHPStan/Analyser/nsrt/bug-14413.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php // lint >= 8.0

declare(strict_types = 1);

namespace Bug14413;

use function PHPStan\Testing\assertType;

/** @template T */
abstract class Animal {
/** @return T */
abstract public function value(): mixed;
}

/**
* @template T
* @extends Animal<T>
*/
class Cat extends Animal {
/** @param T $val */
public function __construct(private mixed $val) {}
/** @return T */
public function value(): mixed { return $this->val; }
}

/**
* @template T
* @extends Animal<T>
*/
class Dog extends Animal {
/** @return never */
public function value(): never { throw new \RuntimeException(); }
}

/** @param Cat<string>|Dog<string> $a */
function unionMatchPreservesGeneric(Animal $a): void {
match ($a::class) {
Cat::class => assertType('string', $a->value()),
Dog::class => assertType('never', $a->value()),
};
}

/** @param Cat<int>|Dog<int> $a */
function ifElseClassPreservesGeneric(Animal $a): void {
if ($a::class === Cat::class) {
assertType('Bug14413\Cat<int>', $a);
assertType('int', $a->value());
} else {
assertType('int', $a->value());
}
}

/** @param Cat<float>|Dog<float> $a */
function mirrorCasePreservesGeneric(Animal $a): void {
if (Cat::class === $a::class) {
assertType('float', $a->value());
}
}

/** @param Cat<array<string>>|Dog<array<string>> $a */
function matchWithMethodCall(Animal $a): void {
$result = match ($a::class) {
Cat::class => $a->value(),
Dog::class => [],
};
assertType('array<string>', $result);
}

/** @param Cat<string>|Dog<string> $a */
function nonMatchingClass(Animal $a): void {
if ($a::class === \stdClass::class) {
assertType('*NEVER*', $a);
} else {
assertType('Bug14413\Cat<string>|Bug14413\Dog<string>', $a);
}
}
Loading