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

TraverserFactory::create()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 36
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 24
nc 2
nop 3
dl 0
loc 36
ccs 0
cts 24
cp 0
crap 6
rs 8.8571
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\Scoper;
16
17
use Humbug\PhpScoper\NodeVisitor;
18
use Humbug\PhpScoper\NodeVisitor\UseStmtCollection;
19
use Humbug\PhpScoper\NodeVisitor\WhitelistedStatements;
20
use PhpParser\NodeTraverser;
21
use PhpParser\NodeTraverserInterface;
22
23
/**
24
 * @internal
25
 */
26
final class TraverserFactory
27
{
28
    /**
29
     * Functions for which the arguments will be prefixed.
30
     */
31
    const WHITELISTED_FUNCTIONS = [
32
        'class_exists',
33
        'interface_exists',
34
    ];
35
36
    private $traverser;
37
38
    public function create(string $prefix, array $whitelist, callable $globalWhitelister): NodeTraverserInterface
39
    {
40
        if (null !== $this->traverser) {
41
            return $this->traverser;
42
        }
43
44
        $this->traverser = new NodeTraverser();
45
46
        $namespaceStatements = new NodeVisitor\NamespaceStmtCollection();
47
        $useStatements = new UseStmtCollection();
48
        $whitelistedStatements = new WhitelistedStatements();
49
50
        $this->traverser->addVisitor(new NodeVisitor\ParentNodeVisitor());
51
52
        $this->traverser->addVisitor(new NodeVisitor\CollectNamespaceStmtNodeVisitor($namespaceStatements));
53
        $this->traverser->addVisitor(new NodeVisitor\IgnoreNamespaceScoperNodeVisitor($whitelist, $globalWhitelister));
54
        $this->traverser->addVisitor(new NodeVisitor\ScopeNamespaceStmtNodeVisitor($prefix));
55
56
        $this->traverser->addVisitor(new NodeVisitor\UseStmt\CollectUseStmtNodeVisitor($useStatements));
57
        $this->traverser->addVisitor(new NodeVisitor\UseStmt\ScopeUseStmtNodeVisitor($prefix, $whitelist, $whitelistedStatements));
58
        $this->traverser->addVisitor(new NodeVisitor\UseStmt\ScopeSingleLevelUseAliasVisitor($prefix));
59
        $this->traverser->addVisitor(new NodeVisitor\UseStmt\ScopeGroupUseStmtNodeVisitor($prefix));
60
61
        $this->traverser->addVisitor(new NodeVisitor\ScopeFullyQualifiedNodeVisitor($prefix));
62
        $this->traverser->addVisitor(new NodeVisitor\ScopeWhitelistedElementsFromGlobalNamespaceNodeVisitor($prefix, $globalWhitelister));
63
        $this->traverser->addVisitor(new NodeVisitor\ScopeConstStmtNodeVisitor($prefix, $namespaceStatements, $useStatements, $whitelist));
64
65
        $this->traverser->addVisitor(new NodeVisitor\NewStmt\ScopeNewStmtNodeVisitor($prefix, $namespaceStatements, $useStatements, $whitelist));
66
        $this->traverser->addVisitor(new NodeVisitor\NewStmt\ScopeSingleLevelNewStmtNodeVisitor($prefix, $namespaceStatements, $useStatements, $whitelist));
67
68
        $this->traverser->addVisitor(new NodeVisitor\FunctionStmt\ScopeFunctionCallArgumentsStmtNodeVisitor($prefix, $whitelist, self::WHITELISTED_FUNCTIONS));
69
        $this->traverser->addVisitor(new NodeVisitor\FunctionStmt\ScopeStaticCallStmtNodeVisitor($prefix, $namespaceStatements, $useStatements, $whitelist));
70
        $this->traverser->addVisitor(new NodeVisitor\FunctionStmt\ScopeFunctionCallStmtNodeVisitor($prefix, $namespaceStatements, $useStatements, $whitelist));
71
72
        return $this->traverser;
73
    }
74
}
75