Completed
Push — master ( 8a5799...ed0b0a )
by Kirill
07:58
created

RuleDelegate::getDelegate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 0
cts 8
cp 0
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 0
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\Parser\Ast\Delegate;
13
use Railt\Parser\Ast\Rule;
14
use Railt\Parser\Ast\RuleInterface;
15
use Railt\Parser\Environment;
16
use Railt\Parser\Grammar;
17
use Railt\Parser\GrammarInterface;
18
19
/**
20
 * Class RuleDelegate
21
 */
22
class RuleDelegate extends Rule implements Delegate
23
{
24
    /**
25
     * @param Environment $env
26
     */
27
    public function boot(Environment $env): void
28
    {
29
        /** @var GrammarInterface|Grammar $grammar */
30
        $grammar = $env->get(GrammarInterface::class);
31
32
        if ($delegate = $this->getDelegate()) {
33
            $grammar->addDelegate($this->getRuleName(), $delegate);
0 ignored issues
show
Bug introduced by
The method addDelegate() does not exist on Railt\Parser\GrammarInterface. Did you maybe mean delegate()?

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...
34
        }
35
36
        // TODO Add analyzer
37
    }
38
39
    /**
40
     * @return string
41
     */
42
    public function getRuleName(): string
43
    {
44
        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...
45
            ->first('RuleName')
46
            ->first('T_NAME')
47
            ->getValue();
48
    }
49
50
    /**
51
     * @return bool
52
     */
53
    public function isKept(): bool
54
    {
55
        return (bool)$this->first('ShouldKeep');
56
    }
57
58
    /**
59
     * @return null|string
60
     */
61
    public function getDelegate(): ?string
62
    {
63
        $delegate = $this->first('RuleDelegate');
64
65
        if ($delegate instanceof RuleInterface) {
66
            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...
67
        }
68
69
        return null;
70
    }
71
}
72