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

RulesResolver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 2
dl 0
loc 49
c 0
b 0
f 0
ccs 0
cts 24
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A resolve() 0 6 2
A build() 0 17 4
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\Resolvers;
11
12
use Railt\Compiler\Grammar\PP2\Delegate\RuleDelegate;
13
use Railt\Compiler\Grammar\PP2\Mapping;
14
use Railt\Compiler\Grammar\PP2\ResolverInterface;
15
use Railt\Compiler\Reader\BaseRules;
16
use Railt\Io\Exception\ExternalFileException;
17
use Railt\Io\Readable;
18
use Railt\Parser\Ast\RuleInterface;
19
20
/**
21
 * Class PragmasResolver
22
 */
23
class RulesResolver extends BaseRules implements ResolverInterface
24
{
25
    /**
26
     * @var Mapping
27
     */
28
    private $map;
29
30
    /**
31
     * RulesResolver constructor.
32
     * @param Mapping $map
33
     */
34
    public function __construct(Mapping $map)
35
    {
36
        $this->map = $map;
37
    }
38
39
    /**
40
     * @param Readable $readable
41
     * @param RuleInterface|RuleDelegate $rule
42
     * @throws \LogicException
43
     */
44
    public function resolve(Readable $readable, RuleInterface $rule): void
45
    {
46
        if ($this->has($rule->getRuleName())) {
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface Railt\Parser\Ast\RuleInterface as the method getRuleName() does only exist in the following implementations of said interface: Railt\Compiler\Grammar\PP2\Delegate\RuleDelegate.

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...
47
            return;
48
        }
49
    }
50
51
    /**
52
     * @throws ExternalFileException
53
     */
54
    protected function build(): void
55
    {
56
        $current = $rule->getRule();
0 ignored issues
show
Bug introduced by
The variable $rule 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...
57
        $this->add($current);
58
59
        foreach ($rule->getChildrenRules() as $child) {
60
            $this->add($child);
61
        }
62
63
        try {
64
            if ($delegate = $rule->getDelegate()) {
0 ignored issues
show
Unused Code introduced by
$delegate is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
Unused Code introduced by
This if statement is empty and can be removed.

This check looks for the bodies of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These if bodies can be removed. If you have an empty if but statements in the else branch, consider inverting the condition.

if (rand(1, 6) > 3) {
//print "Check failed";
} else {
    print "Check succeeded";
}

could be turned into

if (rand(1, 6) <= 3) {
    print "Check succeeded";
}

This is much more concise to read.

Loading history...
65
                // Delegate: $rule->getRuleName() => $delegate
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
66
            }
67
        } catch (ExternalFileException $e) {
68
            throw $e->throwsIn($readable, $rule->getOffset());
69
        }
70
    }
71
}
72
73