1 | <?php |
||
22 | final class PhpScoper implements Scoper |
||
23 | { |
||
24 | /** @internal */ |
||
25 | const FILE_PATH_PATTERN = '/.*\.php$/'; |
||
26 | /** @internal */ |
||
27 | const NOT_FILE_BINARY = '/\..+?$/'; |
||
28 | /** @internal */ |
||
29 | const PHP_BINARY = '/^#!.+?php.*\n{1,}<\?php/'; |
||
30 | |||
31 | private $parser; |
||
32 | private $decoratedScoper; |
||
33 | private $traverserFactory; |
||
34 | |||
35 | 308 | public function __construct(Parser $parser, Scoper $decoratedScoper) |
|
36 | { |
||
37 | 308 | $this->parser = $parser; |
|
38 | 308 | $this->decoratedScoper = $decoratedScoper; |
|
39 | 308 | $this->traverserFactory = new TraverserFactory(); |
|
40 | } |
||
41 | |||
42 | /** |
||
43 | * Scopes PHP files. |
||
44 | * |
||
45 | * {@inheritdoc} |
||
46 | * |
||
47 | * @throws PhpParserError |
||
48 | */ |
||
49 | 307 | public function scope(string $filePath, string $prefix, array $patchers, array $whitelist, callable $globalWhitelister): string |
|
50 | { |
||
51 | 307 | if (false === $this->isPhpFile($filePath)) { |
|
52 | 2 | return $this->decoratedScoper->scope($filePath, $prefix, $patchers, $whitelist, $globalWhitelister); |
|
53 | } |
||
54 | |||
55 | 305 | $content = file_get_contents($filePath); |
|
56 | |||
57 | 305 | $traverser = $this->traverserFactory->create($prefix, $whitelist, $globalWhitelister); |
|
58 | |||
59 | 305 | $statements = $this->parser->parse($content); |
|
60 | 304 | $statements = $traverser->traverse($statements); |
|
61 | |||
62 | 304 | $prettyPrinter = new Standard(); |
|
63 | |||
64 | 304 | return $prettyPrinter->prettyPrintFile($statements)."\n"; |
|
65 | } |
||
66 | |||
67 | 307 | private function isPhpFile(string $filePath): bool |
|
81 | } |
||
82 |