Completed
Push — master ( db8578...85f9c3 )
by Kirill
10:30
created

RuleDelegate::reduce()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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\PP2\Delegate;
11
12
use Railt\Parser\Ast\NodeInterface;
13
use Railt\Parser\Ast\Rule;
14
use Railt\Parser\Ast\RuleInterface;
15
use Railt\Parser\Rule\Symbol;
16
17
/**
18
 * Class RuleDelegate
19
 */
20
class RuleDelegate extends Rule implements ProvidesChildrenSymbol
21
{
22
    /**
23
     * @return string
24
     */
25
    public function getRuleName(): string
26
    {
27
        return $this->name()->first('T_NAME')->getValue();
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\P...legate\BaseRuleDelegate, Railt\Compiler\Grammar\P...e\ConcatenationDelegate, Railt\Compiler\Grammar\P...elegate\IncludeDelegate, Railt\Compiler\Grammar\P...gate\InvocationDelegate, Railt\Compiler\Grammar\P...ragmaDefinitionDelegate, Railt\Compiler\Grammar\P...gate\RepetitionDelegate, Railt\Compiler\Grammar\PP2\Delegate\RuleDelegate, Railt\Compiler\Grammar\P...TokenDefinitionDelegate, 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...
28
    }
29
30
    /**
31
     * @return null|string
32
     */
33
    public function getDelegate(): ?string
34
    {
35
        $delegate = $this->name()->first('Delegate');
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\P...legate\BaseRuleDelegate, Railt\Compiler\Grammar\P...e\ConcatenationDelegate, Railt\Compiler\Grammar\P...elegate\IncludeDelegate, Railt\Compiler\Grammar\P...gate\InvocationDelegate, Railt\Compiler\Grammar\P...ragmaDefinitionDelegate, Railt\Compiler\Grammar\P...gate\RepetitionDelegate, Railt\Compiler\Grammar\PP2\Delegate\RuleDelegate, Railt\Compiler\Grammar\P...TokenDefinitionDelegate, 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...
36
37
        return $delegate ? $delegate->first('T_NAME')->getValue() : null;
38
    }
39
40
    /**
41
     * @return RuleInterface|NodeInterface
42
     */
43
    private function name(): RuleInterface
44
    {
45
        return $this->first('Name');
46
    }
47
48
    /**
49
     * @return bool
50
     */
51
    public function isKept(): bool
52
    {
53
        return $this->name()->first('ShouldKeep') !== null;
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\P...legate\BaseRuleDelegate, Railt\Compiler\Grammar\P...e\ConcatenationDelegate, Railt\Compiler\Grammar\P...elegate\IncludeDelegate, Railt\Compiler\Grammar\P...gate\InvocationDelegate, Railt\Compiler\Grammar\P...ragmaDefinitionDelegate, Railt\Compiler\Grammar\P...gate\RepetitionDelegate, Railt\Compiler\Grammar\PP2\Delegate\RuleDelegate, Railt\Compiler\Grammar\P...TokenDefinitionDelegate, 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...
54
    }
55
56
    /**
57
     * @return iterable
58
     */
59
    public function getChildrenRules(): iterable
60
    {
61
        return $this->getProduction()->getChildrenRules();
62
    }
63
64
    /**
65
     * @return Symbol
66
     */
67
    public function getRule(): Symbol
68
    {
69
        return $this->getProduction()->getRule();
0 ignored issues
show
Bug introduced by
The method getRule does only exist in Railt\Compiler\Grammar\PP2\Delegate\ProvidesSymbol, but not in Railt\Parser\Ast\NodeInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
70
    }
71
72
    /**
73
     * @return ProvidesSymbol|ConcatenationDelegate|NodeInterface
74
     */
75
    private function getProduction(): ProvidesSymbol
76
    {
77
        return $this->first('Production');
78
    }
79
}
80