Completed
Push — master ( f56a64...b6e499 )
by Théo
04:01 queued 01:18
created

PhpScoper::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
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\Scoper;
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 Humbug\PhpScoper\Scoper;
24
use PhpParser\Error as PhpParserError;
25
use PhpParser\NodeTraverser;
26
use PhpParser\NodeTraverserInterface;
27
use PhpParser\Parser;
28
use PhpParser\PrettyPrinter\Standard;
29
30
final class PhpScoper implements Scoper
31
{
32
    /** @internal */
33
    const FILE_PATTERN = '/.*\.php$/';
34
35
    private $parser;
36
    private $decoratedScoper;
37
38 34
    public function __construct(Parser $parser, Scoper $decoratedScoper)
39
    {
40 34
        $this->parser = $parser;
41 34
        $this->decoratedScoper = $decoratedScoper;
42 34
    }
43
44
    /**
45
     * Scopes PHP files.
46
     *
47
     * {@inheritdoc}
48
     *
49
     * @throws PhpParserError
50
     */
51 33
    public function scope(string $filePath, string $prefix): string
52
    {
53 33
        if (1 !== preg_match(self::FILE_PATTERN, $filePath)) {
54 1
            return $this->decoratedScoper->scope($filePath, $prefix);
55
        }
56
57 32
        $content = file_get_contents($filePath);
58
59 32
        $traverser = $this->createTraverser($prefix);
60
61 32
        $statements = $this->parser->parse($content);
62 31
        $statements = $traverser->traverse($statements);
63
64 31
        $prettyPrinter = new Standard();
65
66 31
        return $prettyPrinter->prettyPrintFile($statements)."\n";
67
    }
68
69 32
    private function createTraverser(string $prefix): NodeTraverserInterface
70
    {
71 32
        $traverser = new NodeTraverser();
72
73 32
        $traverser->addVisitor(new ParentNodeVisitor());
74 32
        $traverser->addVisitor(new IgnoreNamespaceScoperNodeVisitor());
75 32
        $traverser->addVisitor(new GroupUseNamespaceScoperNodeVisitor($prefix));
76 32
        $traverser->addVisitor(new NamespaceScoperNodeVisitor($prefix));
77 32
        $traverser->addVisitor(new UseNamespaceScoperNodeVisitor($prefix));
78 32
        $traverser->addVisitor(new FullyQualifiedNamespaceUseScoperNodeVisitor($prefix));
79
80 32
        return $traverser;
81
    }
82
}
83