Completed
Push — master ( fd91f4...d6930c )
by Théo
03:58 queued 01:56
created

PhpScoper   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 12

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 73
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0
wmc 7
lcom 1
cbo 12

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A scope() 0 17 2
A isPhpFile() 0 14 3
A createTraverser() 0 14 1
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\SingleLevelUseAliasVisitor;
23
use Humbug\PhpScoper\NodeVisitor\UseNamespaceScoperNodeVisitor;
24
use Humbug\PhpScoper\Scoper;
25
use PhpParser\Error as PhpParserError;
26
use PhpParser\NodeTraverser;
27
use PhpParser\NodeTraverserInterface;
28
use PhpParser\Parser;
29
use PhpParser\PrettyPrinter\Standard;
30
31
final class PhpScoper implements Scoper
32
{
33
    /** @internal */
34
    const FILE_PATH_PATTERN = '/.*\.php$/';
35
    /** @internal */
36
    const NOT_FILE_BINARY = '/\..+?$/';
37
    /** @internal */
38
    const PHP_BINARY = '/^#!.+?php.*\n{1,}<\?php/';
39
40
    private $parser;
41
    private $decoratedScoper;
42
43 40
    public function __construct(Parser $parser, Scoper $decoratedScoper)
44
    {
45 40
        $this->parser = $parser;
46 40
        $this->decoratedScoper = $decoratedScoper;
47 40
    }
48
49
    /**
50
     * Scopes PHP files.
51
     *
52
     * {@inheritdoc}
53
     *
54
     * @throws PhpParserError
55
     */
56 39
    public function scope(string $filePath, string $prefix): string
57
    {
58 39
        if (false === $this->isPhpFile($filePath)) {
59 2
            return $this->decoratedScoper->scope($filePath, $prefix);
60
        }
61
62 37
        $content = file_get_contents($filePath);
63
64 37
        $traverser = $this->createTraverser($prefix);
65
66 37
        $statements = $this->parser->parse($content);
67 36
        $statements = $traverser->traverse($statements);
68
69 36
        $prettyPrinter = new Standard();
70
71 36
        return $prettyPrinter->prettyPrintFile($statements)."\n";
72
    }
73
74 39
    private function isPhpFile(string $filePath): bool
75
    {
76 39
        if (1 === preg_match(self::FILE_PATH_PATTERN, $filePath)) {
77 36
            return true;
78
        }
79
80 3
        if (1 === preg_match(self::NOT_FILE_BINARY, basename($filePath))) {
81 1
            return false;
82
        }
83
84 2
        $content = file_get_contents($filePath);
85
86 2
        return 1 === preg_match(self::PHP_BINARY, $content);
87
    }
88
89 37
    private function createTraverser(string $prefix): NodeTraverserInterface
90
    {
91 37
        $traverser = new NodeTraverser();
92
93 37
        $traverser->addVisitor(new ParentNodeVisitor());
94 37
        $traverser->addVisitor(new SingleLevelUseAliasVisitor($prefix));
95 37
        $traverser->addVisitor(new IgnoreNamespaceScoperNodeVisitor());
96 37
        $traverser->addVisitor(new GroupUseNamespaceScoperNodeVisitor($prefix));
97 37
        $traverser->addVisitor(new NamespaceScoperNodeVisitor($prefix));
98 37
        $traverser->addVisitor(new UseNamespaceScoperNodeVisitor($prefix));
99 37
        $traverser->addVisitor(new FullyQualifiedNamespaceUseScoperNodeVisitor($prefix));
100
101 37
        return $traverser;
102
    }
103
}
104