| 1: | <?php declare(strict_types = 1); |
| 2: | |
| 3: | namespace PHPStan\PhpDocParser\Ast\Type; |
| 4: | |
| 5: | use PHPStan\PhpDocParser\Ast\NodeAttributes; |
| 6: | use function implode; |
| 7: | use function sprintf; |
| 8: | |
| 9: | class GenericTypeNode implements TypeNode |
| 10: | { |
| 11: | |
| 12: | public const VARIANCE_INVARIANT = 'invariant'; |
| 13: | public const VARIANCE_COVARIANT = 'covariant'; |
| 14: | public const VARIANCE_CONTRAVARIANT = 'contravariant'; |
| 15: | public const VARIANCE_BIVARIANT = 'bivariant'; |
| 16: | |
| 17: | use NodeAttributes; |
| 18: | |
| 19: | public IdentifierTypeNode $type; |
| 20: | |
| 21: | |
| 22: | public array $genericTypes; |
| 23: | |
| 24: | |
| 25: | public array $variances; |
| 26: | |
| 27: | |
| 28: | |
| 29: | |
| 30: | |
| 31: | public function __construct(IdentifierTypeNode $type, array $genericTypes, array $variances = []) |
| 32: | { |
| 33: | $this->type = $type; |
| 34: | $this->genericTypes = $genericTypes; |
| 35: | $this->variances = $variances; |
| 36: | } |
| 37: | |
| 38: | public function __toString(): string |
| 39: | { |
| 40: | $genericTypes = []; |
| 41: | |
| 42: | foreach ($this->genericTypes as $index => $type) { |
| 43: | $variance = $this->variances[$index] ?? self::VARIANCE_INVARIANT; |
| 44: | if ($variance === self::VARIANCE_INVARIANT) { |
| 45: | $genericTypes[] = (string) $type; |
| 46: | } elseif ($variance === self::VARIANCE_BIVARIANT) { |
| 47: | $genericTypes[] = '*'; |
| 48: | } else { |
| 49: | $genericTypes[] = sprintf('%s %s', $variance, $type); |
| 50: | } |
| 51: | } |
| 52: | |
| 53: | return $this->type . '<' . implode(', ', $genericTypes) . '>'; |
| 54: | } |
| 55: | |
| 56: | |
| 57: | |
| 58: | |
| 59: | public static function __set_state(array $properties): self |
| 60: | { |
| 61: | $instance = new self($properties['type'], $properties['genericTypes'], $properties['variances'] ?? []); |
| 62: | if (isset($properties['attributes'])) { |
| 63: | foreach ($properties['attributes'] as $key => $value) { |
| 64: | $instance->setAttribute($key, $value); |
| 65: | } |
| 66: | } |
| 67: | return $instance; |
| 68: | } |
| 69: | |
| 70: | } |
| 71: | |