|
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\Builder\Definition; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\LeafInterface; |
|
13
|
|
|
use Railt\Parser\Ast\RuleInterface; |
|
14
|
|
|
use Railt\Reflection\Contracts\Definition; |
|
15
|
|
|
use Railt\Reflection\Definition\Dependent\DirectiveLocation; |
|
16
|
|
|
use Railt\Reflection\Definition\DirectiveDefinition; |
|
17
|
|
|
use Railt\SDL\Compiler\Ast\Definition\DirectiveDefinitionNode; |
|
18
|
|
|
use Railt\SDL\Compiler\Builder\Builder; |
|
19
|
|
|
use Railt\SDL\Exception\TypeConflictException; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class DirectiveBuilder |
|
23
|
|
|
*/ |
|
24
|
|
|
class DirectiveBuilder extends Builder |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @param RuleInterface|DirectiveDefinitionNode $rule |
|
28
|
|
|
* @param Definition $parent |
|
29
|
|
|
* @return Definition |
|
30
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
|
31
|
|
|
*/ |
|
32
|
|
|
public function build(RuleInterface $rule, Definition $parent): Definition |
|
33
|
|
|
{ |
|
34
|
|
|
$directive = new DirectiveDefinition($parent->getDocument(), $rule->getTypeName()); |
|
|
|
|
|
|
35
|
|
|
$directive->withOffset($rule->getOffset()); |
|
36
|
|
|
$directive->withDescription($rule->getDescription()); |
|
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
$this->buildLocations($directive, $rule); |
|
|
|
|
|
|
39
|
|
|
$this->buildArguments($directive, $rule); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
return $directive; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param DirectiveDefinition $directive |
|
46
|
|
|
* @param DirectiveDefinitionNode $rule |
|
47
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
|
48
|
|
|
*/ |
|
49
|
|
|
private function buildArguments(DirectiveDefinition $directive, DirectiveDefinitionNode $rule): void |
|
50
|
|
|
{ |
|
51
|
|
|
foreach ($rule->getArguments() as $ast) { |
|
52
|
|
|
/** @var Definition\Dependent\ArgumentDefinition $argument */ |
|
53
|
|
|
$argument = $this->dependent($ast, $directive); |
|
54
|
|
|
|
|
55
|
|
|
$directive->withArgument($argument); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param DirectiveDefinition $directive |
|
61
|
|
|
* @param DirectiveDefinitionNode $rule |
|
62
|
|
|
* @throws \Railt\Io\Exception\ExternalFileException |
|
63
|
|
|
*/ |
|
64
|
|
|
private function buildLocations(DirectiveDefinition $directive, DirectiveDefinitionNode $rule): void |
|
65
|
|
|
{ |
|
66
|
|
|
$locations = \array_merge( |
|
67
|
|
|
DirectiveLocation::EXECUTABLE_LOCATIONS, |
|
68
|
|
|
DirectiveLocation::SDL_LOCATIONS |
|
69
|
|
|
); |
|
70
|
|
|
|
|
71
|
|
|
/** @var LeafInterface $ast */ |
|
72
|
|
|
foreach ($rule->getLocations() as $ast => $name) { |
|
73
|
|
|
$location = new DirectiveLocation($directive, $name); |
|
74
|
|
|
$location->withOffset($ast->getOffset()); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
if (! \in_array($name, $locations, true)) { |
|
77
|
|
|
$error = \sprintf('Invalid directive location %s, only one of {%s} allowed', |
|
78
|
|
|
$location, \implode(', ', $locations)); |
|
79
|
|
|
throw (new TypeConflictException($error))->throwsIn($directive->getFile(), $ast->getOffset()); |
|
|
|
|
|
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
$directive->withLocation($location); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: