Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
24 changes: 23 additions & 1 deletion src/Type/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use PHPStan\Type\Generic\GenericClassStringType;
use PHPStan\Type\Generic\TemplateMixedType;
use PHPStan\Type\Generic\TemplateType;
use PHPStan\Type\Generic\TemplateTypeFactory;
use PHPStan\Type\Generic\TemplateTypeMap;
use PHPStan\Type\Generic\TemplateTypeVariance;
use PHPStan\Type\Generic\TemplateUnionType;
Expand Down Expand Up @@ -92,15 +93,36 @@ public function getTypes(): array
public function filterTypes(callable $filterCb): Type
{
$newTypes = [];
$changed = false;
foreach ($this->getTypes() as $innerType) {
if (!$filterCb($innerType)) {
$changed = true;
continue;
}

$newTypes[] = $innerType;
}

return TypeCombinator::union(...$newTypes);
if (!$changed) {
return $this;
}

$result = TypeCombinator::union(...$newTypes);
if ($this instanceof BenevolentUnionType) {
$result = TypeUtils::toBenevolentUnion($result);
}
if ($this instanceof TemplateType) {
Copy link
Member

Choose a reason for hiding this comment

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

I'd rather have this logic in the subtypes in overridden methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

$result = TemplateTypeFactory::create(
$this->getScope(),
$this->getName(),
$result,
$this->getVariance(),
$this->getStrategy(),
$this->getDefault(),
);
}

return $result;
}

public function isNormalized(): bool
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-6609-83.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function modify2(\DateTimeInterface $date) {
*/
function modify3(\DateTimeInterface $date, string $s) {
$date = $date->modify($s);
assertType('DateTime|DateTimeImmutable', $date);
assertType('T of DateTime|DateTimeImmutable (method Bug6609Php83\Foo::modify3(), argument)', $date);

return $date;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/nsrt/bug-6609.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function modify2(\DateTimeInterface $date) {
*/
function modify3(\DateTimeInterface $date, string $s) {
$date = $date->modify($s);
assertType('(DateTime|DateTimeImmutable|false)', $date);
assertType('((T of DateTime|DateTimeImmutable (method Bug6609\Foo::modify3(), argument))|false)', $date);

return $date;
}
Expand Down
5 changes: 5 additions & 0 deletions tests/PHPStan/Rules/Methods/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1054,4 +1054,9 @@ public function testBug10715(): void
$this->analyse([__DIR__ . '/data/bug-10715.php'], []);
}

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

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

namespace Bug11663;

class BuilderA
{
/**
* @param string $test
* @return $this
*/
public function where(string $test)
{
return $this;
}
}

class BuilderB
{
/**
* @param string $test
* @return $this
*/
public function where(string $test)
{
return $this;
}
}

interface A
{
public function doFoo(): static;
}

interface B
{
}

class Test
{
/**
* @template B of BuilderA|BuilderB
* @param B $template
* @return B
*/
public function test($template)
{
return $template->where('test');
}

/**
* @param __benevolent<BuilderA|BuilderB|false> $template
* @return __benevolent<BuilderA|BuilderB>
*/
public function test2($template)
{
return $template->where('test');
}


/**
* @template T of A|B
* @param T $ab
* @return T
*/
function foo(A|B $ab): A|B
{
return $ab->doFoo();
}

/**
* @template T of __benevolent<A|B>
* @param T $ab
* @return T
*/
function foo2(A|B $ab): A|B
{
return $ab->doFoo();
}
}
Loading