[Scoped] Handle Cannot use RectorPrefix20210329parent as class name as it is reserved on Build docker image (#6009)

* [Scoped] Fix #6007 Handle Cannot use RectorPrefix20210329parent as class name as it is reserved on Build docker image

* regex update to check \ prefix before Prefix namespace
This commit is contained in:
Abdul Malik Ikhsan 2021-03-30 01:04:23 +07:00 committed by GitHub
parent 5fa858b072
commit 9229279980
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -63,6 +63,27 @@ return [
return Strings::replace($content, '#' . $prefix . '\\\\Composer\\\\InstalledVersions#', 'Composer\InstalledVersions');
},
// fixes https://github.com/rectorphp/rector/issues/6007
function (string $filePath, string $prefix, string $content): string {
if (! Strings::contains($filePath, 'vendor/')) {
return $content;
}
// @see https://regex101.com/r/lBV8IO/2
$fqcnReservedPattern = sprintf('#(\\\\)?%s\\\\(parent|self|static)#m', $prefix);
$matches = Strings::matchAll($content, $fqcnReservedPattern);
if (! $matches) {
return $content;
}
foreach ($matches as $match) {
$content = str_replace($match[0], $match[2], $content);
}
return $content;
},
// unprefix string classes, as they're string on purpose - they have to be checked in original form, not prefixed
function (string $filePath, string $prefix, string $content): string {
// skip vendor, expect rector packages