Test Failed
Pull Request — master (#37)
by Divine Niiquaye
12:23
created

PhpLiteralVisitor   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
eloc 10
c 4
b 0
f 0
dl 0
loc 29
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A enterNode() 0 13 4
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of DivineNii opensource projects.
7
 *
8
 * PHP version 7.4 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2021 DivineNii (https://divinenii.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace Rade\DI\NodeVisitor;
19
20
use PhpParser\BuilderHelpers;
21
use PhpParser\Node\Scalar\String_;
22
23
/**
24
 * PhpLiteral Node Visitor.
25
 *
26
 * @author Divine Niiquaye Ibok <[email protected]>
27
 */
28
final class PhpLiteralVisitor extends \PhpParser\NodeVisitorAbstract
29
{
30
    private int $offset = -1;
31
    private array $args;
32
33
    /**
34
     * @param array<int,mixed> $args
35
     */
36
    public function __construct(array $args)
37
    {
38
        $this->args = $args;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function enterNode(\PhpParser\Node $node)
45
    {
46
        if ($node instanceof String_ && '%?' === $node->value) {
47
            $value = $this->args[++$this->offset] ?? null;
48
49
            if (null === $value) {
50
                throw new \ParseError('Unable to parse syntax "%?" as no value supplied for its string node expression.');
51
            }
52
53
            return BuilderHelpers::normalizeValue($value);
54
        }
55
56
        return parent::enterNode($node);
0 ignored issues
show
Bug introduced by
Are you sure the usage of parent::enterNode($node) targeting PhpParser\NodeVisitorAbstract::enterNode() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
57
    }
58
}
59