-
Notifications
You must be signed in to change notification settings - Fork 479
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Helper PHPDoc type:
template-type
(calling `Type::getTemplateType()…
…` method)
- Loading branch information
1 parent
b647037
commit b6d0c87
Showing
4 changed files
with
213 additions
and
0 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,83 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Type\Helper; | ||
|
||
use PHPStan\Type\CompoundType; | ||
use PHPStan\Type\Generic\TemplateTypeVariance; | ||
use PHPStan\Type\LateResolvableType; | ||
use PHPStan\Type\Traits\LateResolvableTypeTrait; | ||
use PHPStan\Type\Traits\NonGeneralizableTypeTrait; | ||
use PHPStan\Type\Type; | ||
use PHPStan\Type\TypeUtils; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function sprintf; | ||
|
||
/** @api */ | ||
final class GetTemplateTypeType implements CompoundType, LateResolvableType | ||
{ | ||
|
||
use LateResolvableTypeTrait; | ||
use NonGeneralizableTypeTrait; | ||
|
||
public function __construct(private Type $type, private string $ancestorClassName, private string $templateTypeName) | ||
{ | ||
} | ||
|
||
public function getReferencedClasses(): array | ||
{ | ||
return $this->type->getReferencedClasses(); | ||
} | ||
|
||
public function getReferencedTemplateTypes(TemplateTypeVariance $positionVariance): array | ||
{ | ||
return $this->type->getReferencedTemplateTypes($positionVariance); | ||
} | ||
|
||
public function equals(Type $type): bool | ||
{ | ||
return $type instanceof self | ||
&& $this->type->equals($type->type); | ||
} | ||
|
||
public function describe(VerbosityLevel $level): string | ||
{ | ||
return sprintf('template-type<%s, %s, %s>', $this->type->describe($level), $this->ancestorClassName, $this->templateTypeName); | ||
} | ||
|
||
public function isResolvable(): bool | ||
{ | ||
return !TypeUtils::containsTemplateType($this->type); | ||
} | ||
|
||
protected function getResult(): Type | ||
{ | ||
return $this->type->getTemplateType($this->ancestorClassName, $this->templateTypeName); | ||
} | ||
|
||
/** | ||
* @param callable(Type): Type $cb | ||
*/ | ||
public function traverse(callable $cb): Type | ||
{ | ||
$type = $cb($this->type); | ||
|
||
if ($this->type === $type) { | ||
return $this; | ||
} | ||
|
||
return new self($type, $this->ancestorClassName, $this->templateTypeName); | ||
} | ||
|
||
/** | ||
* @param mixed[] $properties | ||
*/ | ||
public static function __set_state(array $properties): Type | ||
{ | ||
return new self( | ||
$properties['type'], | ||
$properties['ancestorClassName'], | ||
$properties['templateTypeName'], | ||
); | ||
} | ||
|
||
} |
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,110 @@ | ||
<?php | ||
|
||
namespace Discussion9053; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
/** | ||
* @template-covariant TChild of ChildInterface | ||
*/ | ||
interface ModelInterface { | ||
/** | ||
* @return TChild[] | ||
*/ | ||
public function getChildren(): array; | ||
} | ||
|
||
/** | ||
* @implements ModelInterface<Child> | ||
*/ | ||
class Model implements ModelInterface | ||
{ | ||
/** | ||
* @var Child[] | ||
*/ | ||
public array $children; | ||
|
||
public function getChildren(): array | ||
{ | ||
return $this->children; | ||
} | ||
} | ||
|
||
/** | ||
* @template-covariant T of ModelInterface | ||
*/ | ||
interface ChildInterface { | ||
/** | ||
* @return T | ||
*/ | ||
public function getModel(): ModelInterface; | ||
} | ||
|
||
|
||
/** | ||
* @implements ChildInterface<Model> | ||
*/ | ||
class Child implements ChildInterface | ||
{ | ||
public function __construct(private Model $model) | ||
{ | ||
} | ||
|
||
public function getModel(): Model | ||
{ | ||
return $this->model; | ||
} | ||
} | ||
|
||
/** | ||
* @template-covariant T of ModelInterface | ||
*/ | ||
class Helper | ||
{ | ||
/** | ||
* @param T $model | ||
*/ | ||
public function __construct(private ModelInterface $model) | ||
{} | ||
|
||
/** | ||
* @return template-type<T, ModelInterface, 'TChild'> | ||
*/ | ||
public function getFirstChildren(): ChildInterface | ||
{ | ||
$firstChildren = $this->model->getChildren()[0] ?? null; | ||
|
||
if (!$firstChildren) { | ||
throw new \RuntimeException('No first child found.'); | ||
} | ||
|
||
return $firstChildren; | ||
} | ||
} | ||
|
||
class Other { | ||
/** | ||
* @template TChild of ChildInterface | ||
* @template TModel of ModelInterface<TChild> | ||
* @param Helper<TModel> $helper | ||
* @return TChild | ||
*/ | ||
public function getFirstChildren(Helper $helper): ChildInterface { | ||
$child = $helper->getFirstChildren(); | ||
assertType('TChild of Discussion9053\ChildInterface (method Discussion9053\Other::getFirstChildren(), argument)', $child); | ||
|
||
return $child; | ||
} | ||
} | ||
|
||
function (): void { | ||
$model = new Model(); | ||
$helper = new Helper($model); | ||
assertType('Discussion9053\Helper<Discussion9053\Model>', $helper); | ||
$child = $helper->getFirstChildren(); | ||
assertType('Discussion9053\Child', $child); | ||
|
||
$other = new Other(); | ||
$child2 = $other->getFirstChildren($helper); | ||
assertType('Discussion9053\Child', $child2); | ||
}; |