Passed
Pull Request — master (#496)
by Théo
01:49
created

TraverserFactory::create()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 80
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 56
c 3
b 0
f 0
nc 1
nop 3
dl 0
loc 80
ccs 17
cts 17
cp 1
crap 1
rs 8.9599

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\FullyQualifiedNameResolver;
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 FullyQualifiedNameResolver($namespaceStatements, $useStatements);
47
        $newNameResolver = new NameResolver();
48 550
49
        self::addVisitors(
50 550
            $traverser,
51 550
            [
52
                $newNameResolver,
53 550
                new NodeVisitor\ParentNodeAppender(),
54 550
55 550
                new NodeVisitor\NamespaceStmt\NamespaceStmtPrefixer(
56 550
                    $prefix,
57 550
                    $whitelist,
58 550
                    $namespaceStatements,
59
                ),
60 550
61 550
                new NodeVisitor\UseStmt\UseStmtCollector(
62
                    $namespaceStatements,
63 550
                    $useStatements,
64
                ),
65
                new NodeVisitor\UseStmt\UseStmtPrefixer(
66
                    $prefix,
67
                    $whitelist,
68
                    $this->reflector,
69
                ),
70
71
                new NodeVisitor\NamespaceStmt\FunctionIdentifierRecorder(
72
                    $prefix,
73
                    $nameResolver,
74
                    $whitelist,
75
                    $this->reflector,
76
                ),
77
                new NodeVisitor\ClassIdentifierRecorder(
78
                    $prefix,
79
                    $nameResolver,
80
                    $whitelist
81
                ),
82
                new NodeVisitor\NameStmtPrefixer(
83
                    $prefix,
84
                    $whitelist,
85
                    $namespaceStatements,
86
                    $useStatements,
87
                    $nameResolver,
88
                    $this->reflector,
89
                ),
90
                new NodeVisitor\StringScalarPrefixer(
91
                    $prefix,
92
                    $whitelist,
93
                    $this->reflector,
94
                ),
95
                new NodeVisitor\NewdocPrefixer(
96
                    $scoper,
97
                    $prefix,
98
                    $whitelist,
99
                ),
100
                new NodeVisitor\EvalPrefixer(
101
                    $scoper,
102
                    $prefix,
103
                    $whitelist,
104
                ),
105
106
                new NodeVisitor\ClassAliasStmtAppender(
107
                    $prefix,
108
                    $whitelist,
109
                    $nameResolver,
110
                ),
111
                new NodeVisitor\ConstStmtReplacer(
112
                    $whitelist,
113
                    $nameResolver,
114
                ),
115
            ],
116
        );
117
118
        return $traverser;
119
    }
120
121
    /**
122
     * @param PhpParserNodeVisitor[] $nodeVisitors
123
     */
124
    private static function addVisitors(
125
        NodeTraverserInterface $nodeTraverser,
126
        array $nodeVisitors
127
    ): void
128
    {
129
        foreach ($nodeVisitors as $nodeVisitor) {
130
            $nodeTraverser->addVisitor($nodeVisitor);
131
        }
132
    }
133
}
134