-
-
Notifications
You must be signed in to change notification settings - Fork 357
/
scoper.php
154 lines (130 loc) · 5.6 KB
/
scoper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
declare(strict_types=1);
use Isolated\Symfony\Component\Finder\Finder;
use Nette\Utils\DateTime;
use Nette\Utils\Strings;
use Rector\Application\VersionResolver;
use Rector\Utils\Compiler\Unprefixer;
require_once __DIR__ . '/vendor/autoload.php';
// remove phpstan, because it is already prefixed in its own scope
$dateTime = DateTime::from('now');
$timestamp = $dateTime->format('Ym');
// @see https://github.com/humbug/php-scoper/blob/master/docs/further-reading.md
$polyfillsBootstraps = array_map(
static fn (SplFileInfo $fileInfo): string => $fileInfo->getPathname(),
iterator_to_array(
Finder::create()
->files()
->in(__DIR__ . '/vendor/symfony/polyfill-*')
->name('bootstrap*.php'),
false,
),
);
// see https://github.com/humbug/php-scoper/blob/master/docs/configuration.md#configuration
return [
'prefix' => 'RectorPrefix' . $timestamp,
// exclude
'exclude-classes' => [
'PHPUnit\Framework\Constraint\IsEqual',
'PHPUnit\Framework\TestCase',
'PHPUnit\Framework\ExpectationFailedException',
],
'exclude-namespaces' => [
'#^Rector#',
'#^PhpParser#',
'#^PHPStan#',
'#^Symplify\\\\RuleDocGenerator#',
'#^Symfony\\\\Polyfill#',
],
'exclude-files' => [...$polyfillsBootstraps],
// expose
'expose-classes' => ['Normalizer'],
'expose-functions' => [
'u',
'b',
's',
'trigger_deprecation',
'dump_with_depth',
'dn',
'dump_node',
'print_node',
],
'expose-constants' => ['__RECTOR_RUNNING__', '#^SYMFONY\_[\p{L}_]+$#'],
'patchers' => [
// fix short import bug, @see https://github.com/rectorphp/rector-scoper-017/blob/23f3256a6f5a18483d6eb4659d69ba117501e2e3/vendor/nikic/php-parser/lib/PhpParser/Builder/Declaration.php#L6
static fn (string $filePath, string $prefix, string $content): string => str_replace(
sprintf('use %s\PhpParser;', $prefix),
'use PhpParser;',
$content
),
static fn (string $filePath, string $prefix, string $content): string =>
// comment out
str_replace('\\' . $prefix . '\trigger_deprecation(', '// \trigger_deprecation(', $content),
// make external rules easier to write without enforing getRuleDefinition()
// as they are not designed for open-sourcing
// remove implements is the safest way to avoid error on conflict with real dependency of symplify/rule-doc-generator
static function (string $filePath, string $prefix, string $content): string {
if (! \str_ends_with($filePath, 'src/Contract/Rector/RectorInterface.php')) {
return $content;
}
// remove DocumentedRuleInterface implements
$content = str_replace(
'interface RectorInterface extends NodeVisitor, DocumentedRuleInterface',
'interface RectorInterface extends NodeVisitor',
$content
);
// remove use import itself, to make contract clean
return str_replace('use Symplify\RuleDocGenerator\Contract\DocumentedRuleInterface;', '', $content);
},
static function (string $filePath, string $prefix, string $content): string {
if (! \str_ends_with($filePath, 'src/Application/VersionResolver.php')) {
return $content;
}
$releaseDateTime = VersionResolver::resolverReleaseDateTime();
return strtr(
$content,
[
'@package_version@' => VersionResolver::resolvePackageVersion(),
'@release_date@' => $releaseDateTime->format('Y-m-d H:i:s'),
]
);
},
// un-prefix composer plugin
static function (string $filePath, string $prefix, string $content): string {
if (! \str_ends_with($filePath, 'vendor/rector/extension-installer/src/Plugin.php')) {
return $content;
}
// see https://regex101.com/r/v8zRMm/1
return Strings::replace($content, '#' . $prefix . '\\\\Composer\\\\#', 'Composer\\');
},
// unprefix string classes, as they're string on purpose - they have to be checked in original form, not prefixed
static function (string $filePath, string $prefix, string $content): string {
// skip vendor, expect rector packages
if (\str_contains($filePath, 'vendor/') && ! \str_contains($filePath, 'vendor/rector')) {
return $content;
}
// skip bin/rector.php for composer autoload class
if (\str_ends_with($filePath, 'bin/rector.php')) {
return $content;
}
return Unprefixer::unprefixQuoted($content, $prefix);
},
static function (string $filePath, string $prefix, string $content): string {
if (! \str_ends_with($filePath, 'vendor/nette/utils/src/Utils/Strings.php')) {
return $content;
}
# see https://github.com/rectorphp/rector/issues/8564
return str_replace(
'return self::pcre(\'preg_replace_callback\', [$pattern, $replacement, $subject, $limit, 0, $flags]);',
<<<'CODE_REPLACE'
if (PHP_VERSION_ID < 70400) {
return self::pcre('preg_replace_callback', [$pattern, $replacement, $subject, $limit]);
}
return self::pcre('preg_replace_callback', [$pattern, $replacement, $subject, $limit, 0, $flags]);
CODE_REPLACE
,
$content
);
},
],
];