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