These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\Instruction; |
||
11 | |||
12 | use Railt\Parser\Ast\RuleInterface; |
||
13 | use Railt\SDL\Frontend\Builder\BaseBuilder; |
||
14 | use Railt\SDL\Frontend\Context\ContextInterface; |
||
15 | use Railt\SDL\IR\SymbolTable\ValueInterface; |
||
16 | |||
17 | /** |
||
18 | * Class VariableReassigmentBuilder |
||
19 | */ |
||
20 | class VariableReassigmentBuilder extends BaseBuilder |
||
21 | { |
||
22 | /** |
||
23 | * @param RuleInterface $rule |
||
24 | * @return bool |
||
25 | */ |
||
26 | public function match(RuleInterface $rule): bool |
||
27 | { |
||
28 | return $rule->getName() === 'VariableReassigment'; |
||
29 | } |
||
30 | |||
31 | /** |
||
32 | * @param ContextInterface $ctx |
||
33 | * @param RuleInterface $rule |
||
34 | * @return \Generator|\Closure |
||
35 | */ |
||
36 | public function reduce(ContextInterface $ctx, RuleInterface $rule) |
||
37 | { |
||
38 | yield function () use ($ctx, $rule): \Generator { |
||
39 | /** @var ValueInterface $value */ |
||
40 | $value = yield $rule->first('> #VariableValue')->getChild(0); |
||
0 ignored issues
–
show
|
|||
41 | |||
42 | foreach ($rule->find('> #VariableName') as $child) { |
||
43 | $name = $child->first('> :T_VARIABLE')->getValue(1); |
||
44 | |||
45 | $ctx->fetch($name)->set($value); |
||
46 | } |
||
47 | }; |
||
48 | } |
||
49 | } |
||
50 |
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: