From d5952978c901c390438a052dd56370ebb1dc52a2 Mon Sep 17 00:00:00 2001 From: TomasVotruba Date: Sun, 24 Nov 2019 16:05:51 +0100 Subject: [PATCH] update backers --- .dockerignore | 1 - .gitattributes | 1 - BACKERS.md | 20 ++--- composer.json | 4 +- .../src/Command/CreateRectorCommand.php | 79 ++++++++++++++----- .../ComposerPackageAutoloadUpdater.php | 2 +- .../src/ValueObject/Configuration.php | 10 +-- 7 files changed, 77 insertions(+), 40 deletions(-) diff --git a/.dockerignore b/.dockerignore index 1a834540de6..ea060b1681b 100644 --- a/.dockerignore +++ b/.dockerignore @@ -11,7 +11,6 @@ phpstan.neon rector.yaml -create-rector.yaml.dist phpunit.xml ecs.yaml ecs-after-rector.yaml diff --git a/.gitattributes b/.gitattributes index 1771a755cde..3b537a3bf05 100644 --- a/.gitattributes +++ b/.gitattributes @@ -27,6 +27,5 @@ LICENSE export-ignore ecs.yml export-ignore phpstan.neon export-ignore phpunit.xml export-ignore -create-rector.yaml.dist export-ignore rector.yaml export-ignore utils export-ignore diff --git a/BACKERS.md b/BACKERS.md index b0e3b63353c..25f14d9f45c 100644 --- a/BACKERS.md +++ b/BACKERS.md @@ -9,25 +9,21 @@ Development of Rector is made possible thanks to these awesome backers! Would yo Check out all the tiers - higher ones include additional goodies like placing the logo of your company in Rector's README or creating custom set for your needs. -## $20+ - - Jan Votruba - -## $5+ - -- Kerrial Newham -- Jan Mikeš -- Jan Kuchař -- Jakob Oberhummer +- Sebastian Schreiber - Attila Fulop - -## $1+ - +- Jakob Oberhummer +- Jan Kuchař +- Jan Mikes - Arnaud TIERANT +**Thank you for making this happen.** +
## PayPal Backers - Musement S.p.a. - Sebastian Schreiber + + \ No newline at end of file diff --git a/composer.json b/composer.json index d914a8b0f6c..611333d956f 100644 --- a/composer.json +++ b/composer.json @@ -82,7 +82,7 @@ "Rector\\Php72\\": "packages/Php72/src", "Rector\\Php73\\": "packages/Php73/src", "Rector\\Php74\\": "packages/Php74/src", - "Rector\\Php80\\": "packages/Php80/src" + "Rector\\Php80\\": "packages/Php80/src", "Rector\\RemovingStatic\\": "packages/RemovingStatic/src", "Rector\\Renaming\\": "packages/Renaming/src", "Rector\\Restoration\\": "packages/Restoration/src", @@ -143,7 +143,7 @@ "Rector\\Php72\\Tests\\": "packages/Php72/tests", "Rector\\Php73\\Tests\\": "packages/Php73/tests", "Rector\\Php74\\Tests\\": "packages/Php74/tests", - "Rector\\Php80\\Tests\\": "packages/Php80/tests" + "Rector\\Php80\\Tests\\": "packages/Php80/tests", "Rector\\RemovingStatic\\Tests\\": "packages/RemovingStatic/tests", "Rector\\Renaming\\Tests\\": "packages/Renaming/tests", "Rector\\Restoration\\Tests\\": "packages/Restoration/tests", diff --git a/utils/RectorGenerator/src/Command/CreateRectorCommand.php b/utils/RectorGenerator/src/Command/CreateRectorCommand.php index a584180f55d..ae147df7cd1 100644 --- a/utils/RectorGenerator/src/Command/CreateRectorCommand.php +++ b/utils/RectorGenerator/src/Command/CreateRectorCommand.php @@ -97,7 +97,7 @@ final class CreateRectorCommand extends Command implements ContributorCommandInt protected function configure(): void { $this->setName(CommandNaming::classToName(self::class)); - $this->setDescription('Create a new Rector, in proper location, with new tests'); + $this->setDescription('Create a new Rector, in a proper location, with new tests'); } protected function execute(InputInterface $input, OutputInterface $output): int @@ -108,37 +108,64 @@ final class CreateRectorCommand extends Command implements ContributorCommandInt // setup psr-4 autoload, if not already in $this->composerPackageAutoloadUpdater->processComposerAutoload($configuration); + $templateFileInfos = $this->findTemplateFileInfos(); + $isUnwantedOverride = $this->isUnwantedOverride($templateFileInfos, $templateVariables, $configuration); + + if ($isUnwantedOverride) { + $this->symfonyStyle->warning( + 'The rule already exists and you decided to keep the original. No files were changed' + ); + return ShellCode::SUCCESS; + } + foreach ($this->findTemplateFileInfos() as $smartFileInfo) { $destination = $this->resolveDestination($smartFileInfo, $templateVariables, $configuration); $content = $this->resolveContent($smartFileInfo, $templateVariables); if ($configuration->getPackage() === 'Rector') { - $content = Strings::replace($content, '#Rector\\\\Rector\\\\#ms', 'Rector\\'); - $content = Strings::replace( - $content, - '#use Rector\\\\AbstractRector;#', - 'use Rector\\Rector\\AbstractRector;' - ); + $content = $this->addOneMoreRectorNesting($content); } FileSystem::write($destination, $content); $this->generatedFiles[] = $destination; - // is test case? + // is a test case? if (Strings::endsWith($destination, 'Test.php')) { $this->testCasePath = dirname($destination); } } - $this->appendToLevelConfig($configuration, $templateVariables); + $this->appendRectorServiceToSetConfig($configuration, $templateVariables); $this->printSuccess($configuration->getName()); return ShellCode::SUCCESS; } + /** + * @param SmartFileInfo[] $templateFileInfos + * @param mixed[] $templateVariables + */ + private function isUnwantedOverride( + array $templateFileInfos, + array $templateVariables, + Configuration $configuration + ) { + foreach ($templateFileInfos as $templateFileInfo) { + $destination = $this->resolveDestination($templateFileInfo, $templateVariables, $configuration); + + if (! file_exists($destination)) { + continue; + } + + return ! $this->symfonyStyle->confirm('Files for this rules already exist. Should we override them?'); + } + + return false; + } + /** * @return SmartFileInfo[] */ @@ -189,33 +216,33 @@ final class CreateRectorCommand extends Command implements ContributorCommandInt /** * @param string[] $templateVariables */ - private function appendToLevelConfig(Configuration $configuration, array $templateVariables): void + private function appendRectorServiceToSetConfig(Configuration $configuration, array $templateVariables): void { - if ($configuration->getLevelConfig() === null) { + if ($configuration->getSetConfig() === null) { return; } - if (! file_exists($configuration->getLevelConfig())) { + if (! file_exists($configuration->getSetConfig())) { return; } $rectorFqnName = $this->applyVariables(self::RECTOR_FQN_NAME_PATTERN, $templateVariables); - $levelConfigContent = FileSystem::read($configuration->getLevelConfig()); + $setConfigContent = FileSystem::read($configuration->getSetConfig()); // already added - if (Strings::contains($levelConfigContent, $rectorFqnName)) { + if (Strings::contains($setConfigContent, $rectorFqnName)) { return; } - $levelConfigContent = trim($levelConfigContent) . sprintf( + $setConfigContent = trim($setConfigContent) . sprintf( '%s%s: ~%s', PHP_EOL, - Strings::indent($rectorFqnName, 4, ' '), + $this->indentFourSpaces($rectorFqnName), PHP_EOL ); - FileSystem::write($configuration->getLevelConfig(), $levelConfigContent); + FileSystem::write($configuration->getSetConfig(), $setConfigContent); } private function printSuccess(string $name): void @@ -225,7 +252,7 @@ final class CreateRectorCommand extends Command implements ContributorCommandInt $this->symfonyStyle->listing($this->generatedFiles); $this->symfonyStyle->success(sprintf( - 'Now make these tests green again:%svendor/bin/phpunit %s', + 'Now make these tests green:%svendor/bin/phpunit %s', PHP_EOL . PHP_EOL, $this->testCasePath )); @@ -238,4 +265,20 @@ final class CreateRectorCommand extends Command implements ContributorCommandInt { return str_replace(array_keys($variables), array_values($variables), $content); } + + private function addOneMoreRectorNesting(string $content): string + { + $content = Strings::replace($content, '#Rector\\\\Rector\\\\#ms', 'Rector\\'); + + return Strings::replace( + $content, + '#use Rector\\\\AbstractRector;#', + 'use Rector\\Rector\\AbstractRector;' + ); + } + + private function indentFourSpaces(string $string): string + { + return Strings::indent($string, 4, ' '); + } } diff --git a/utils/RectorGenerator/src/Composer/ComposerPackageAutoloadUpdater.php b/utils/RectorGenerator/src/Composer/ComposerPackageAutoloadUpdater.php index 612ac32f344..def0e709c84 100644 --- a/utils/RectorGenerator/src/Composer/ComposerPackageAutoloadUpdater.php +++ b/utils/RectorGenerator/src/Composer/ComposerPackageAutoloadUpdater.php @@ -46,7 +46,7 @@ final class ComposerPackageAutoloadUpdater // ask user $isConfirmed = $this->symfonyStyle->confirm(sprintf( - 'Can we update composer.json autoload section with "%s" namespace?%s Or you have to handle it manually', + 'Can we update "composer.json" autoload with "%s" namespace?%s Handle it manually otherwise', $package->getSrcNamespace(), PHP_EOL )); diff --git a/utils/RectorGenerator/src/ValueObject/Configuration.php b/utils/RectorGenerator/src/ValueObject/Configuration.php index 59d41977fbb..5677fc3fd38 100644 --- a/utils/RectorGenerator/src/ValueObject/Configuration.php +++ b/utils/RectorGenerator/src/ValueObject/Configuration.php @@ -41,7 +41,7 @@ final class Configuration /** * @var string|null */ - private $levelConfig; + private $setConfig; /** * @var string[] @@ -66,7 +66,7 @@ final class Configuration string $codeBefore, string $codeAfter, array $source, - ?string $levelConfig + ?string $setConfig ) { $this->package = $package; $this->setName($name); @@ -76,7 +76,7 @@ final class Configuration $this->codeAfter = $codeAfter; $this->description = $description; $this->source = $source; - $this->levelConfig = $levelConfig; + $this->setConfig = $setConfig; } public function getDescription(): string @@ -125,9 +125,9 @@ final class Configuration return $this->source; } - public function getLevelConfig(): ?string + public function getSetConfig(): ?string { - return $this->levelConfig; + return $this->setConfig; } private function setName(string $name): void