Test Failed
Pull Request — master (#38)
by Théo
02:36
created

Scoper::createNodeTraverser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 1
dl 0
loc 13
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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;
16
17
use Humbug\PhpScoper\NodeVisitor\FullyQualifiedNamespaceUseScoperNodeVisitor;
18
use Humbug\PhpScoper\NodeVisitor\GroupUseNamespaceScoperNodeVisitor;
19
use Humbug\PhpScoper\NodeVisitor\IgnoreNamespaceScoperNodeVisitor;
20
use Humbug\PhpScoper\NodeVisitor\NamespaceScoperNodeVisitor;
21
use Humbug\PhpScoper\NodeVisitor\ParentNodeVisitor;
22
use Humbug\PhpScoper\NodeVisitor\UseNamespaceScoperNodeVisitor;
23
use PhpParser\Error;
24
use PhpParser\NodeTraverser;
25
use PhpParser\NodeTraverserInterface;
26
use PhpParser\Parser;
27
use PhpParser\PrettyPrinter\Standard;
28
29
/**
30
 * @final
31
 */
32
class Scoper
33
{
34
    private $parser;
35 22
36
    public function __construct(Parser $parser)
37 22
    {
38 22
        $this->parser = $parser;
39
    }
40
41
    /**
42
     * @param string $content Content of the file to scope
43
     * @param string $prefix  Prefix to apply to the file
44
     *
45
     * @throws Error
46
     *
47
     * @return string Content of the file with the prefix applied
48 22
     */
49
    public function scope(string $content, string $prefix): string
50 22
    {
51 22
        $traverser = $this->createNodeTraverser($prefix);
52 22
53 22
        $statements = $this->parser->parse($content);
54 22
        $statements = $traverser->traverse($statements);
1 ignored issue
show
Bug introduced by
It seems like $statements can also be of type null; however, PhpParser\NodeTraverserInterface::traverse() does only seem to accept array<integer,object<PhpParser\Node>>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
55 22
56
        $prettyPrinter = new Standard();
57
58 22
        return $prettyPrinter->prettyPrintFile($statements)."\n";
59 1
    }
60 1
61
    private function createNodeTraverser(string $prefix): NodeTraverserInterface
62
    {
63 21
        $traverser = new NodeTraverser();
64
65 21
        $traverser->addVisitor(new ParentNodeVisitor());
66
        $traverser->addVisitor(new IgnoreNamespaceScoperNodeVisitor());
67 21
        $traverser->addVisitor(new GroupUseNamespaceScoperNodeVisitor($prefix));
68
        $traverser->addVisitor(new NamespaceScoperNodeVisitor($prefix));
69
        $traverser->addVisitor(new UseNamespaceScoperNodeVisitor($prefix));
70
        $traverser->addVisitor(new FullyQualifiedNamespaceUseScoperNodeVisitor($prefix));
71
72
        return $traverser;
73
    }
74
}
75