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