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\Reflection\Contracts\Definition; |
14
|
|
|
use Railt\Reflection\Contracts\Definition\TypeDefinition; |
15
|
|
|
use Railt\Reflection\Contracts\Document as DocumentInterface; |
16
|
|
|
use Railt\Reflection\Definition\Dependent\DirectiveLocation; |
17
|
|
|
use Railt\Reflection\Definition\DirectiveDefinition; |
18
|
|
|
use Railt\Reflection\Document; |
19
|
|
|
use Railt\SDL\Compiler\Pipeline; |
20
|
|
|
use Railt\SDL\Exception\TypeConflictException; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class DirectiveDelegate |
24
|
|
|
*/ |
25
|
|
|
class DirectiveDelegate extends TypeDefinitionDelegate |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @param DocumentInterface|Document $document |
29
|
|
|
* @return Definition |
30
|
|
|
*/ |
31
|
|
|
protected function create(DocumentInterface $document): Definition |
32
|
|
|
{ |
33
|
|
|
return new DirectiveDefinition($document, $this->getTypeName()); |
|
|
|
|
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return void |
38
|
|
|
*/ |
39
|
|
|
protected function register(): void |
40
|
|
|
{ |
41
|
|
|
$this->future(Pipeline::PRIORITY_DEFINITION, function() { |
42
|
|
|
$this->bootLocations($this->definition); |
|
|
|
|
43
|
|
|
}); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param DirectiveDefinition|TypeDefinition $directive |
48
|
|
|
*/ |
49
|
|
|
private function bootLocations(DirectiveDefinition $directive): void |
50
|
|
|
{ |
51
|
|
|
foreach ($this->getLocations($directive) as $offset => $location) { |
52
|
|
|
$this->transaction($location, function (DirectiveLocation $location) use ($directive): void { |
53
|
|
|
$this->verifyLocation($location); |
54
|
|
|
$this->verifyDuplication($directive, $location); |
55
|
|
|
|
56
|
|
|
$directive->withLocation($location); |
57
|
|
|
}); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param DirectiveDefinition $directive |
63
|
|
|
* @param DirectiveLocation $location |
64
|
|
|
* @throws \Railt\SDL\Exception\CompilerException |
65
|
|
|
*/ |
66
|
|
|
private function verifyDuplication(DirectiveDefinition $directive, DirectiveLocation $location): void |
|
|
|
|
67
|
|
|
{ |
68
|
|
View Code Duplication |
if ($directive->hasLocation($location->getName())) { |
|
|
|
|
69
|
|
|
$error = 'Could not determine the location %s, because %s already exists'; |
70
|
|
|
$error = \sprintf($error, $location, $directive->getLocation($location->getName())); |
71
|
|
|
|
72
|
|
|
throw (new TypeConflictException($error))->using($this->getCallStack())->in($location); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param DirectiveDefinition $definition |
78
|
|
|
* @return iterable|DirectiveLocation[] |
79
|
|
|
*/ |
80
|
|
|
private function getLocations(DirectiveDefinition $definition): iterable |
81
|
|
|
{ |
82
|
|
|
/** @var LeafInterface $child */ |
83
|
|
|
foreach ($this->first('DirectiveLocations', 1) as $child) { |
|
|
|
|
84
|
|
|
yield (new DirectiveLocation($definition, $child->getValue()))->withOffset($child->getOffset()); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param DirectiveLocation $location |
90
|
|
|
* @throws \Railt\SDL\Exception\CompilerException |
91
|
|
|
*/ |
92
|
|
|
private function verifyLocation(DirectiveLocation $location): void |
93
|
|
|
{ |
94
|
|
|
$isValid = $location->isExecutable() || $location->isPrivate(); |
95
|
|
|
|
96
|
|
|
if (! $isValid) { |
97
|
|
|
$error = \sprintf('Invalid directive location %s', $location); |
98
|
|
|
|
99
|
|
|
throw $this->error(new TypeConflictException($error))->in($location); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
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.