Completed
Push — master ( b2a65d...63a169 )
by Kirill
02:18
created

VariableBuilder::getVariableNames()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 6
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
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
use Railt\SDL\IR\SymbolTable\VarSymbol;
17
18
/**
19
 * Class VariableBuilder
20
 */
21
class VariableBuilder extends BaseBuilder
22
{
23
    /**
24
     * @var string[]
25
     */
26
    private const VARIABLE_DEFINITIONS = [
27
        'ConstantDefinition',
28
        'VariableDefinition',
29
    ];
30
31
    /**
32
     * @param RuleInterface $rule
33
     * @return bool
34
     */
35
    public function match(RuleInterface $rule): bool
36
    {
37
        return \in_array($rule->getName(), self::VARIABLE_DEFINITIONS, true);
38
    }
39
40
    /**
41
     * @param ContextInterface $ctx
42
     * @param RuleInterface $rule
43
     * @return \Generator|\Closure
44
     */
45
    public function reduce(ContextInterface $ctx, RuleInterface $rule): \Generator
46
    {
47
        $isConstant = $this->isConstant($rule);
48
        $variables = [];
49
50
        foreach ($this->getVariableNames($rule) as $variable) {
51
            $record = $variables[] = $ctx->declare($variable);
52
53
            $isConstant ? $record->lock() : $record->unlock();
54
        }
55
56
        yield function () use ($rule, $variables) {
57
            /** @var ValueInterface $value */
58
            $value = yield $this->getValueNode($rule);
59
60
            /** @var VarSymbol $variable */
61
            foreach ($variables as $variable) {
62
                $variable->set($value);
63
            }
64
        };
65
    }
66
67
    /**
68
     * @param RuleInterface $rule
69
     * @return iterable|string[]
70
     */
71
    private function getVariableNames(RuleInterface $rule): iterable
72
    {
73
        foreach ($rule->find('> #VariableName') as $name) {
74
            yield $name->first('> :T_VARIABLE')->getValue(1);
75
        }
76
    }
77
78
    /**
79
     * @param RuleInterface $rule
80
     * @return RuleInterface
81
     */
82
    private function getValueNode(RuleInterface $rule): RuleInterface
83
    {
84
        return $rule->first('> #VariableValue')->getChild(0);
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Railt\Parser\Ast\NodeInterface as the method getChild() does only exist in the following implementations of said interface: Railt\Compiler\Grammar\Delegate\IncludeDelegate, Railt\Compiler\Grammar\Delegate\RuleDelegate, Railt\Compiler\Grammar\Delegate\TokenDelegate, Railt\Parser\Ast\Rule.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

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

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
85
    }
86
87
    /**
88
     * @param RuleInterface $rule
89
     * @return bool
90
     */
91
    private function isConstant(RuleInterface $rule): bool
92
    {
93
        return $rule->getName() === 'ConstantDefinition';
94
    }
95
}
96