1: <?php declare(strict_types = 1);
2:
3: namespace PHPStan\PhpDocParser\Ast\Type;
4:
5: use PHPStan\PhpDocParser\Ast\NodeAttributes;
6: use function sprintf;
7:
8: class ConditionalTypeNode implements TypeNode
9: {
10:
11: use NodeAttributes;
12:
13: public TypeNode $subjectType;
14:
15: public TypeNode $targetType;
16:
17: public TypeNode $if;
18:
19: public TypeNode $else;
20:
21: public bool $negated;
22:
23: public function __construct(TypeNode $subjectType, TypeNode $targetType, TypeNode $if, TypeNode $else, bool $negated)
24: {
25: $this->subjectType = $subjectType;
26: $this->targetType = $targetType;
27: $this->if = $if;
28: $this->else = $else;
29: $this->negated = $negated;
30: }
31:
32: public function __toString(): string
33: {
34: return sprintf(
35: '(%s %s %s ? %s : %s)',
36: // "?Foo is Bar ? ... : ..." reads as a nullable type followed by
37: // something else, so the subject keeps the parentheses it was read
38: // with
39: $this->subjectType instanceof NullableTypeNode
40: ? '(' . $this->subjectType . ')'
41: : $this->subjectType,
42: $this->negated ? 'is not' : 'is',
43: $this->targetType,
44: $this->if,
45: $this->else,
46: );
47: }
48:
49: /**
50: * @param array<string, mixed> $properties
51: */
52: public static function __set_state(array $properties): self
53: {
54: $instance = new self($properties['subjectType'], $properties['targetType'], $properties['if'], $properties['else'], $properties['negated']);
55: if (isset($properties['attributes'])) {
56: foreach ($properties['attributes'] as $key => $value) {
57: $instance->setAttribute($key, $value);
58: }
59: }
60: return $instance;
61: }
62:
63: }
64: