-
-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Downgrade] Add composer platform check (#4721)
- Loading branch information
1 parent
dade407
commit d962e3f
Showing
11 changed files
with
250 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
151 changes: 151 additions & 0 deletions
151
rules/downgrade/src/Rector/LNumber/ChangePhpVersionInPlatformCheckRector.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Downgrade\Rector\LNumber; | ||
|
||
use Nette\Utils\Strings; | ||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\BinaryOp\Greater; | ||
use PhpParser\Node\Expr\BinaryOp\GreaterOrEqual; | ||
use PhpParser\Node\Scalar\LNumber; | ||
use PhpParser\Node\Scalar\String_; | ||
use Rector\Core\Contract\Rector\ConfigurableRectorInterface; | ||
use Rector\Core\Rector\AbstractRector; | ||
use Rector\Core\Util\PhpVersionFactory; | ||
use Rector\NodeTypeResolver\Node\AttributeKey; | ||
use Symplify\RuleDocGenerator\ValueObject\CodeSample\ConfiguredCodeSample; | ||
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; | ||
use Webmozart\Assert\Assert; | ||
|
||
/** | ||
* @see https://php.watch/articles/composer-platform-check | ||
* @see https://getcomposer.org/doc/06-config.md#platform-check | ||
* | ||
* @see \Rector\Downgrade\Tests\Rector\LNumber\ChangePhpVersionInPlatformCheckRector\ChangePhpVersionInPlatformCheckRectorTest | ||
*/ | ||
final class ChangePhpVersionInPlatformCheckRector extends AbstractRector implements ConfigurableRectorInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
public const TARGET_PHP_VERSION = 'target_php_version'; | ||
|
||
/** | ||
* @see https://regex101.com/r/oVWPoe/1/ | ||
* @var string | ||
*/ | ||
private const PHP_VERSION_REGEX = '#(?<sign>>=|>) (?<version>\d\.\d\.\d)#'; | ||
|
||
/** | ||
* @var int | ||
*/ | ||
private $targetPhpVersion; | ||
|
||
/** | ||
* @var PhpVersionFactory | ||
*/ | ||
private $phpVersionFactory; | ||
|
||
public function __construct(PhpVersionFactory $phpVersionFactory) | ||
{ | ||
$this->phpVersionFactory = $phpVersionFactory; | ||
} | ||
|
||
public function getRuleDefinition(): RuleDefinition | ||
{ | ||
return new RuleDefinition('Change `vendor/composer/platform_check.php` to desired minimal PHP version', [ | ||
new ConfiguredCodeSample( | ||
<<<'CODE_SAMPLE' | ||
$issues = []; | ||
if (!(PHP_VERSION_ID >= 70300)) { | ||
$issues[] = 'Your Composer dependencies require a PHP version ">= 7.3.0". You are running ' . PHP_VERSION . '.'; | ||
} | ||
CODE_SAMPLE | ||
, | ||
<<<'CODE_SAMPLE' | ||
$issues = []; | ||
if (!(PHP_VERSION_ID >= 70100)) { | ||
$issues[] = 'Your Composer dependencies require a PHP version ">= 7.1.0". You are running ' . PHP_VERSION . '.'; | ||
} | ||
CODE_SAMPLE | ||
, | ||
[ | ||
self::TARGET_PHP_VERSION => 70100, | ||
] | ||
), | ||
]); | ||
} | ||
|
||
/** | ||
* @return string[] | ||
*/ | ||
public function getNodeTypes(): array | ||
{ | ||
return [LNumber::class, String_::class]; | ||
} | ||
|
||
/** | ||
* @param LNumber|String_ $node | ||
*/ | ||
public function refactor(Node $node): ?Node | ||
{ | ||
if ($node instanceof LNumber) { | ||
return $this->refactorLNumber($node); | ||
} | ||
|
||
if ($node instanceof String_) { | ||
return $this->refactorString($node); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param mixed[] $configuration | ||
*/ | ||
public function configure(array $configuration): void | ||
{ | ||
$targetPhpVersion = $configuration[self::TARGET_PHP_VERSION] ?? null; | ||
Assert::integer($targetPhpVersion); | ||
|
||
$this->targetPhpVersion = $targetPhpVersion; | ||
} | ||
|
||
private function refactorLNumber(LNumber $lNumber): ?LNumber | ||
{ | ||
if (Strings::length((string) $lNumber->value) !== 5) { | ||
return null; | ||
} | ||
|
||
$parent = $lNumber->getAttribute(AttributeKey::PARENT_NODE); | ||
if (! $parent instanceof Greater && ! $parent instanceof GreaterOrEqual) { | ||
return null; | ||
} | ||
|
||
return new LNumber($this->targetPhpVersion); | ||
} | ||
|
||
private function refactorString(String_ $string): ?String_ | ||
{ | ||
$match = Strings::match($string->value, self::PHP_VERSION_REGEX); | ||
if ($match === null) { | ||
return null; | ||
} | ||
|
||
$stringPhpVersion = $this->phpVersionFactory->createStringVersion($this->targetPhpVersion); | ||
$changedContent = Strings::replace($string->value, self::PHP_VERSION_REGEX, function (array $match) use ( | ||
$stringPhpVersion | ||
): string { | ||
return $match['sign'] . ' ' . $stringPhpVersion; | ||
}); | ||
|
||
if ($string->value === $changedContent) { | ||
return null; | ||
} | ||
|
||
return new String_($changedContent); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...umber/ChangePhpVersionInPlatformCheckRector/ChangePhpVersionInPlatformCheckRectorTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Rector\Downgrade\Tests\Rector\LNumber\ChangePhpVersionInPlatformCheckRector; | ||
|
||
use Iterator; | ||
use Rector\Downgrade\Rector\LNumber\ChangePhpVersionInPlatformCheckRector; | ||
use Rector\Testing\PHPUnit\AbstractRectorTestCase; | ||
use Symplify\SmartFileSystem\SmartFileInfo; | ||
|
||
final class ChangePhpVersionInPlatformCheckRectorTest extends AbstractRectorTestCase | ||
{ | ||
/** | ||
* @dataProvider provideData() | ||
*/ | ||
public function test(SmartFileInfo $fileInfo): void | ||
{ | ||
$this->doTestFileInfo($fileInfo); | ||
} | ||
|
||
public function provideData(): Iterator | ||
{ | ||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture'); | ||
} | ||
|
||
/** | ||
* @return mixed[] | ||
*/ | ||
protected function getRectorsWithConfiguration(): array | ||
{ | ||
return [ | ||
ChangePhpVersionInPlatformCheckRector::class => [ | ||
ChangePhpVersionInPlatformCheckRector::TARGET_PHP_VERSION => 70100, | ||
], | ||
]; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
...ngrade/tests/Rector/LNumber/ChangePhpVersionInPlatformCheckRector/Fixture/fixture.php.inc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace Rector\Downgrade\Tests\Rector\LNumber\ChangePhpVersionInPlatformCheckRector\Fixture; | ||
|
||
$issues = []; | ||
|
||
if (!(PHP_VERSION_ID >= 70300)) { | ||
$issues[] = 'Your Composer dependencies require a PHP version ">= 7.3.0". You are running ' . PHP_VERSION . '.'; | ||
} | ||
|
||
?> | ||
----- | ||
<?php | ||
|
||
namespace Rector\Downgrade\Tests\Rector\LNumber\ChangePhpVersionInPlatformCheckRector\Fixture; | ||
|
||
$issues = []; | ||
|
||
if (!(PHP_VERSION_ID >= 70100)) { | ||
$issues[] = 'Your Composer dependencies require a PHP version ">= 7.1". You are running ' . PHP_VERSION . '.'; | ||
} | ||
|
||
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters