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