|
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\PhpParser\TraverserFactory; |
|
18
|
|
|
use Humbug\PhpScoper\Scoper; |
|
19
|
|
|
use Humbug\PhpScoper\Whitelist; |
|
20
|
|
|
use PhpParser\Error as PhpParserError; |
|
21
|
|
|
use PhpParser\Parser; |
|
22
|
|
|
use PhpParser\PrettyPrinter\Standard; |
|
23
|
|
|
|
|
24
|
|
|
final class PhpScoper implements Scoper |
|
25
|
|
|
{ |
|
26
|
|
|
private const FILE_PATH_PATTERN = '/.*\.php$/'; |
|
27
|
|
|
private const NOT_FILE_BINARY = '/\..+?$/'; |
|
28
|
|
|
private const PHP_TAG = '/^<\?php/'; |
|
29
|
|
|
private const PHP_BINARY = '/^#!.+?php.*\n{1,}<\?php/'; |
|
30
|
|
|
|
|
31
|
|
|
private $parser; |
|
32
|
|
|
private $decoratedScoper; |
|
33
|
|
|
private $traverserFactory; |
|
34
|
|
|
|
|
35
|
546 |
|
public function __construct(Parser $parser, Scoper $decoratedScoper, TraverserFactory $traverserFactory) |
|
36
|
|
|
{ |
|
37
|
546 |
|
$this->parser = $parser; |
|
38
|
546 |
|
$this->decoratedScoper = $decoratedScoper; |
|
39
|
546 |
|
$this->traverserFactory = $traverserFactory; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Scopes PHP files. |
|
44
|
|
|
* |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
* |
|
47
|
|
|
* @throws PhpParserError |
|
48
|
|
|
*/ |
|
49
|
545 |
|
public function scope(string $filePath, string $contents, string $prefix, array $patchers, Whitelist $whitelist): string |
|
50
|
|
|
{ |
|
51
|
545 |
|
if (false === $this->isPhpFile($filePath, $contents)) { |
|
52
|
2 |
|
return $this->decoratedScoper->scope($filePath, $contents, $prefix, $patchers, $whitelist); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
543 |
|
return $this->scopePhp($contents, $prefix, $whitelist); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
543 |
|
public function scopePhp(string $php, string $prefix, Whitelist $whitelist): string |
|
59
|
|
|
{ |
|
60
|
543 |
|
$statements = $this->parser->parse($php); |
|
61
|
|
|
|
|
62
|
542 |
|
$traverser = $this->traverserFactory->create($this, $prefix, $whitelist); |
|
63
|
|
|
|
|
64
|
542 |
|
$statements = $traverser->traverse($statements); |
|
65
|
|
|
|
|
66
|
541 |
|
$prettyPrinter = new Standard(); |
|
67
|
|
|
|
|
68
|
541 |
|
return $prettyPrinter->prettyPrintFile($statements)."\n"; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
545 |
|
private function isPhpFile(string $filePath, string $contents): bool |
|
72
|
|
|
{ |
|
73
|
545 |
|
if (1 === preg_match(self::FILE_PATH_PATTERN, $filePath)) { |
|
74
|
541 |
|
return true; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
4 |
|
if (1 === preg_match(self::NOT_FILE_BINARY, basename($filePath))) { |
|
78
|
1 |
|
return false; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
3 |
|
if (1 === preg_match(self::PHP_TAG, ltrim($contents))) { |
|
82
|
1 |
|
return true; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
2 |
|
return 1 === preg_match(self::PHP_BINARY, $contents); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|