Passed
Push — master ( 449a59...2d8dc6 )
by Théo
02:06
created

FunctionCallScoperNodeVisitor   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 0
cts 18
cp 0
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
C enterNode() 0 28 8
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the humbug/php-scoper package.
7
 *
8
 * Copyright (c) 2017 Théo FIDRY <[email protected]>,
9
 *                    Pádraic Brady <[email protected]>
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace Humbug\PhpScoper\NodeVisitor;
16
17
use PhpParser\Node;
18
use PhpParser\NodeVisitorAbstract;
19
20
final class FunctionCallScoperNodeVisitor extends NodeVisitorAbstract
21
{
22
    private $prefix;
23
    private $functions;
24
25
    /**
26
     * @param string   $prefix
27
     * @param string[] $functions Functions which first parameter should be prefixed.
28
     */
29
    public function __construct($prefix, array $functions)
30
    {
31
        $this->prefix = $prefix;
32
        $this->functions = $functions;
33
    }
34
35
    /**
36
     * @inheritdoc
37
     */
38
    public function enterNode(Node $node)
39
    {
40
        if (!$node instanceof Node\Expr\FuncCall || null === $node->name) {
41
            return $node;
42
        }
43
44
        if (!$node->name instanceof Node\Name) {
45
            return $node;
46
        }
47
48
        if (!in_array($node->name->getFirst(), $this->functions)) {
49
            return $node;
50
        }
51
52
        $value = $node->args[0]->value;
53
        if (!$value instanceof Node\Scalar\String_) {
54
            return $node;
55
        }
56
57
        $stringValue = ltrim($value->value, '\\');
58
59
        // Do not prefix global namespace
60
        if (false !== strstr($stringValue, '\\')) {
61
            $value->value = ($value->value !== $stringValue ? '\\' : '').$this->prefix.'\\'.$stringValue;
62
        }
63
64
        return $node;
65
    }
66
}
67