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\Frontend\Builder\Common; |
11
|
|
|
|
12
|
|
|
use Railt\Parser\Ast\RuleInterface; |
13
|
|
|
use Railt\SDL\Exception\InvalidArgumentException; |
14
|
|
|
use Railt\SDL\Frontend\Builder\BaseBuilder; |
15
|
|
|
use Railt\SDL\Frontend\Definition\Definition; |
16
|
|
|
use Railt\SDL\Frontend\Context\ContextInterface; |
17
|
|
|
use Railt\SDL\IR\SymbolTable\ValueInterface; |
18
|
|
|
use Railt\SDL\IR\Type\TypeNameInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class TypeDefinitionHeaderBuilder |
22
|
|
|
*/ |
23
|
|
|
class TypeDefinitionBuilder extends BaseBuilder |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @param RuleInterface $rule |
27
|
|
|
* @return bool |
28
|
|
|
*/ |
29
|
|
|
public function match(RuleInterface $rule): bool |
30
|
|
|
{ |
31
|
|
|
return $rule->getName() === 'TypeDefinition'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param ContextInterface $ctx |
36
|
|
|
* @param RuleInterface $rule |
37
|
|
|
* @return \Generator|Definition |
38
|
|
|
* @throws InvalidArgumentException |
39
|
|
|
*/ |
40
|
|
|
public function reduce(ContextInterface $ctx, RuleInterface $rule): \Generator |
41
|
|
|
{ |
42
|
|
|
/** @var TypeNameInterface $name */ |
43
|
|
|
$name = yield $rule->first('> #TypeName'); |
44
|
|
|
|
45
|
|
|
$definition = new Definition($ctx, $name); |
46
|
|
|
|
47
|
|
|
foreach ($rule->find('> #GenericDefinitionArgument') as $argument) { |
48
|
|
|
yield from $name = $this->getArgumentName($argument); |
49
|
|
|
yield from $value = $this->getArgumentValue($argument); |
50
|
|
|
|
51
|
|
|
$definition->addArgument((string)$name->getReturn(), $value->getReturn()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return $definition; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param RuleInterface $argument |
59
|
|
|
* @return \Generator|TypeNameInterface |
60
|
|
|
*/ |
61
|
|
|
private function getArgumentName(RuleInterface $argument): \Generator |
62
|
|
|
{ |
63
|
|
|
/** @var TypeNameInterface $name */ |
64
|
|
|
return yield $argument->first('> #GenericDefinitionArgumentName')->getChild(0); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param RuleInterface $argument |
69
|
|
|
* @return \Generator|TypeNameInterface |
70
|
|
|
*/ |
71
|
|
|
private function getArgumentValue(RuleInterface $argument): \Generator |
72
|
|
|
{ |
73
|
|
|
/** @var ValueInterface|TypeNameInterface $value */ |
74
|
|
|
return yield $argument->first('> #GenericDefinitionArgumentValue')->getChild(0); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
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: