Skip to content
Open
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
28 changes: 27 additions & 1 deletion src/PhpDoc/ResolvedPhpDocBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\ShouldNotHappenException;
use PHPStan\Turbo\ReferencedByTurboExtension;
use PHPStan\Type\ConditionalType;
use PHPStan\Type\ConditionalTypeForParameter;
use PHPStan\Type\Generic\TemplateTypeHelper;
use PHPStan\Type\Generic\TemplateTypeMap;
Expand Down Expand Up @@ -1249,7 +1250,32 @@ private static function resolveTemplateTypeInTag(
$classReflection->getCallSiteVarianceMap(),
$positionVariance,
);
return $tag->withType($type);
return $tag->withType(self::resolveConditionalTypes($type));
}

/**
* A conditional type on the parent's template type can be decided by the child's
* type arguments. It stays when its subject is static, which depends on the called
* class, or when it resolves to another conditional type, including one on a
* parameter, which the parent's conditional type is compared with branch by branch.
*/
private static function resolveConditionalTypes(Type $type): Type
{
if (!$type->hasTemplateOrLateResolvableType()) {
return $type;
}

return TypeTraverser::map($type, static function (Type $type, callable $traverse): Type {
$type = $traverse($type);
if ($type instanceof ConditionalType && !$type->getSubject() instanceof StaticType && $type->isResolvable()) {
$resolved = $type->resolve();
if (!$resolved instanceof ConditionalType && !$resolved instanceof ConditionalTypeForParameter) {
return $resolved;
}
}

return $type;
});
}

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

declare(strict_types = 1);

namespace Bug15128Nsrt;

use function PHPStan\Testing\assertType;

/**
* @template T of int|string
*/
class A
{

/**
* @return (T is int ? string : int)
*/
public function foo(): int|string
{
return 1;
}

}

/**
* @extends A<string>
*/
class B extends A
{

public function foo(): int
{
return 1;
}

}

/**
* @template U of int|string
* @extends A<U>
*/
class C extends A
{

public function foo(): int|string
{
return 1;
}

}

interface Foo
{

}

class Base
{

/**
* @return (static is Foo ? int : string)
*/
public function bar(): int|string
{
return 1;
}

}

class Child extends Base
{

public function bar(): int|string
{
return 1;
}

}

class GrandChild extends Child implements Foo
{

}

/**
* @param C<int> $c
*/
function doFoo(B $b, C $c, Child $child, GrandChild $grandChild): void
{
assertType('int', $b->foo());
assertType('string', $c->foo());
assertType('int|string', $child->bar());
assertType('int', $grandChild->bar());
}
7 changes: 7 additions & 0 deletions tests/PHPStan/Rules/Methods/MethodSignatureRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -676,4 +676,11 @@ public function testPureUnlessParameterPassedOverride(): void
]);
}

public function testBug15128(): void
{
$this->reportMaybes = true;
$this->reportStatic = true;
$this->analyse([__DIR__ . '/data/bug-15128.php'], []);
}

}
42 changes: 42 additions & 0 deletions tests/PHPStan/Rules/Methods/data/bug-15128.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php declare(strict_types = 1);

namespace Bug15128Methods;

/**
* @template TKey of int|string
* @template TValue of int|string
*/
interface Store
{

/**
* @return (TKey is int ? list<TValue> : (TValue is int ? int : string))
*/
public function get();

/**
* @param int|string $key
* @return (TKey is int ? list<TValue> : ($key is int ? int : string))
*/
public function find($key);

}

/**
* @template TValue of int|string
* @implements Store<string, TValue>
*/
class StringStore implements Store
{

public function get()
{
return 1;
}

public function find($key)
{
return 1;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -112,4 +112,9 @@ public function testBug10938(): void
$this->analyse([__DIR__ . '/../../Analyser/nsrt/bug-10938.php'], []);
}

public function testBug15128(): void
{
$this->analyse([__DIR__ . '/data/bug-15128.php'], []);
}

}
68 changes: 68 additions & 0 deletions tests/PHPStan/Rules/PhpDoc/data/bug-15128.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php declare(strict_types = 1); // lint >= 8.0

namespace Bug15128;

use Override;

/**
* @template T of int|string
*/
class A {
/**
* @param T $val
*/
public function __construct(
protected int|string $val,
) {
}

/**
* @return (T is int ? string : int)
*/
public function foo(): int|string {
return is_string($this->val) ? intval($this->val) : (string)$this->val;
}

/**
* @param (T is int ? string : int) $x
*/
public function bar(int|string $x): void {
}

/**
* @return (T is int ? string : (T is 'a' ? int : string))
*/
public function baz(): int|string {
return 1;
}
}

/**
* @extends A<string>
*/
class B extends A {
#[Override]
public function foo(): int {
return intval($this->val);
}

#[Override]
public function bar(int|string $x): void {
}

#[Override]
public function baz(): int|string {
return 1;
}
}

/**
* @template U of int
* @extends A<U>
*/
class C extends A {
#[Override]
public function foo(): string {
return (string)$this->val;
}
}
Loading