|
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\Scoper; |
|
18
|
|
|
use PhpParser\Error as PhpParserError; |
|
19
|
|
|
use PhpParser\Parser; |
|
20
|
|
|
use PhpParser\PrettyPrinter\Standard; |
|
21
|
|
|
|
|
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 |
|
68
|
|
|
{ |
|
69
|
307 |
|
if (1 === preg_match(self::FILE_PATH_PATTERN, $filePath)) { |
|
70
|
305 |
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
2 |
|
if (1 === preg_match(self::NOT_FILE_BINARY, basename($filePath))) { |
|
74
|
1 |
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
$content = file_get_contents($filePath); |
|
78
|
|
|
|
|
79
|
1 |
|
return 1 === preg_match(self::PHP_BINARY, $content); |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|