|
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\Reflection\Builder\Definitions; |
|
11
|
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\NodeInterface; |
|
13
|
|
|
use Railt\Parser\Ast\RuleInterface; |
|
14
|
|
|
use Railt\SDL\Base\Definitions\BaseDirective; |
|
15
|
|
|
use Railt\SDL\Reflection\Builder\Dependent\Argument\ArgumentsBuilder; |
|
16
|
|
|
use Railt\SDL\Reflection\Builder\DocumentBuilder; |
|
17
|
|
|
use Railt\SDL\Reflection\Builder\Process\Compilable; |
|
18
|
|
|
use Railt\SDL\Reflection\Builder\Process\Compiler; |
|
19
|
|
|
use Railt\SDL\Reflection\Validation\Uniqueness; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class DirectiveBuilder |
|
23
|
|
|
*/ |
|
24
|
|
|
class DirectiveBuilder extends BaseDirective implements Compilable |
|
25
|
|
|
{ |
|
26
|
|
|
use Compiler; |
|
27
|
|
|
use ArgumentsBuilder; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* DirectiveBuilder constructor. |
|
31
|
|
|
* @param NodeInterface $ast |
|
32
|
|
|
* @param DocumentBuilder $document |
|
33
|
|
|
* @throws \Railt\SDL\Exceptions\TypeConflictException |
|
34
|
|
|
*/ |
|
35
|
3540 |
|
public function __construct(NodeInterface $ast, DocumentBuilder $document) |
|
36
|
|
|
{ |
|
37
|
3540 |
|
$this->boot($ast, $document); |
|
38
|
3540 |
|
$this->offset = $this->offsetPrefixedBy('directive'); |
|
39
|
3540 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param NodeInterface|RuleInterface $ast |
|
43
|
|
|
* @return bool |
|
44
|
|
|
* @throws \OutOfBoundsException |
|
45
|
|
|
*/ |
|
46
|
3540 |
|
protected function onCompile(NodeInterface $ast): bool |
|
47
|
|
|
{ |
|
48
|
3540 |
|
switch ($ast->getName()) { |
|
49
|
3540 |
|
case 'Target': |
|
50
|
2436 |
|
$validator = $this->getValidator(Uniqueness::class); |
|
51
|
|
|
|
|
52
|
2436 |
|
foreach ($ast->getChild(0)->getChildren() as $child) { |
|
|
|
|
|
|
53
|
2436 |
|
$location = $child->getValue(); |
|
54
|
|
|
|
|
55
|
2436 |
|
$validator->validate($this->locations, $child->getValue(), static::LOCATION_TYPE_NAME); |
|
56
|
|
|
|
|
57
|
2436 |
|
$this->locations[] = $location; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
3540 |
|
return false; |
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
|
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: