forked from phpstan/phpstan-src
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make ParametersAcceptorSelector::selectSingle() return union-ed condi…
…tional types See phpstan/phpstan/issues/3853#issuecomment-1082351763
- Loading branch information
1 parent
eeedd1c
commit af88720
Showing
8 changed files
with
100 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Reflection; | ||
|
||
use PHPStan\Type\ConditionalType; | ||
use PHPStan\Type\ConditionalTypeForParameter; | ||
use PHPStan\Type\Generic\TemplateTypeMap; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeTraverser; | ||
|
||
class SingleParametersAcceptor implements ParametersAcceptor | ||
{ | ||
|
||
public function __construct(private ParametersAcceptor $acceptor) | ||
{ | ||
} | ||
|
||
public function getTemplateTypeMap(): TemplateTypeMap | ||
{ | ||
return $this->acceptor->getTemplateTypeMap(); | ||
} | ||
|
||
public function getResolvedTemplateTypeMap(): TemplateTypeMap | ||
{ | ||
return $this->acceptor->getResolvedTemplateTypeMap(); | ||
} | ||
|
||
/** | ||
* @return array<int, ParameterReflection> | ||
*/ | ||
public function getParameters(): array | ||
{ | ||
return $this->acceptor->getParameters(); | ||
} | ||
|
||
public function isVariadic(): bool | ||
{ | ||
return $this->acceptor->isVariadic(); | ||
} | ||
|
||
public function getReturnType(): Type | ||
{ | ||
return TypeTraverser::map($this->acceptor->getReturnType(), static function (Type $type, callable $traverse) { | ||
while ($type instanceof ConditionalType || $type instanceof ConditionalTypeForParameter) { | ||
$type = $type->getResult(); | ||
} | ||
|
||
return $traverse($type); | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
tests/PHPStan/Analyser/data/dynamic-method-return-getsingle-conditional.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace DynamicMethodReturnGetSingleConditional; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
abstract class Foo | ||
{ | ||
/** | ||
* @return ($input is 1 ? true : false) | ||
*/ | ||
abstract public function get(int $input): mixed; | ||
|
||
public function doFoo(): void | ||
{ | ||
assertType('bool', $this->get(0)); | ||
assertType('bool', $this->get(1)); | ||
assertType('bool', $this->get(2)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters