|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Railt package. |
|
4
|
|
|
* |
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
6
|
|
|
* file that was distributed with this source code. |
|
7
|
|
|
*/ |
|
8
|
|
|
declare(strict_types=1); |
|
9
|
|
|
|
|
10
|
|
|
namespace Railt\SDL\Compiler\Definition; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\LeafInterface; |
|
13
|
|
|
use Railt\Parser\Ast\NodeInterface; |
|
14
|
|
|
use Railt\Parser\Ast\RuleInterface; |
|
15
|
|
|
use Railt\Parser\Environment; |
|
16
|
|
|
use Railt\Reflection\Contracts\Definition; |
|
17
|
|
|
use Railt\Reflection\Contracts\Definition\TypeDefinition; |
|
18
|
|
|
use Railt\Reflection\Contracts\Document as DocumentInterface; |
|
19
|
|
|
use Railt\Reflection\Definition\Dependent\EnumValueDefinition; |
|
20
|
|
|
use Railt\Reflection\Definition\EnumDefinition; |
|
21
|
|
|
use Railt\Reflection\Document; |
|
22
|
|
|
use Railt\SDL\Compiler\Pipeline; |
|
23
|
|
|
use Railt\SDL\Exception\TypeConflictException; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Class EnumDelegate |
|
27
|
|
|
*/ |
|
28
|
|
|
class EnumDelegate extends TypeDefinitionDelegate |
|
29
|
|
|
{ |
|
30
|
|
|
/** |
|
31
|
|
|
* @param DocumentInterface|Document $document |
|
32
|
|
|
* @return Definition |
|
33
|
|
|
*/ |
|
34
|
|
|
protected function create(DocumentInterface $document): Definition |
|
35
|
|
|
{ |
|
36
|
|
|
return new EnumDefinition($document, $this->getTypeName()); |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param Environment $env |
|
41
|
|
|
*/ |
|
42
|
|
|
public function boot(Environment $env): void |
|
43
|
|
|
{ |
|
44
|
|
|
parent::boot($env); |
|
45
|
|
|
|
|
46
|
|
|
$this->future(Pipeline::PRIORITY_DEFINITION, function() { |
|
47
|
|
|
$this->bootValues($this->definition); |
|
|
|
|
|
|
48
|
|
|
}); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @param EnumDefinition|TypeDefinition $enum |
|
53
|
|
|
*/ |
|
54
|
|
|
private function bootValues(EnumDefinition $enum): void |
|
55
|
|
|
{ |
|
56
|
|
|
foreach ($this->getEnumValues($enum) as $value) { |
|
57
|
|
|
$this->transaction($value, function (EnumValueDefinition $value) use ($enum): void { |
|
58
|
|
|
$this->verifyDuplication($enum, $value); |
|
59
|
|
|
|
|
60
|
|
|
$enum->withValue($value); |
|
61
|
|
|
}); |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* @param EnumDefinition $enum |
|
67
|
|
|
* @return iterable |
|
68
|
|
|
*/ |
|
69
|
|
|
private function getEnumValues(EnumDefinition $enum): iterable |
|
70
|
|
|
{ |
|
71
|
|
|
/** @var RuleInterface $ast */ |
|
72
|
|
|
foreach ($this->first('EnumValues', 1) as $ast) { |
|
|
|
|
|
|
73
|
|
|
$enumValue = $this->createEnumValue($enum, $ast->first('T_NAME', 1)); |
|
74
|
|
|
|
|
75
|
|
|
$this->transaction($enumValue, function (EnumValueDefinition $def) use ($ast): void { |
|
76
|
|
|
//$this->withDescription($def, $ast); |
|
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
/** @var RuleInterface $defValue */ |
|
79
|
|
|
if ($defValue = $ast->first('Value', 1)) { |
|
80
|
|
|
$def->withValue($this->value($defValue)->toScalar()); |
|
81
|
|
|
} |
|
82
|
|
|
}); |
|
83
|
|
|
|
|
84
|
|
|
yield $enumValue; |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param EnumDefinition $enum |
|
90
|
|
|
* @param LeafInterface|NodeInterface $name |
|
91
|
|
|
* @return EnumValueDefinition |
|
92
|
|
|
*/ |
|
93
|
|
|
private function createEnumValue(EnumDefinition $enum, LeafInterface $name): EnumValueDefinition |
|
94
|
|
|
{ |
|
95
|
|
|
$def = new EnumValueDefinition($enum, $name->getValue()); |
|
96
|
|
|
$def->withOffset($name->getOffset()); |
|
97
|
|
|
|
|
98
|
|
|
return $def; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @param EnumDefinition $enum |
|
103
|
|
|
* @param EnumValueDefinition $value |
|
104
|
|
|
* @throws \Railt\SDL\Exception\CompilerException |
|
105
|
|
|
*/ |
|
106
|
|
|
private function verifyDuplication(EnumDefinition $enum, EnumValueDefinition $value): void |
|
|
|
|
|
|
107
|
|
|
{ |
|
108
|
|
View Code Duplication |
if ($enum->hasValue($value->getName())) { |
|
|
|
|
|
|
109
|
|
|
$error = 'Could not define %s, because %s already exists'; |
|
110
|
|
|
$error = \sprintf($error, $value, $enum->getValue($value->getName())); |
|
111
|
|
|
|
|
112
|
|
|
throw (new TypeConflictException($error))->using($this->getCallStack())->in($value); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.