Test Failed
Push — master ( 33bfdc...47f867 )
by Kirill
02:32
created

VariableBuilder::getValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
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
17
/**
18
 * Class VariableBuilder
19
 */
20
class VariableBuilder extends BaseBuilder
21
{
22
    /**
23
     * @var string[]
24
     */
25
    private const VARIABLE_DEFINITIONS = [
26
        'ConstantDefinition',
27
        'VariableDefinition',
28
    ];
29
30
    /**
31
     * @param RuleInterface $rule
32
     * @return bool
33
     */
34
    public function match(RuleInterface $rule): bool
35
    {
36
        return \in_array($rule->getName(), self::VARIABLE_DEFINITIONS, true);
37
    }
38
39
    /**
40
     * @param ContextInterface $ctx
41
     * @param RuleInterface $rule
42
     * @return \Generator|\Closure
43
     */
44
    public function reduce(ContextInterface $ctx, RuleInterface $rule): \Generator
45
    {
46
        yield function() use ($ctx, $rule): \Generator {
47
            /** @var ValueInterface $value */
48
            [$isConstant, $value] = [$this->isConstant($rule), yield $this->getValueNode($rule)];
0 ignored issues
show
Bug introduced by
The variable $isConstant does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The variable $value does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
49
50
            foreach ($rule->find('> #VariableName') as $name) {
51
                $variable = $name->first('> :T_VARIABLE')->getValue(1);
52
53
                $record = $ctx->declare($variable)->set($value);
54
55
                $isConstant ? $record->lock() : $record->unlock();
56
            }
57
        };
58
    }
59
60
    /**
61
     * @param RuleInterface $rule
62
     * @return RuleInterface
63
     */
64
    private function getValueNode(RuleInterface $rule): RuleInterface
65
    {
66
        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, Railt\SDL\Frontend\AST\Value\AbstractAstValueNode, Railt\SDL\Frontend\AST\Value\ConstantValueNode, Railt\SDL\Frontend\AST\Value\NullValueNode, Railt\SDL\Frontend\AST\Value\NumberValueNode, Railt\SDL\Frontend\AST\Value\StringValueNode.

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...
67
    }
68
69
    /**
70
     * @param RuleInterface $rule
71
     * @return bool
72
     */
73
    private function isConstant(RuleInterface $rule): bool
74
    {
75
        return $rule->getName() === 'ConstantDefinition';
76
    }
77
}
78