1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\Type\Generic;
4:
5: use PHPStan\Turbo\ShadowedByTurboExtension;
6: use PHPStan\Type\BenevolentUnionType;
7: use PHPStan\Type\Type;
8:
9: /** @api */
10: #[ShadowedByTurboExtension(implementation: __DIR__ . '/../../../turbo-ext/src/TemplateBenevolentUnionType.cpp')]
11: final class TemplateBenevolentUnionType extends BenevolentUnionType implements TemplateType
12: {
13:
14: /** @use TemplateTypeTrait<BenevolentUnionType> */
15: use TemplateTypeTrait;
16:
17: /**
18: * @param non-empty-string $name
19: */
20: public function __construct(
21: TemplateTypeScope $scope,
22: TemplateTypeStrategy $templateTypeStrategy,
23: TemplateTypeVariance $templateTypeVariance,
24: string $name,
25: BenevolentUnionType $bound,
26: ?Type $default,
27: )
28: {
29: parent::__construct($bound->getTypes());
30:
31: $this->scope = $scope;
32: $this->strategy = $templateTypeStrategy;
33: $this->variance = $templateTypeVariance;
34: $this->name = $name;
35: $this->bound = $bound;
36: $this->default = $default;
37: }
38:
39: /** @param list<Type> $types */
40: public function withTypes(array $types): self
41: {
42: return new self(
43: $this->scope,
44: $this->strategy,
45: $this->variance,
46: $this->name,
47: new BenevolentUnionType($types),
48: $this->default,
49: );
50: }
51:
52: public function filterTypes(callable $filterCb): Type
53: {
54: $result = parent::filterTypes($filterCb);
55: if (!$result instanceof TemplateType) {
56: return TemplateTypeFactory::create(
57: $this->getScope(),
58: $this->getName(),
59: $result,
60: $this->getVariance(),
61: $this->getStrategy(),
62: $this->getDefault(),
63: );
64: }
65:
66: return $result;
67: }
68:
69: }
70: