Passed
Push — master ( b58c84...f28b68 )
by Théo
02:18
created

TraverserFactory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 76
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 1
eloc 53
c 6
b 0
f 0
nc 1
nop 3
dl 0
loc 76
ccs 17
cts 17
cp 1
crap 1
rs 9.0254

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\PhpParser;
16
17
use Humbug\PhpScoper\PhpParser\NodeVisitor\NamespaceStmt\NamespaceStmtCollection;
18
use Humbug\PhpScoper\PhpParser\NodeVisitor\Resolver\IdentifierResolver;
19
use Humbug\PhpScoper\PhpParser\NodeVisitor\UseStmt\UseStmtCollection;
20
use Humbug\PhpScoper\Reflector;
21
use Humbug\PhpScoper\Scoper\PhpScoper;
22
use Humbug\PhpScoper\Whitelist;
23
use PhpParser\NodeTraverserInterface;
24
use PhpParser\NodeVisitor as PhpParserNodeVisitor;
25
use PhpParser\NodeVisitor\NameResolver;
26
27
/**
28
 * @private
29
 */
30
class TraverserFactory
31
{
32 555
    private Reflector $reflector;
33
34 555
    public function __construct(Reflector $reflector)
35
    {
36
        $this->reflector = $reflector;
37 550
    }
38
39 550
    public function create(PhpScoper $scoper, string $prefix, Whitelist $whitelist): NodeTraverserInterface
40
    {
41 550
        $traverser = new NodeTraverser();
42 550
43
        $namespaceStatements = new NamespaceStmtCollection();
44 550
        $useStatements = new UseStmtCollection();
45
46 550
        $nameResolver = new NameResolver(
47
            null,
48 550
            ['preserveOriginalNames' => true],
49
        );
50 550
        $identifierResolver = new IdentifierResolver($nameResolver);
51 550
        $stringNodePrefixer = new StringNodePrefixer($scoper);
52
53 550
        self::addVisitors(
54 550
            $traverser,
55 550
            [
56 550
                $nameResolver,
57 550
                new NodeVisitor\ParentNodeAppender(),
58 550
                new NodeVisitor\IdentifierNameAppender($identifierResolver),
59
60 550
                new NodeVisitor\NamespaceStmt\NamespaceStmtPrefixer(
61 550
                    $prefix,
62
                    $whitelist,
63 550
                    $namespaceStatements,
64
                ),
65
66
                new NodeVisitor\UseStmt\UseStmtCollector(
67
                    $namespaceStatements,
68
                    $useStatements,
69
                ),
70
                new NodeVisitor\UseStmt\UseStmtPrefixer(
71
                    $prefix,
72
                    $whitelist,
73
                    $this->reflector,
74
                ),
75
76
                new NodeVisitor\NamespaceStmt\FunctionIdentifierRecorder(
77
                    $prefix,
78
                    $identifierResolver,
79
                    $whitelist,
80
                    $this->reflector,
81
                ),
82
                new NodeVisitor\ClassIdentifierRecorder(
83
                    $prefix,
84
                    $identifierResolver,
85
                    $whitelist
86
                ),
87
                new NodeVisitor\NameStmtPrefixer(
88
                    $prefix,
89
                    $whitelist,
90
                    $namespaceStatements,
91
                    $useStatements,
92
                    $this->reflector,
93
                ),
94
                new NodeVisitor\StringScalarPrefixer(
95
                    $prefix,
96
                    $whitelist,
97
                    $this->reflector,
98
                ),
99
                new NodeVisitor\NewdocPrefixer($stringNodePrefixer),
100
                new NodeVisitor\EvalPrefixer($stringNodePrefixer),
101
102
                new NodeVisitor\ClassAliasStmtAppender(
103
                    $prefix,
104
                    $whitelist,
105
                    $identifierResolver,
106
                ),
107
                new NodeVisitor\ConstStmtReplacer(
108
                    $whitelist,
109
                    $identifierResolver,
110
                ),
111
            ],
112
        );
113
114
        return $traverser;
115
    }
116
117
    /**
118
     * @param PhpParserNodeVisitor[] $nodeVisitors
119
     */
120
    private static function addVisitors(
121
        NodeTraverserInterface $nodeTraverser,
122
        array $nodeVisitors
123
    ): void
124
    {
125
        foreach ($nodeVisitors as $nodeVisitor) {
126
            $nodeTraverser->addVisitor($nodeVisitor);
127
        }
128
    }
129
}
130