Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing Compiler #3736

Merged
merged 4 commits into from
Jul 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 48 additions & 13 deletions compiler/build/scoper.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
'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;
}

Expand Down Expand Up @@ -61,7 +61,7 @@ function (string $filePath, string $prefix, string $content): string {
);
},
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;
}

Expand All @@ -72,7 +72,7 @@ function (string $filePath, string $prefix, string $content): string {
);
},
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;
}

Expand All @@ -86,18 +86,15 @@ function (string $filePath, string $prefix, string $content): string {
);
},

// 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 *.yaml files
if (! Strings::endsWith($filePath, '.yaml')) {
return $content;
}

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);
return $content;
},

// mimics https://github.com/phpstan/phpstan-src/commit/5a6a22e5c4d38402c8cc888d8732360941c33d43#diff-463a36e4a5687fb2366b5ee56cdad92d
Expand Down Expand Up @@ -150,6 +147,44 @@ function (string $filePath, string $prefix, string $content): string {

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(),
];
33 changes: 33 additions & 0 deletions compiler/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

use OndraM\CiDetector\CiDetector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\Filesystem\Filesystem;

return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();

$parameters->set('dataDir', __DIR__ . '/../build');

$parameters->set('buildDir', __DIR__ . '/../..');

$services = $containerConfigurator->services();

$services->defaults()
->public()
->autowire();

$services->load('Rector\Compiler\\', __DIR__ . '/../src')
->exclude([
__DIR__ . '/../src/Exception/*',
__DIR__ . '/../src/DependencyInjection/*',
__DIR__ . '/../src/HttpKernel/*',
__DIR__ . '/../src/PhpScoper/*',
]);

$services->set(Filesystem::class);

$services->set(CiDetector::class);
};
20 changes: 0 additions & 20 deletions compiler/config/config.yaml

This file was deleted.

4 changes: 3 additions & 1 deletion compiler/src/Composer/ComposerJsonManipulator.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ public function fixComposerJson(string $composerJsonFile): void
$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);
}
Expand Down
30 changes: 20 additions & 10 deletions compiler/src/Console/Command/CompileCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
*/
final class CompileCommand extends Command
{
/**
* @var string
*/
private const ANSI = '--ansi';

/**
* @var string
*/
Expand Down Expand Up @@ -80,6 +85,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$composerJsonFile = $this->buildDir . '/composer.json';

$this->symfonyStyle->title('1. Adding "phpstan/phpstan-src" to ' . $composerJsonFile);

$this->composerJsonManipulator->fixComposerJson($composerJsonFile);

$this->symfonyStyle->newLine(2);
Expand All @@ -93,39 +99,43 @@ protected function execute(InputInterface $input, OutputInterface $output): int
'--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;
Expand All @@ -137,7 +147,7 @@ private function restoreDependenciesLocallyIfNotCi(OutputInterface $output): voi
return;
}

$process = new Process(['composer', 'install'], $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);
});
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/HttpKernel/RectorCompilerKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getLogDir(): string

public function registerContainerConfiguration(LoaderInterface $loader): void
{
$loader->load(__DIR__ . '/../../config/config.yaml');
$loader->load(__DIR__ . '/../../config/config.php');
}

/**
Expand Down
26 changes: 21 additions & 5 deletions compiler/src/PhpScoper/StaticEasyPrefixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,4 +62,12 @@ public static function unPreSlashQuotedValues(string $content): string
{
return Strings::replace($content, '#\'\\\\(\w|@)#', "'$1");
}

/**
* @return string[]
*/
public static function getExcludedNamespacesAndClasses(): array
{
return array_merge(self::EXCLUDED_NAMESPACES, self::EXCLUDED_CLASSES);
}
}
7 changes: 7 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
use Rector\Core\PhpParser\Parser\NikicPhpParserFactory;
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 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;
Expand Down Expand Up @@ -89,4 +91,9 @@
$services->alias(EventDispatcherInterface::class, AutowiredEventDispatcher::class);

$services->set(SmartFileSystem::class);

$services->set(SymfonyStyleFactory::class);

$services->set(SymfonyStyle::class)
->factory([ref(SymfonyStyleFactory::class), 'create']);
};
7 changes: 0 additions & 7 deletions packages/console-differ/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@
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();
Expand Down Expand Up @@ -50,9 +48,4 @@
$services->set(ColorConsoleDiffFormatter::class);

$services->set(ConsoleDiffer::class);

$services->set(SymfonyStyleFactory::class);

$services->set(SymfonyStyle::class)
->factory([ref(SymfonyStyleFactory::class), 'create']);
};