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

RepetitionDelegate::getRule()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 6
c 0
b 0
f 0
ccs 0
cts 5
cp 0
rs 10
cc 1
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\Repetition;
16
use Railt\Parser\Rule\Symbol;
17
18
/**
19
 * Class RepetitionDelegate
20
 */
21
class RepetitionDelegate extends BaseRuleDelegate implements ProvidesChildrenSymbol
22
{
23
    /**
24
     * @return Symbol
25
     */
26
    public function getRule(): Symbol
27
    {
28
        [$from, $to] = $this->getInterval($this->first('RepetitionInterval'));
0 ignored issues
show
Bug introduced by
The variable $from 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...
Bug introduced by
The variable $to 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...
Documentation introduced by
$this->first('RepetitionInterval') 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...
29
30
        return new Repetition($this->getId(), $from, $to, $this->getChildrenRuleIds());
31
    }
32
33
    /**
34
     * @param RuleInterface|NodeInterface $repetition
35
     * @return array
36
     */
37
    private function getInterval(RuleInterface $repetition): array
38
    {
39
        switch (true) {
40
            case (bool)$repetition->first('T_ZERO_OR_MORE'):
41
                return [0, Repetition::INF_MAX_VALUE];
42
43
            case (bool)$repetition->first('T_ONE_OR_MORE'):
44
                return [1, Repetition::INF_MAX_VALUE];
45
46
            case (bool)$repetition->first('T_ZERO_OR_ONE'):
47
                return [0, 1];
48
49
            case (bool)($repeat = $repetition->first('Repeat')):
50
                $value = (int)$repeat->first('T_NUMBER')->getValue();
0 ignored issues
show
Bug introduced by
The variable $repeat seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
51
52
                return [$value, $value];
53
                break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
54
55
            default:
56
                $from = $to = Repetition::INF_MAX_VALUE;
57
58
                if ($nodeFrom = $repetition->first('From')) {
59
                    $from = (int)$nodeFrom->first('T_NUMBER')->getValue();
60
                }
61
62
                if ($nodeTo = $repetition->first('To')) {
63
                    $to = (int)$nodeTo->first('T_NUMBER')->getValue();
64
                }
65
66
                return [$from, $to];
67
        }
68
    }
69
}
70