Completed
Push — master ( dcddf7...d2fb4d )
by Théo
05:16 queued 03:22
created

ScopeFunctionCallStmtNodeVisitor::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 9

Duplication

Lines 11
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 4
dl 11
loc 11
rs 9.4285
c 0
b 0
f 0
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\FunctionStmt;
16
17
use Humbug\PhpScoper\NodeVisitor\NamespaceStmtCollection;
18
use Humbug\PhpScoper\NodeVisitor\UseStmtCollection;
19
use PhpParser\Node;
20
use PhpParser\Node\Expr\FuncCall;
21
use PhpParser\Node\Name;
22
use PhpParser\Node\Name\FullyQualified;
23
use PhpParser\NodeVisitorAbstract;
24
25
final class ScopeFunctionCallStmtNodeVisitor extends NodeVisitorAbstract
26
{
27
    private $prefix;
28
    private $namespaceStatements;
29
    private $useStatements;
30
    private $whitelist;
31
32 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
        string $prefix,
34
        NamespaceStmtCollection $namespaceStatements,
35
        UseStmtCollection $useStatements,
36
        array $whitelist
37
    ) {
38
        $this->prefix = $prefix;
39
        $this->namespaceStatements = $namespaceStatements;
40
        $this->useStatements = $useStatements;
41
        $this->whitelist = $whitelist;
42
    }
43
44
    /**
45
     * @inheritdoc
46
     */
47
    public function enterNode(Node $node): Node
48
    {
49 View Code Duplication
        if (false === ($node instanceof Name)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
            || false === $node->hasAttribute('parent')
51
            || ($node->hasAttribute('phpscoper_ignore') && $node->getAttribute('phpscoper_ignore'))
52
        ) {
53
            return $node;
54
        }
55
        /** @var Name $node */
56
        $parentNode = $node->getAttribute('parent');
57
58
        if (false === ($parentNode instanceof FuncCall)) {
59
            return $node;
60
        }
61
62
        if (1 === count($node->parts)) {
63
            return $node;
64
        }
65
66
        $useStatement = $this->useStatements->findStatementForName($node->getFirst());
67
68
        $prefix = false;
69
70 View Code Duplication
        if (null === $useStatement) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
71
            if (0 === count($this->namespaceStatements)) {
72
                $newNode = new FullyQualified($node, $node->getAttributes());
73
74
                $prefix = true;
75
            } else {
76
                $namespaceStatement = $this->namespaceStatements->getNamespaceName();
77
78
                $newNode = FullyQualified::concat($namespaceStatement, $node, $node->getAttributes());
79
            }
80
        } else {
81
            $newNode = FullyQualified::concat($useStatement, $node->slice(1), $node->getAttributes());
82
        }
83
84
        $newNode->setAttribute('phpscoper_ignore', true);
85
86
        if ($prefix) {
87
            return FullyQualified::concat($this->prefix, $newNode, $newNode->getAttributes());
88
        }
89
90
        return $node;
91
    }
92
}
93