| 1: | <?php declare(strict_types = 1); |
| 2: | |
| 3: | namespace PHPStan\Type\Generic; |
| 4: | |
| 5: | use function array_key_exists; |
| 6: | |
| 7: | |
| 8: | class TemplateTypeVarianceMap |
| 9: | { |
| 10: | |
| 11: | private static ?TemplateTypeVarianceMap $empty = null; |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | public function __construct(private array $variances) |
| 18: | { |
| 19: | } |
| 20: | |
| 21: | public static function createEmpty(): self |
| 22: | { |
| 23: | $empty = self::$empty; |
| 24: | |
| 25: | if ($empty !== null) { |
| 26: | return $empty; |
| 27: | } |
| 28: | |
| 29: | $empty = new self([]); |
| 30: | self::$empty = $empty; |
| 31: | |
| 32: | return $empty; |
| 33: | } |
| 34: | |
| 35: | |
| 36: | public function getVariances(): array |
| 37: | { |
| 38: | return $this->variances; |
| 39: | } |
| 40: | |
| 41: | public function hasVariance(string $name): bool |
| 42: | { |
| 43: | return array_key_exists($name, $this->getVariances()); |
| 44: | } |
| 45: | |
| 46: | public function getVariance(string $name): ?TemplateTypeVariance |
| 47: | { |
| 48: | return $this->getVariances()[$name] ?? null; |
| 49: | } |
| 50: | |
| 51: | } |
| 52: | |