Passed
Push — master ( 2944f7...cee3f8 )
by Théo
02:04
created

TraverserFactory::create()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 78
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 17
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 54
c 2
b 0
f 0
nc 1
nop 3
dl 0
loc 78
ccs 17
cts 17
cp 1
crap 1
rs 9.0036

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