Test Failed
Push — master ( b7ab7b...94ee03 )
by Kirill
02:59
created

VariableBuilder   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 59
ccs 0
cts 21
cp 0
rs 10
c 0
b 0
f 0
wmc 6
lcom 0
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A match() 0 4 1
A reduce() 0 16 3
A getValue() 0 4 1
A isConstant() 0 4 1
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
16
/**
17
 * Class VariableBuilder
18
 */
19
class VariableBuilder extends BaseBuilder
20
{
21
    /**
22
     * @var string[]
23
     */
24
    private const VARIABLE_DEFINITIONS = [
25
        'ConstantDefinition',
26
        'VariableDefinition',
27
    ];
28
29
    /**
30
     * @param RuleInterface $rule
31
     * @return bool
32
     */
33
    public function match(RuleInterface $rule): bool
34
    {
35
        return \in_array($rule->getName(), self::VARIABLE_DEFINITIONS, true);
36
    }
37
38
    /**
39
     * @param ContextInterface $ctx
40
     * @param RuleInterface $rule
41
     * @return \Generator|mixed|void
42
     */
43
    public function reduce(ContextInterface $ctx, RuleInterface $rule)
44
    {
45
        /**
46
         * @var bool $isConstant
47
         * @var mixed $value
48
         */
49
        [$isConstant, $value] = [$this->isConstant($rule), yield $this->getValue($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...
50
51
        foreach ($rule->find('> #VariableName') as $name) {
52
            $variable = $name->first('> :T_VARIABLE')->getValue(1);
53
54
            $record = $ctx->declare($variable)->set($value);
55
56
            $isConstant ? $record->lock() : $record->unlock();
57
        }
58
    }
59
60
    /**
61
     * @param RuleInterface $rule
62
     * @return mixed
63
     */
64
    private function getValue(RuleInterface $rule)
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