fix scoper

This commit is contained in:
TomasVotruba 2020-07-20 10:20:23 +02:00
parent 46b3090e37
commit 0a29fe0c0a
7 changed files with 93 additions and 44 deletions

View File

@ -20,7 +20,7 @@ return [
'files-whitelist' => $whitelistedStubsProvider->provide(),
'patchers' => [
function (string $filePath, string $prefix, string $content): string {
if ($filePath !== 'bin/rector') {
if ($filePath !== 'bin/rector' /* && ! Strings::contains($filePath, 'config/')*/) {
return $content;
}
@ -61,7 +61,7 @@ return [
);
},
function (string $filePath, string $prefix, string $content): string {
if ($filePath !== 'src/Testing/TestCase.php') {
if ($filePath !== 'vendor/phpstan/phpstan-src/src/Testing/TestCase.php') {
return $content;
}
@ -72,7 +72,7 @@ return [
);
},
function (string $filePath, string $prefix, string $content): string {
if ($filePath !== 'src/Testing/LevelsTestCase.php') {
if ($filePath !== 'vendor/phpstan/phpstan-src/src/Testing/LevelsTestCase.php') {
return $content;
}
@ -86,29 +86,15 @@ return [
);
},
// unprefix configuraion in sets, @see https://github.com/rectorphp/rector/issues/3227
// unprefix excluded classes
// fixes https://github.com/humbug/box/issues/470
function (string $filePath, string $prefix, string $content): string {
// only sets
if (! Strings::startsWith($filePath, 'config/set/')) {
return $content;
foreach (StaticEasyPrefixer::EXCLUDED_CLASSES as $excludedClass) {
$prefixedClassPattern = '#' . $prefix . '\\\\' . preg_quote($excludedClass, '#') . '#';
$content = Strings::replace($content, $prefixedClassPattern, $excludedClass);
}
return StaticEasyPrefixer::unPrefixQuotedValues($prefix, $content);
},
// unprefix all excluded classes
function (string $filePath, string $prefix, string $content): string {
// only sets
if (! Strings::endsWith($filePath, '*.php')) {
return $content;
}
foreach (StaticEasyPrefixer::EXCLUDED_NAMESPACES_AND_CLASSES as $excludedNamespaceAndClass) {
dump($excludedNamespaceAndClass);
die;
}
return StaticEasyPrefixer::unPrefixQuotedValues($prefix, $content);
return $content;
},
// mimics https://github.com/phpstan/phpstan-src/commit/5a6a22e5c4d38402c8cc888d8732360941c33d43#diff-463a36e4a5687fb2366b5ee56cdad92d
@ -161,6 +147,44 @@ return [
return StaticEasyPrefixer::unPreSlashQuotedValues($content);
},
// mimics
// https://github.com/phpstan/phpstan-src/commit/9c2eb91b630bdfee2c1bb642a4c81ebfa0f1ca9a#diff-87f75ce3f908a819a9a2c77ffeffcc38
// https://github.com/phpstan/phpstan-src/commit/7048109ab17aa16102dc0fd21190782e6d6d5e7e#diff-87f75ce3f908a819a9a2c77ffeffcc38
function (string $filePath, string $prefix, string $content): string {
if (! in_array($filePath, [
'vendor/phpstan/phpstan-src/src/Type/TypehintHelper.php',
'vendor/ondrejmirtes/better-reflection/src/Reflection/Adapter/ReflectionUnionType.php',
], true)) {
return $content;
}
return str_replace(sprintf('%s\\ReflectionUnionType', $prefix), 'ReflectionUnionType', $content);
},
// mimics: https://github.com/phpstan/phpstan-src/commit/6bb92ed7b92b186bb1eb5111bc49ec7679ed780f#diff-87f75ce3f908a819a9a2c77ffeffcc38
function (string $filePath, string $prefix, string $content): string {
return str_replace('private static final', 'private static', $content);
},
// mimics: https://github.com/phpstan/phpstan-src/commit/1c63a785e5fce8d031b04f52c61904bd57b51e27#diff-87f75ce3f908a819a9a2c77ffeffcc38
function (string $filePath, string $prefix, string $content): string {
if (! in_array($filePath, [
'vendor/phpstan/phpstan-src/src/Testing/TestCaseSourceLocatorFactory.php',
'vendor/phpstan/phpstan-src/src/Testing/TestCase.php',
], true)) {
return $content;
}
return str_replace(
sprintf('%s\\Composer\\Autoload\\ClassLoader', $prefix),
'Composer\\Autoload\\ClassLoader',
$content
);
},
],
'whitelist' => StaticEasyPrefixer::EXCLUDED_NAMESPACES_AND_CLASSES,
'whitelist' => StaticEasyPrefixer::getExcludedNamespacesAndClasses(),
];

View File

@ -3,7 +3,6 @@
declare(strict_types=1);
use OndraM\CiDetector\CiDetector;
use Rector\Core\Configuration\Option;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\Filesystem\Filesystem;

View File

@ -65,7 +65,9 @@ final class ComposerJsonManipulator
$encodedJson = Json::encode($json, Json::PRETTY);
// show diff
$this->consoleDiffer->diff($this->originalComposerJsonFileContent, $encodedJson);
if ($encodedJson !== $this->originalComposerJsonFileContent) {
$this->consoleDiffer->diff($this->originalComposerJsonFileContent, $encodedJson);
}
$this->filesystem->dumpFile($composerJsonFile, $encodedJson);
}

View File

@ -20,6 +20,11 @@ use Symplify\PackageBuilder\Console\ShellCode;
*/
final class CompileCommand extends Command
{
/**
* @var string
*/
private const ANSI = '--ansi';
/**
* @var string
*/
@ -80,6 +85,7 @@ final class CompileCommand extends Command
$composerJsonFile = $this->buildDir . '/composer.json';
$this->symfonyStyle->title('1. Adding "phpstan/phpstan-src" to ' . $composerJsonFile);
$this->composerJsonManipulator->fixComposerJson($composerJsonFile);
$this->symfonyStyle->newLine(2);
@ -93,39 +99,43 @@ final class CompileCommand extends Command
'--prefer-dist',
'--no-interaction',
'--classmap-authoritative',
'--ansi',
self::ANSI,
], $this->buildDir, null, null, null);
$process->mustRun(static function (string $type, string $buffer) use ($output): void {
$output->write($buffer);
});
$this->symfonyStyle->newLine(2);
$this->symfonyStyle->title('3. Downgrading PHPStan code to PHP 7.1');
$this->downgradePHPStanCodeToPHP71($output);
$this->symfonyStyle->title('3. Renaming PHPStorm stubs from "*.php" to ".stub"');
$this->symfonyStyle->title('4. Renaming PHPStorm stubs from "*.php" to ".stub"');
$this->jetbrainsStubsRenamer->renamePhpStormStubs($this->buildDir);
$this->symfonyStyle->newLine(2);
// the '--no-parallel' is needed, so "scoper.php.inc" can "require __DIR__ ./vendor/autoload.php"
// and "Nette\Neon\Neon" class can be used there
$this->symfonyStyle->title('4. Packing and prefixing rector.phar with Box and PHP Scoper');
$process = new Process(['php', 'box.phar', 'compile', '--no-parallel'], $this->dataDir, null, null, null);
$this->symfonyStyle->title('5. Packing and prefixing rector.phar with Box and PHP Scoper');
$process = new Process([
'php',
'box.phar',
'compile',
'--no-parallel',
self::ANSI,
], $this->dataDir, null, null, null);
$process->mustRun(static function (string $type, string $buffer) use ($output): void {
$output->write($buffer);
});
$this->symfonyStyle->newLine(2);
$this->symfonyStyle->title('5. Restoring root composer.json with "require-dev"');
$this->symfonyStyle->title('6. Restoring root composer.json with "require-dev"');
$this->composerJsonManipulator->restoreComposerJson($composerJsonFile);
$this->restoreDependenciesLocallyIfNotCi($output);
return ShellCode::SUCCESS;
@ -137,7 +147,7 @@ final class CompileCommand extends Command
return;
}
$process = new Process(['composer', 'install', '--ansi'], $this->buildDir, null, null, null);
$process = new Process(['composer', 'install', self::ANSI], $this->buildDir, null, null, null);
$process->mustRun(static function (string $type, string $buffer) use ($output): void {
$output->write($buffer);
});

View File

@ -11,22 +11,30 @@ final class StaticEasyPrefixer
/**
* @var string[]
*/
public const EXCLUDED_NAMESPACES_AND_CLASSES = [
public const EXCLUDED_CLASSES = [
'Symfony\Component\EventDispatcher\EventSubscriberInterface',
'Symfony\Component\Console\Style\SymfonyStyle',
// doctrine annotations to autocomplete
'JMS\DiExtraBundle\Annotation\Inject',
];
/**
* @var string[]
*/
private const EXCLUDED_NAMESPACES = [
'Hoa\*',
'PhpParser\*',
'PHPStan\*',
'Rector\*',
'Symplify\SmartFileSystem\*',
'Symfony\Component\EventDispatcher\EventSubscriberInterface',
'Symfony\Component\Console\Style\SymfonyStyle',
'Symplify\ConsoleColorDiff\*',
// doctrine annotations to autocomplete
'Doctrine\ORM\Mapping\*',
'JMS\DiExtraBundle\Annotation\Inject',
];
public static function prefixClass(string $class, string $prefix): string
{
foreach (self::EXCLUDED_NAMESPACES_AND_CLASSES as $excludedNamespace) {
foreach (self::EXCLUDED_NAMESPACES as $excludedNamespace) {
$excludedNamespace = Strings::substring($excludedNamespace, 0, -2) . '\\';
if (Strings::startsWith($class, $excludedNamespace)) {
return $class;
@ -54,4 +62,12 @@ final class StaticEasyPrefixer
{
return Strings::replace($content, '#\'\\\\(\w|@)#', "'$1");
}
/**
* @return string[]
*/
public static function getExcludedNamespacesAndClasses(): array
{
return array_merge(self::EXCLUDED_NAMESPACES, self::EXCLUDED_CLASSES);
}
}

View File

@ -18,10 +18,10 @@ use Symfony\Component\Console\Application as SymfonyApplication;
use Symfony\Component\Console\Descriptor\TextDescriptor;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\PackageBuilder\Console\Style\SymfonyStyleFactory;
use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symplify\PackageBuilder\Console\Style\SymfonyStyleFactory;
use Symplify\PackageBuilder\Parameter\ParameterProvider;
use Symplify\PackageBuilder\Reflection\PrivatesAccessor;
use Symplify\PackageBuilder\Reflection\PrivatesCaller;

View File

@ -8,12 +8,10 @@ use Rector\ConsoleDiffer\MarkdownDifferAndFormatter;
use SebastianBergmann\Diff\Differ;
use SebastianBergmann\Diff\Output\StrictUnifiedDiffOutputBuilder;
use SebastianBergmann\Diff\Output\UnifiedDiffOutputBuilder;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use function Symfony\Component\DependencyInjection\Loader\Configurator\ref;
use Symplify\ConsoleColorDiff\Console\Formatter\ColorConsoleDiffFormatter;
use Symplify\ConsoleColorDiff\Console\Output\ConsoleDiffer;
use Symplify\PackageBuilder\Console\Style\SymfonyStyleFactory;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();