Completed
Push — master ( 598945...316866 )
by Kirill
09:09
created

RuleDelegate::boot()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 7
cp 0
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
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\Compiler\Grammar\Delegate;
11
12
use Railt\Compiler\Grammar\LookaheadIterator;
13
use Railt\Lexer\Result\Eoi;
14
use Railt\Lexer\Result\Token;
15
use Railt\Lexer\TokenInterface;
16
use Railt\Parser\Ast\Delegate;
17
use Railt\Parser\Ast\LeafInterface;
18
use Railt\Parser\Ast\NodeInterface;
19
use Railt\Parser\Ast\Rule;
20
use Railt\Parser\Ast\RuleInterface;
21
use Railt\Parser\Environment;
22
use Railt\Parser\Grammar;
23
use Railt\Parser\GrammarInterface;
24
25
/**
26
 * Class RuleDelegate
27
 */
28
class RuleDelegate extends Rule
29
{
30
    /**
31
     * @return iterable|TokenInterface[]|LookaheadIterator
32
     */
33
    public function getInnerTokens(): iterable
34
    {
35
        return new LookaheadIterator((function() {
36
            yield from $this->getTokens($this->first('RuleProduction'));
0 ignored issues
show
Documentation introduced by
$this->first('RuleProduction') is of type null|object<Railt\Parser\Ast\NodeInterface>, but the function expects a object<Railt\Parser\Ast\RuleInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
37
            yield new Eoi(0);
38
        })->call($this));
39
    }
40
41
    /**
42
     * @param RuleInterface|NodeInterface $rule
43
     * @return \Traversable
44
     */
45
    private function getTokens(RuleInterface $rule): \Traversable
46
    {
47
        /** @var LeafInterface $child */
48
        foreach ($rule->getChildren() as $child) {
49
            if ($child instanceof RuleInterface) {
50
                yield from $this->getTokens($child);
51
            } else {
52
                yield new Token($child->getName(), $child->getValues(), $child->getOffset());
53
            }
54
        }
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public function getRuleName(): string
61
    {
62
        return $this
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 first() 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...
63
            ->first('RuleName')
64
            ->first('T_NAME')
65
            ->getValue();
66
    }
67
68
    /**
69
     * @return bool
70
     */
71
    public function isKept(): bool
72
    {
73
        return (bool)$this->first('ShouldKeep');
74
    }
75
76
    /**
77
     * @return null|string
78
     */
79
    public function getDelegate(): ?string
80
    {
81
        $delegate = $this->first('RuleDelegate');
82
83
        if ($delegate instanceof RuleInterface) {
84
            return $delegate->first('T_NAME')->getValue();
0 ignored issues
show
Bug introduced by
The method getValue() does not exist on Railt\Parser\Ast\NodeInterface. Did you maybe mean getValues()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
85
        }
86
87
        return null;
88
    }
89
}
90