Test Failed
Push — master ( a0eed4...bb00ac )
by Kirill
149:40
created

TypeHintNode   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
dl 0
loc 58
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 22 5
A analyze() 0 9 2
A getModifiers() 0 4 1
1
<?php
2
/**
3
 * This file is part of sdl 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\SDL\Frontend\AST\Definition\Common;
11
12
use Railt\Parser\Ast\NodeInterface;
13
use Railt\Parser\Ast\Rule;
14
use Railt\Parser\Ast\RuleInterface;
15
use Railt\SDL\IR\TypeHint;
16
17
/**
18
 * Class TypeHintNode
19
 */
20
class TypeHintNode extends Rule
21
{
22
    /**
23
     * @var TypeHint
24
     */
25
    private $hint;
26
27
    /**
28
     * TypeHintNode constructor.
29
     * @param string $name
30
     * @param array $children
31
     * @param int $offset
32
     */
33
    public function __construct(string $name, array $children = [], int $offset = 0)
34
    {
35
        parent::__construct($name, $children, $offset);
36
37
        $this->hint = new TypeHint();
38
39
        foreach ($this->analyze($this->getChild(0)) as $child) {
0 ignored issues
show
Documentation introduced by
$this->getChild(0) 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...
40
            switch ($child) {
41
                case 'List':
42
                    $this->hint->withModifiers(TypeHint::IS_LIST);
43
                    break;
44
45
                case 'NonNull':
46
                    $modifier = $this->hint->isList()
47
                        ? TypeHint::IS_LIST_OF_NOT_NULL
48
                        : TypeHint::IS_NOT_NULL;
49
50
                    $this->hint->withModifiers($modifier);
51
                    break;
52
            }
53
        }
54
    }
55
56
    /**
57
     * @param RuleInterface|NodeInterface $rule
58
     * @return \Generator
59
     */
60
    private function analyze(RuleInterface $rule): \Generator
61
    {
62
        $name = $rule->getName();
63
64
        if ($name !== 'TypeName') {
65
            yield $name;
66
            yield from $this->analyze($rule->getChild(0));
0 ignored issues
show
Documentation introduced by
$rule->getChild(0) 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...
67
        }
68
    }
69
70
    /**
71
     * @return int
72
     */
73
    public function getModifiers(): int
74
    {
75
        return $this->hint->getModifiers();
76
    }
77
}
78