From 0eb6e291763372490ded707d4d4e4d75e09dfcc4 Mon Sep 17 00:00:00 2001 From: Tomas Votruba Date: Fri, 27 Nov 2020 17:32:38 +0100 Subject: [PATCH] drop MinimalVersionChecker, is checked by composer itself (#4715) Co-authored-by: rector-bot --- bin/rector.php | 10 +--- config/services.php | 5 -- .../src/PHPUnit/AbstractRectorTestCase.php | 9 ++-- phpstan.neon | 3 -- .../If_/ChangeAndIfToEarlyReturnRector.php | 25 +++++---- src/Configuration/MinimalVersionChecker.php | 54 ------------------- .../ComposerJsonParser.php | 30 ----------- .../ComposerJsonReader.php | 31 ----------- src/PhpParser/Node/BetterNodeFinder.php | 2 +- .../Configuration/ComposerJsonParserTest.php | 45 ---------------- .../MinimalVersionChecker/composer-7.2.0.json | 5 -- .../MinimalVersionCheckerTest.php | 43 --------------- .../Source/CustomLocalRector.php | 16 ------ .../Source/config/some_config.php | 14 ----- 14 files changed, 24 insertions(+), 268 deletions(-) delete mode 100644 src/Configuration/MinimalVersionChecker.php delete mode 100644 src/Configuration/MinimalVersionChecker/ComposerJsonParser.php delete mode 100644 src/Configuration/MinimalVersionChecker/ComposerJsonReader.php delete mode 100644 tests/Configuration/ComposerJsonParserTest.php delete mode 100644 tests/Configuration/MinimalVersionChecker/composer-7.2.0.json delete mode 100644 tests/Configuration/MinimalVersionCheckerTest.php delete mode 100644 tests/Configuration/Source/CustomLocalRector.php delete mode 100644 tests/Configuration/Source/config/some_config.php diff --git a/bin/rector.php b/bin/rector.php index 7ecc1a80674..e096e41a49f 100755 --- a/bin/rector.php +++ b/bin/rector.php @@ -6,12 +6,10 @@ use Rector\Caching\Detector\ChangedFilesDetector; use Rector\Core\Bootstrap\ConfigShifter; use Rector\Core\Bootstrap\RectorConfigsResolver; use Rector\Core\Configuration\Configuration; -use Rector\Core\Configuration\MinimalVersionChecker; -use Rector\Core\Configuration\MinimalVersionChecker\ComposerJsonParser; -use Rector\Core\Configuration\MinimalVersionChecker\ComposerJsonReader; use Rector\Core\Console\ConsoleApplication; use Rector\Core\Console\Style\SymfonyStyleFactory; use Rector\Core\DependencyInjection\RectorContainerFactory; +use Rector\Core\HttpKernel\RectorKernel; use Symplify\PackageBuilder\Console\ShellCode; use Symplify\PackageBuilder\Reflection\PrivatesCaller; use Symplify\SetConfigResolver\Bootstrap\InvalidSetReporter; @@ -38,10 +36,6 @@ $symfonyStyleFactory = new SymfonyStyleFactory(new PrivatesCaller()); $symfonyStyle = $symfonyStyleFactory->create(); try { - $composerJsonReader = new ComposerJsonReader(__DIR__ . '/../composer.json'); - $versionChecker = new MinimalVersionChecker(PHP_VERSION, new ComposerJsonParser($composerJsonReader->read())); - $versionChecker->check(); - $rectorConfigsResolver = new RectorConfigsResolver(); $configFileInfos = $rectorConfigsResolver->provide(); @@ -100,7 +94,7 @@ final class AutoloadIncluder public function includeDependencyOrRepositoryVendorAutoloadIfExists(): void { // Rector's vendor is already loaded - if (class_exists('Rector\HttpKernel\RectorKernel')) { + if (class_exists(RectorKernel::class)) { return; } diff --git a/config/services.php b/config/services.php index 630e5cc6ac4..1a62faa213f 100644 --- a/config/services.php +++ b/config/services.php @@ -12,7 +12,6 @@ use PhpParser\NodeVisitor\CloningVisitor; use PhpParser\Parser; use PhpParser\ParserFactory; use Rector\Core\Bootstrap\NoRectorsLoadedReporter; -use Rector\Core\Configuration\MinimalVersionChecker; use Rector\Core\Configuration\RectorClassesProvider; use Rector\Core\Console\ConsoleApplication; use Rector\Core\EventDispatcher\AutowiredEventDispatcher; @@ -52,16 +51,12 @@ return static function (ContainerConfigurator $containerConfigurator): void { __DIR__ . '/../src/PhpParser/Builder', __DIR__ . '/../src/HttpKernel', __DIR__ . '/../src/ValueObject', - __DIR__ . '/../src/Configuration/MinimalVersionChecker', __DIR__ . '/../src/Bootstrap', __DIR__ . '/../src/PhpParser/Node/CustomNode', // loaded for PHPStan factory __DIR__ . '/../src/PHPStan/Type', ]); - $services->set(MinimalVersionChecker::class) - ->autowire(false); - $services->alias(SymfonyApplication::class, ConsoleApplication::class); $services->set(NoRectorsLoadedReporter::class); diff --git a/packages/testing/src/PHPUnit/AbstractRectorTestCase.php b/packages/testing/src/PHPUnit/AbstractRectorTestCase.php index d488e845a54..9a415d3ebb5 100644 --- a/packages/testing/src/PHPUnit/AbstractRectorTestCase.php +++ b/packages/testing/src/PHPUnit/AbstractRectorTestCase.php @@ -487,10 +487,6 @@ abstract class AbstractRectorTestCase extends AbstractKernelTestCase } } - private function normalizeNewlines(string $string):string { - return preg_replace('/\r\n|\r|\n/', "\n", $string); - } - private function createContainerWithAllRectors(): void { $rectorsFinder = new RectorsFinder(); @@ -522,4 +518,9 @@ abstract class AbstractRectorTestCase extends AbstractKernelTestCase $fileContent = $smartPhpConfigPrinter->printConfiguredServices($rectorClassesWithConfiguration); $this->smartFileSystem->dumpFile($filePath, $fileContent); } + + private function normalizeNewlines(string $string): string + { + return Strings::replace($string, '#\r\n|\r|\n#', "\n"); + } } diff --git a/phpstan.neon b/phpstan.neon index 9eab37ec48e..e82ceedbb57 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -658,9 +658,6 @@ parameters: - rules/phpstan/src/Type/ShortenedObjectType.php # 20 - rules/restoration/src/Rector/Class_/RemoveUselessJustForSakeInterfaceRector.php # 42 - rules/type-declaration/src/Rector/FunctionLike/ReturnTypeDeclarationRector.php # 82 - - src/Configuration/MinimalVersionChecker.php # 27 - - src/Configuration/MinimalVersionChecker/ComposerJsonParser.php # 22 - - src/Configuration/MinimalVersionChecker/ComposerJsonReader.php # 23 - src/PhpParser/Builder/UseBuilder.php # 17 - src/RectorDefinition/CodeSample.php # 28 - src/RectorDefinition/CodeSample.php # 29 diff --git a/rules/solid/src/Rector/If_/ChangeAndIfToEarlyReturnRector.php b/rules/solid/src/Rector/If_/ChangeAndIfToEarlyReturnRector.php index 84cc8a89e71..761014ef623 100644 --- a/rules/solid/src/Rector/If_/ChangeAndIfToEarlyReturnRector.php +++ b/rules/solid/src/Rector/If_/ChangeAndIfToEarlyReturnRector.php @@ -9,8 +9,12 @@ use PhpParser\Node\Expr; use PhpParser\Node\Expr\BinaryOp\BooleanAnd; use PhpParser\Node\FunctionLike; use PhpParser\Node\Stmt; +use PhpParser\Node\Stmt\Continue_; +use PhpParser\Node\Stmt\For_; +use PhpParser\Node\Stmt\Foreach_; use PhpParser\Node\Stmt\If_; use PhpParser\Node\Stmt\Return_; +use PhpParser\Node\Stmt\While_; use Rector\Core\PhpParser\Node\Manipulator\IfManipulator; use Rector\Core\PhpParser\Node\Manipulator\StmtsManipulator; use Rector\Core\Rector\AbstractRector; @@ -123,7 +127,7 @@ CODE_SAMPLE $this->addNodeAfterNode($ifReturn, $node); $ifNextReturn = $this->getIfNextReturn($node); - if ($ifNextReturn !== null && !$this->isIfInLoop($node)) { + if ($ifNextReturn !== null && ! $this->isIfInLoop($node)) { $this->removeNode($ifNextReturn); } @@ -207,7 +211,7 @@ CODE_SAMPLE $invertedCondition = $this->conditionInverter->createInvertedCondition($condition); $if = new If_($invertedCondition); if ($isIfInLoop && $this->getIfNextReturn($node) === null) { - $if->stmts = [new Stmt\Continue_()]; + $if->stmts = [new Continue_()]; } else { $if->stmts = [new Return_()]; } @@ -237,6 +241,16 @@ CODE_SAMPLE return $nextNode; } + private function isIfInLoop(If_ $if): bool + { + $parentLoop = $this->betterNodeFinder->findFirstParentInstanceOf( + $if, + [Foreach_::class, For_::class, While_::class] + ); + + return $parentLoop !== null; + } + private function isIfReturnsVoid(If_ $if): bool { $lastStmt = $this->stmtsManipulator->getUnwrappedLastStmt($if->stmts); @@ -285,11 +299,4 @@ CODE_SAMPLE } return $nextNode instanceof Return_; } - - private function isIfInLoop(If_ $if): bool - { - $parentLoop = $this->betterNodeFinder->findFirstParentInstanceOf($if, [Stmt\Foreach_::class, Stmt\For_::class, Stmt\While_::class]); - - return $parentLoop !== null; - } } diff --git a/src/Configuration/MinimalVersionChecker.php b/src/Configuration/MinimalVersionChecker.php deleted file mode 100644 index 733f7fcbe17..00000000000 --- a/src/Configuration/MinimalVersionChecker.php +++ /dev/null @@ -1,54 +0,0 @@ -installedPhpVersion = $installedPhpVersion; - $this->composerJsonParser = $composerJsonParser; - $this->phpVersionFactory = new PhpVersionFactory(); - } - - public function check(): void - { - $minimumPhpVersion = $this->composerJsonParser->getPhpVersion(); - - $intInstalledPhpVersion = $this->phpVersionFactory->createIntVersion($this->installedPhpVersion); - $intMinimumPhpVersion = $this->phpVersionFactory->createIntVersion($minimumPhpVersion); - - // Check minimum required PHP version - if ($intInstalledPhpVersion < $intMinimumPhpVersion) { - throw new PhpVersionException(sprintf( - 'PHP version %s or higher is required, but you currently have %s installed.', - $minimumPhpVersion, - $this->installedPhpVersion - )); - } - } -} diff --git a/src/Configuration/MinimalVersionChecker/ComposerJsonParser.php b/src/Configuration/MinimalVersionChecker/ComposerJsonParser.php deleted file mode 100644 index b0267a6d4c3..00000000000 --- a/src/Configuration/MinimalVersionChecker/ComposerJsonParser.php +++ /dev/null @@ -1,30 +0,0 @@ -composerJson = $composerJson; - } - - public function getPhpVersion(): string - { - $composerArray = Json::decode($this->composerJson, Json::FORCE_ARRAY); - return Strings::trim($composerArray['require']['php'], '~^>=*.'); - } -} diff --git a/src/Configuration/MinimalVersionChecker/ComposerJsonReader.php b/src/Configuration/MinimalVersionChecker/ComposerJsonReader.php deleted file mode 100644 index 8d600d41896..00000000000 --- a/src/Configuration/MinimalVersionChecker/ComposerJsonReader.php +++ /dev/null @@ -1,31 +0,0 @@ -filename = $composerJsonFilename; - $this->smartFileSystem = new SmartFileSystem(); - } - - public function read(): string - { - return $this->smartFileSystem->readFile($this->filename); - } -} diff --git a/src/PhpParser/Node/BetterNodeFinder.php b/src/PhpParser/Node/BetterNodeFinder.php index 9cb7efbb864..ce35118fdb9 100644 --- a/src/PhpParser/Node/BetterNodeFinder.php +++ b/src/PhpParser/Node/BetterNodeFinder.php @@ -49,7 +49,7 @@ final class BetterNodeFinder } /** - * @param class-string|class-string[] $type + * @param string|string[] $type */ public function findFirstParentInstanceOf(Node $node, $type): ?Node { diff --git a/tests/Configuration/ComposerJsonParserTest.php b/tests/Configuration/ComposerJsonParserTest.php deleted file mode 100644 index 5df5927db78..00000000000 --- a/tests/Configuration/ComposerJsonParserTest.php +++ /dev/null @@ -1,45 +0,0 @@ -getComposerJsonPhpVersion($version); - - $this->assertSame($expectedVersion, $actualPhpVersion); - } - - public function dataProvider(): Iterator - { - yield ['7.2.0', '7.2.0']; - yield ['7.2.0', '~7.2.0']; - yield ['7.2', '7.2.*']; - yield ['7', '7.*.*']; - yield ['7.2.0', '~7.2.0']; - yield ['7.2.0', '^7.2.0']; - yield ['7.2.0', '>=7.2.0']; - } - - private function getComposerJsonPhpVersion(string $version): string - { - $composerJsonParser = new ComposerJsonParser(Json::encode([ - 'require' => - [ - 'php' => $version, - ], - ])); - return $composerJsonParser->getPhpVersion(); - } -} diff --git a/tests/Configuration/MinimalVersionChecker/composer-7.2.0.json b/tests/Configuration/MinimalVersionChecker/composer-7.2.0.json deleted file mode 100644 index 3016091a59b..00000000000 --- a/tests/Configuration/MinimalVersionChecker/composer-7.2.0.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "require": { - "php": "7.2.0" - } -} diff --git a/tests/Configuration/MinimalVersionCheckerTest.php b/tests/Configuration/MinimalVersionCheckerTest.php deleted file mode 100644 index 989119010da..00000000000 --- a/tests/Configuration/MinimalVersionCheckerTest.php +++ /dev/null @@ -1,43 +0,0 @@ -expectException(PhpVersionException::class); - } else { - $this->expectNotToPerformAssertions(); - } - $minimalVersionChecker = new MinimalVersionChecker( - $version, - new ComposerJsonParser($composerJsonReader->read()) - ); - - $minimalVersionChecker->check(); - } - - public function dataProvider(): Iterator - { - yield ['7.5.0', false]; - yield ['7.5.0-13ubuntu3.2', false]; - yield ['7.1.0', true]; - yield ['7.1.0-13ubuntu3.2', true]; - } -} diff --git a/tests/Configuration/Source/CustomLocalRector.php b/tests/Configuration/Source/CustomLocalRector.php deleted file mode 100644 index 52792b599f5..00000000000 --- a/tests/Configuration/Source/CustomLocalRector.php +++ /dev/null @@ -1,16 +0,0 @@ -services(); - - $services->set(ListEachRector::class); - $services->set(CustomLocalRector::class); -};