Completed
Push — master ( 74c262...0b4843 )
by Théo
03:38 queued 01:33
created

TraverserFactory   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 11

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 10
c 0
b 0
f 0
ccs 0
cts 15
cp 0
wmc 2
lcom 1
cbo 11

1 Method

Rating   Name   Duplication   Size   Complexity  
B create() 0 27 2
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\Collection\NamespaceStmtCollection;
19
use Humbug\PhpScoper\NodeVisitor\Collection\UseStmtCollection;
20
use Humbug\PhpScoper\NodeVisitor\Resolver\FullyQualifiedNameResolver;
21
use PhpParser\NodeTraverser;
22
use PhpParser\NodeTraverserInterface;
23
24
final class TraverserFactory
25
{
26
    /**
27
     * Functions for which the arguments will be prefixed.
28
     */
29
    const WHITELISTED_FUNCTIONS = [
30
        'class_exists',
31
        'interface_exists',
32
    ];
33
34
    private $traverser;
35
36
    /**
37
     * @param string   $prefix            Prefix to apply to the files.
38
     * @param string[] $whitelist         List of classes to exclude from the scoping.
39
     * @param callable $globalWhitelister Closure taking a class name from the global namespace as an argument and
40
     *                                    returning a boolean which if `true` means the class should be scoped
41
     *                                    (i.e. is ignored) or scoped otherwise.
42
     *
43
     * @return NodeTraverserInterface
44
     */
45
    public function create(string $prefix, array $whitelist, callable $globalWhitelister): NodeTraverserInterface
46
    {
47
        if (null !== $this->traverser) {
48
            return $this->traverser;
49
        }
50
51
        $this->traverser = new NodeTraverser();
52
53
        $namespaceStatements = new NamespaceStmtCollection();
54
        $useStatements = new UseStmtCollection();
55
56
        $nameResolver = new FullyQualifiedNameResolver($namespaceStatements, $useStatements);
57
58
        $this->traverser->addVisitor(new NodeVisitor\UseStmt\GroupUseStmtTransformer());
59
60
        $this->traverser->addVisitor(new NodeVisitor\AppendParentNode());
61
62
        $this->traverser->addVisitor(new NodeVisitor\NamespaceStmtPrefixer($prefix, $namespaceStatements));
63
64
        $this->traverser->addVisitor(new NodeVisitor\UseStmt\UseStmtCollector($namespaceStatements, $useStatements));
65
        $this->traverser->addVisitor(new NodeVisitor\UseStmt\UseStmtPrefixer($prefix, $whitelist, $globalWhitelister));
66
67
        $this->traverser->addVisitor(new NodeVisitor\NameStmtPrefixer($prefix, $whitelist, $globalWhitelister, $nameResolver));
68
        $this->traverser->addVisitor(new NodeVisitor\StringScalarPrefixer($prefix, $whitelist, $globalWhitelister, $nameResolver));
69
70
        return $this->traverser;
71
    }
72
}
73