Passed
Push — master ( 01e41e...21756f )
by Théo
02:37
created

TraverserFactory::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
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\NodeTraverser;
18
use Humbug\PhpScoper\NodeVisitor;
19
use Humbug\PhpScoper\NodeVisitor\Collection\NamespaceStmtCollection;
20
use Humbug\PhpScoper\NodeVisitor\Collection\UseStmtCollection;
21
use Humbug\PhpScoper\NodeVisitor\Resolver\FullyQualifiedNameResolver;
22
use Humbug\PhpScoper\Reflector;
23
use PhpParser\NodeTraverserInterface;
24
25
/**
26
 * @final
27
 */
28
class TraverserFactory
29
{
30
    /**
31
     * Functions for which the arguments will be prefixed.
32
     */
33
    public const WHITELISTED_FUNCTIONS = [
34
        'class_exists',
35
        'interface_exists',
36
    ];
37
38
    private $reflector;
39
40 344
    public function __construct(Reflector $reflector)
41
    {
42 344
        $this->reflector = $reflector;
43
    }
44
45
    /**
46
     * @param string   $prefix    Prefix to apply to the files.
47
     * @param string[] $whitelist List of classes to exclude from the scoping.
48
     *
49
     * @return NodeTraverserInterface
50
     */
51 339
    public function create(string $prefix, array $whitelist): NodeTraverserInterface
52
    {
53 339
        $traverser = new NodeTraverser($prefix);
54
55 339
        $namespaceStatements = new NamespaceStmtCollection();
56 339
        $useStatements = new UseStmtCollection();
57
58 339
        $nameResolver = new FullyQualifiedNameResolver($namespaceStatements, $useStatements);
59
60 339
        $traverser->addVisitor(new NodeVisitor\AppendParentNode());
61
62 339
        $traverser->addVisitor(new NodeVisitor\NamespaceStmtPrefixer($prefix, $namespaceStatements));
63
64 339
        $traverser->addVisitor(new NodeVisitor\UseStmt\UseStmtCollector($namespaceStatements, $useStatements));
65 339
        $traverser->addVisitor(new NodeVisitor\UseStmt\UseStmtPrefixer($prefix, $whitelist, $this->reflector));
66
67 339
        $traverser->addVisitor(new NodeVisitor\NameStmtPrefixer($prefix, $nameResolver, $this->reflector));
68 339
        $traverser->addVisitor(new NodeVisitor\StringScalarPrefixer($prefix, self::WHITELISTED_FUNCTIONS, $whitelist, $this->reflector));
69
70 339
        $traverser->addVisitor(new NodeVisitor\WhitelistedClassAppender($whitelist));
71
72 339
        return $traverser;
73
    }
74
}
75