Test Failed
Pull Request — master (#37)
by Divine Niiquaye
02:59
created

MethodsResolver::enterNode()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 11
c 1
b 0
f 0
dl 0
loc 19
rs 8.8333
cc 7
nc 3
nop 1
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
/**
21
 * Fixes container's class methods name conflict.
22
 *
23
 * @author Divine Niiquaye Ibok <[email protected]>
24
 */
25
class MethodsResolver extends \PhpParser\NodeVisitorAbstract
26
{
27
    private array $replacement = [];
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function enterNode(\PhpParser\Node $node)
33
    {
34
        if ($node instanceof \PhpParser\Node\Stmt\PropertyProperty && 'methodsMap' === $node->name->name) {
35
            $sameNaming = [];
36
37
            foreach ($node->default->items as $item) {
38
                $lcName = \strtolower($item->key->value);
39
40
                if (\array_key_exists($lcName, $sameNaming)) {
41
                    $this->replacement[$item->value->value][] = $item->value->value .= $sameNaming[$lcName]++;
42
                } else {
43
                    $sameNaming[$lcName] = 0;
44
                }
45
            }
46
        } elseif ($node instanceof \PhpParser\Node\Stmt\ClassMethod && \array_key_exists($node->name->name, $this->replacement)) {
47
            $node->name->name = \array_shift($this->replacement[$node->name->name]);
48
        }
49
50
        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...
51
    }
52
}
53