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\Compiler\Grammar\PP2\Resolvers; |
11
|
|
|
|
12
|
|
|
use Railt\Compiler\Grammar\PP2\Delegate\RuleDelegate; |
13
|
|
|
use Railt\Compiler\Grammar\PP2\Mapping; |
14
|
|
|
use Railt\Compiler\Grammar\PP2\ResolverInterface; |
15
|
|
|
use Railt\Compiler\Reader\BaseRules; |
16
|
|
|
use Railt\Io\Exception\ExternalFileException; |
17
|
|
|
use Railt\Io\Readable; |
18
|
|
|
use Railt\Parser\Ast\RuleInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class PragmasResolver |
22
|
|
|
*/ |
23
|
|
|
class RulesResolver extends BaseRules implements ResolverInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var Mapping |
27
|
|
|
*/ |
28
|
|
|
private $map; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* RulesResolver constructor. |
32
|
|
|
* @param Mapping $map |
33
|
|
|
*/ |
34
|
|
|
public function __construct(Mapping $map) |
35
|
|
|
{ |
36
|
|
|
$this->map = $map; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Readable $readable |
41
|
|
|
* @param RuleInterface|RuleDelegate $rule |
42
|
|
|
* @throws \LogicException |
43
|
|
|
*/ |
44
|
|
|
public function resolve(Readable $readable, RuleInterface $rule): void |
45
|
|
|
{ |
46
|
|
|
if ($this->has($rule->getRuleName())) { |
|
|
|
|
47
|
|
|
return; |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @throws ExternalFileException |
53
|
|
|
*/ |
54
|
|
|
protected function build(): void |
55
|
|
|
{ |
56
|
|
|
$current = $rule->getRule(); |
|
|
|
|
57
|
|
|
$this->add($current); |
58
|
|
|
|
59
|
|
|
foreach ($rule->getChildrenRules() as $child) { |
60
|
|
|
$this->add($child); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
try { |
64
|
|
|
if ($delegate = $rule->getDelegate()) { |
|
|
|
|
65
|
|
|
// Delegate: $rule->getRuleName() => $delegate |
|
|
|
|
66
|
|
|
} |
67
|
|
|
} catch (ExternalFileException $e) { |
68
|
|
|
throw $e->throwsIn($readable, $rule->getOffset()); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
|
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: