drop MinimalVersionChecker, is checked by composer itself (#4715)

Co-authored-by: rector-bot <tomas@getrector.org>
This commit is contained in:
Tomas Votruba 2020-11-27 17:32:38 +01:00 committed by GitHub
parent 947b9383ee
commit 0eb6e29176
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 24 additions and 268 deletions

View File

@ -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;
}

View File

@ -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);

View File

@ -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");
}
}

View File

@ -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

View File

@ -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;
}
}

View File

@ -1,54 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Configuration;
use Rector\Core\Configuration\MinimalVersionChecker\ComposerJsonParser;
use Rector\Core\Exception\Application\PhpVersionException;
use Rector\Core\Util\PhpVersionFactory;
/**
* @see \Rector\Core\Tests\Configuration\MinimalVersionCheckerTest
*/
final class MinimalVersionChecker
{
/**
* @var string
*/
private $installedPhpVersion;
/**
* @var ComposerJsonParser
*/
private $composerJsonParser;
/**
* @var PhpVersionFactory
*/
private $phpVersionFactory;
public function __construct(string $installedPhpVersion, ComposerJsonParser $composerJsonParser)
{
$this->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
));
}
}
}

View File

@ -1,30 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Configuration\MinimalVersionChecker;
use Nette\Utils\Json;
use Nette\Utils\Strings;
/**
* @see \Rector\Core\Tests\Configuration\ComposerJsonParserTest
*/
final class ComposerJsonParser
{
/**
* @var string
*/
private $composerJson;
public function __construct(string $composerJson)
{
$this->composerJson = $composerJson;
}
public function getPhpVersion(): string
{
$composerArray = Json::decode($this->composerJson, Json::FORCE_ARRAY);
return Strings::trim($composerArray['require']['php'], '~^>=*.');
}
}

View File

@ -1,31 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Configuration\MinimalVersionChecker;
use Symplify\SmartFileSystem\SmartFileSystem;
final class ComposerJsonReader
{
/**
* @var string
*/
private $filename;
/**
* @var SmartFileSystem
*/
private $smartFileSystem;
public function __construct(string $composerJsonFilename)
{
$this->filename = $composerJsonFilename;
$this->smartFileSystem = new SmartFileSystem();
}
public function read(): string
{
return $this->smartFileSystem->readFile($this->filename);
}
}

View File

@ -49,7 +49,7 @@ final class BetterNodeFinder
}
/**
* @param class-string|class-string[] $type
* @param string|string[] $type
*/
public function findFirstParentInstanceOf(Node $node, $type): ?Node
{

View File

@ -1,45 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Configuration;
use Iterator;
use Nette\Utils\Json;
use Rector\Core\Configuration\MinimalVersionChecker\ComposerJsonParser;
use Symplify\PackageBuilder\Testing\AbstractKernelTestCase;
final class ComposerJsonParserTest extends AbstractKernelTestCase
{
/**
* @dataProvider dataProvider
*/
public function test(string $expectedVersion, string $version): void
{
$actualPhpVersion = $this->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();
}
}

View File

@ -1,5 +0,0 @@
{
"require": {
"php": "7.2.0"
}
}

View File

@ -1,43 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Configuration;
use Iterator;
use Rector\Core\Configuration\MinimalVersionChecker;
use Rector\Core\Configuration\MinimalVersionChecker\ComposerJsonParser;
use Rector\Core\Configuration\MinimalVersionChecker\ComposerJsonReader;
use Rector\Core\Exception\Application\PhpVersionException;
use Symplify\PackageBuilder\Testing\AbstractKernelTestCase;
final class MinimalVersionCheckerTest extends AbstractKernelTestCase
{
/**
* @dataProvider dataProvider
*/
public function test(string $version, bool $shouldThrowException): void
{
$composerJsonReader = new ComposerJsonReader(__DIR__ . '/MinimalVersionChecker/composer-7.2.0.json');
if ($shouldThrowException) {
$this->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];
}
}

View File

@ -1,16 +0,0 @@
<?php
declare(strict_types=1);
namespace Rector\Core\Tests\Configuration\Source;
use Rector\Core\Contract\Rector\RectorInterface;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
final class CustomLocalRector implements RectorInterface
{
public function getRuleDefinition(): RuleDefinition
{
// TODO: Implement getDefinition() method.
}
}

View File

@ -1,14 +0,0 @@
<?php
declare(strict_types=1);
use Rector\Core\Tests\Configuration\Source\CustomLocalRector;
use Rector\Php72\Rector\Assign\ListEachRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(ListEachRector::class);
$services->set(CustomLocalRector::class);
};