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

Builder/Instruction/VariableReassigmentBuilder.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

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
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...
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