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
10 changes: 10 additions & 0 deletions src/Type/BenevolentUnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ public function __construct(array $types, bool $normalized = false)
parent::__construct($types, $normalized);
}

public function filterTypes(callable $filterCb): Type
{
$result = parent::filterTypes($filterCb);
if (!$result instanceof self && $result instanceof UnionType) {
return TypeUtils::toBenevolentUnion($result);
}

return $result;
}

public function describe(VerbosityLevel $level): string
{
return '(' . parent::describe($level) . ')';
Expand Down
17 changes: 17 additions & 0 deletions src/Type/Generic/TemplateBenevolentUnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,21 @@ public function withTypes(array $types): self
);
}

public function filterTypes(callable $filterCb): Type
{
$result = parent::filterTypes($filterCb);
if (!$result instanceof TemplateType) {
return TemplateTypeFactory::create(
$this->getScope(),
$this->getName(),
$result,
$this->getVariance(),
$this->getStrategy(),
$this->getDefault(),
);
}

return $result;
}

}
17 changes: 17 additions & 0 deletions src/Type/Generic/TemplateUnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,21 @@ public function __construct(
$this->default = $default;
}

public function filterTypes(callable $filterCb): Type
{
$result = parent::filterTypes($filterCb);
if (!$result instanceof TemplateType) {
return TemplateTypeFactory::create(
$this->getScope(),
$this->getName(),
$result,
$this->getVariance(),
$this->getStrategy(),
$this->getDefault(),
);
}

return $result;
}

}
6 changes: 6 additions & 0 deletions src/Type/UnionType.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,20 @@ 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;
}

if (!$changed) {
return $this;
}

return TypeCombinator::union(...$newTypes);
}

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