Passed
Push — master ( 2a3624...3abe14 )
by Théo
03:10
created

Scoper   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 39
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 3
lcom 1
cbo 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A scope() 0 21 2
1
<?php
2
3
/*
4
 * This file is part of the webmozart/php-scoper package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Humbug\PhpScoper;
13
14
use Humbug\PhpScoper\NodeVisitor\FullyQualifiedNamespaceUseScoperNodeVisitor;
15
use Humbug\PhpScoper\NodeVisitor\GroupUseNamespaceScoperNodeVisitor;
16
use Humbug\PhpScoper\NodeVisitor\NamespaceScoperNodeVisitor;
17
use Humbug\PhpScoper\NodeVisitor\ParentNodeVisitor;
18
use Humbug\PhpScoper\NodeVisitor\UseNamespaceScoperNodeVisitor;
19
use Humbug\PhpScoper\Throwable\Exception\ParsingException;
20
use PhpParser\Error;
21
use PhpParser\NodeTraverser;
22
use PhpParser\Parser;
23
use PhpParser\PrettyPrinter\Standard;
24
25
/**
26
 * @final
27
 */
28
class Scoper
29
{
30
    private $parser;
31
32 22
    public function __construct(Parser $parser)
33
    {
34 22
        $this->parser = $parser;
35 22
    }
36
37
    /**
38
     * @param string $content Content of the file to scope
39
     * @param string $prefix  Prefix to apply to the file
40
     *
41
     * @throws ParsingException
42
     *
43
     * @return string Content of the file with the prefix applied
44
     */
45 22
    public function scope(string $content, string $prefix): string
46
    {
47 22
        $traverser = new NodeTraverser();
48 22
        $traverser->addVisitor(new ParentNodeVisitor());
49 22
        $traverser->addVisitor(new GroupUseNamespaceScoperNodeVisitor($prefix));
50 22
        $traverser->addVisitor(new NamespaceScoperNodeVisitor($prefix));
51 22
        $traverser->addVisitor(new UseNamespaceScoperNodeVisitor($prefix));
52 22
        $traverser->addVisitor(new FullyQualifiedNamespaceUseScoperNodeVisitor($prefix));
53
54
        try {
55 22
            $statements = $this->parser->parse($content);
56 1
        } catch (Error $error) {
57 1
            throw new ParsingException($error->getMessage(), 0, $error);
58
        }
59
60 21
        $statements = $traverser->traverse($statements);
0 ignored issues
show
Bug introduced by
It seems like $statements can also be of type null; however, PhpParser\NodeTraverser::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...
61
62 21
        $prettyPrinter = new Standard();
63
64 21
        return $prettyPrinter->prettyPrintFile($statements)."\n";
65
    }
66
}
67