mirror of
https://github.com/rectorphp/rector.git
synced 2024-05-31 16:30:51 +00:00
[DX] Check php version features parameter type (#983)
This commit is contained in:
parent
aae9a4049a
commit
2d6dba4055
|
@ -4,8 +4,11 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Rector\Core\Php;
|
namespace Rector\Core\Php;
|
||||||
|
|
||||||
|
use Nette\Utils\Strings;
|
||||||
use Rector\Core\Configuration\Option;
|
use Rector\Core\Configuration\Option;
|
||||||
|
use Rector\Core\Exception\Configuration\InvalidConfigurationException;
|
||||||
use Rector\Core\Php\PhpVersionResolver\ProjectComposerJsonPhpVersionResolver;
|
use Rector\Core\Php\PhpVersionResolver\ProjectComposerJsonPhpVersionResolver;
|
||||||
|
use Rector\Core\ValueObject\PhpVersion;
|
||||||
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
|
use Rector\Testing\PHPUnit\StaticPHPUnitEnvironment;
|
||||||
use Symplify\PackageBuilder\Parameter\ParameterProvider;
|
use Symplify\PackageBuilder\Parameter\ParameterProvider;
|
||||||
|
|
||||||
|
@ -14,6 +17,12 @@ use Symplify\PackageBuilder\Parameter\ParameterProvider;
|
||||||
*/
|
*/
|
||||||
final class PhpVersionProvider
|
final class PhpVersionProvider
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
* @see https://regex101.com/r/qBMnbl/1
|
||||||
|
*/
|
||||||
|
private const VALID_PHP_VERSION_REGEX = '#^\d{5,6}$#';
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private ParameterProvider $parameterProvider,
|
private ParameterProvider $parameterProvider,
|
||||||
private ProjectComposerJsonPhpVersionResolver $projectComposerJsonPhpVersionResolver
|
private ProjectComposerJsonPhpVersionResolver $projectComposerJsonPhpVersionResolver
|
||||||
|
@ -22,15 +31,17 @@ final class PhpVersionProvider
|
||||||
|
|
||||||
public function provide(): int
|
public function provide(): int
|
||||||
{
|
{
|
||||||
$phpVersionFeatures = $this->parameterProvider->provideIntParameter(Option::PHP_VERSION_FEATURES);
|
$phpVersionFeatures = $this->parameterProvider->provideParameter(Option::PHP_VERSION_FEATURES);
|
||||||
|
$this->validatePhpVersionFeaturesParameter($phpVersionFeatures);
|
||||||
|
|
||||||
if ($phpVersionFeatures > 0) {
|
if ($phpVersionFeatures > 0) {
|
||||||
return $phpVersionFeatures;
|
return $phpVersionFeatures;
|
||||||
}
|
}
|
||||||
|
|
||||||
// for tests
|
// for tests
|
||||||
if (StaticPHPUnitEnvironment::isPHPUnitRun()) {
|
if (StaticPHPUnitEnvironment::isPHPUnitRun()) {
|
||||||
// so we don't have to up
|
// so we don't have to keep with up with newest version
|
||||||
return 100000;
|
return PhpVersion::PHP_10;
|
||||||
}
|
}
|
||||||
|
|
||||||
$projectComposerJson = getcwd() . '/composer.json';
|
$projectComposerJson = getcwd() . '/composer.json';
|
||||||
|
@ -48,4 +59,43 @@ final class PhpVersionProvider
|
||||||
{
|
{
|
||||||
return $phpVersion <= $this->provide();
|
return $phpVersion <= $this->provide();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function validatePhpVersionFeaturesParameter(mixed $phpVersionFeatures): void
|
||||||
|
{
|
||||||
|
if ($phpVersionFeatures === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (PhpVersion::isValid($phpVersionFeatures)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (! is_int($phpVersionFeatures)) {
|
||||||
|
$this->throwInvalidTypeException($phpVersionFeatures);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (Strings::match(
|
||||||
|
(string) $phpVersionFeatures,
|
||||||
|
self::VALID_PHP_VERSION_REGEX
|
||||||
|
) && $phpVersionFeatures >= (PhpVersion::PHP_53 - 1)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->throwInvalidTypeException($phpVersionFeatures);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function throwInvalidTypeException(mixed $phpVersionFeatures): void
|
||||||
|
{
|
||||||
|
$errorMessage = sprintf(
|
||||||
|
'Parameter "%s::%s" must be int, "%s" given.%sUse constant from "%s" to provide it, e.g. "%s::%s"',
|
||||||
|
Option::class,
|
||||||
|
'PHP_VERSION_FEATURES',
|
||||||
|
(string) $phpVersionFeatures,
|
||||||
|
PHP_EOL,
|
||||||
|
PhpVersion::class,
|
||||||
|
PhpVersion::class,
|
||||||
|
'PHP_80'
|
||||||
|
);
|
||||||
|
throw new InvalidConfigurationException($errorMessage);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,9 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Rector\Core\ValueObject;
|
namespace Rector\Core\ValueObject;
|
||||||
|
|
||||||
final class PhpVersion
|
use MyCLabs\Enum\Enum;
|
||||||
|
|
||||||
|
final class PhpVersion extends Enum
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @api
|
* @api
|
||||||
|
|
|
@ -15,7 +15,7 @@ return static function (ContainerConfigurator $containerConfigurator): void {
|
||||||
]);
|
]);
|
||||||
|
|
||||||
// Define what rule sets will be applied
|
// Define what rule sets will be applied
|
||||||
$containerConfigurator->import(SetList::CODE_QUALITY);
|
$containerConfigurator->import(SetList::DEAD_CODE);
|
||||||
|
|
||||||
// get services (needed for register a single rule)
|
// get services (needed for register a single rule)
|
||||||
// $services = $containerConfigurator->services();
|
// $services = $containerConfigurator->services();
|
||||||
|
|
|
@ -4,22 +4,56 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace Rector\Core\Tests\Php;
|
namespace Rector\Core\Tests\Php;
|
||||||
|
|
||||||
|
use Iterator;
|
||||||
|
use Rector\Core\Exception\Configuration\InvalidConfigurationException;
|
||||||
use Rector\Core\Php\PhpVersionProvider;
|
use Rector\Core\Php\PhpVersionProvider;
|
||||||
use Rector\Testing\PHPUnit\AbstractTestCase;
|
use Rector\Testing\PHPUnit\AbstractTestCase;
|
||||||
|
use Symplify\SmartFileSystem\SmartFileInfo;
|
||||||
|
|
||||||
final class PhpVersionProviderTest extends AbstractTestCase
|
final class PhpVersionProviderTest extends AbstractTestCase
|
||||||
{
|
{
|
||||||
private PhpVersionProvider $phpVersionProvider;
|
/**
|
||||||
|
* @doesNotPerformAssertions
|
||||||
protected function setUp(): void
|
* @dataProvider provideValidConfigData()
|
||||||
|
*/
|
||||||
|
public function testValidInput(SmartFileInfo $invalidFileInfo): void
|
||||||
{
|
{
|
||||||
$this->boot();
|
$this->bootFromConfigFileInfos([$invalidFileInfo]);
|
||||||
$this->phpVersionProvider = $this->getService(PhpVersionProvider::class);
|
|
||||||
|
$phpVersionProvider = $this->getService(PhpVersionProvider::class);
|
||||||
|
$phpVersionProvider->provide();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test(): void
|
/**
|
||||||
|
* @return Iterator<SmartFileInfo[]>
|
||||||
|
*/
|
||||||
|
public function provideValidConfigData(): Iterator
|
||||||
{
|
{
|
||||||
$phpVersion = $this->phpVersionProvider->provide();
|
yield [new SmartFileInfo(__DIR__ . '/config/valid_explicit_value.php')];
|
||||||
$this->assertSame(100000, $phpVersion);
|
yield [new SmartFileInfo(__DIR__ . '/config/valid_minus_value.php')];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideInvalidConfigData()
|
||||||
|
*/
|
||||||
|
public function testInvalidInput(SmartFileInfo $invalidFileInfo): void
|
||||||
|
{
|
||||||
|
$this->expectException(InvalidConfigurationException::class);
|
||||||
|
|
||||||
|
$this->bootFromConfigFileInfos([$invalidFileInfo]);
|
||||||
|
|
||||||
|
$phpVersionProvider = $this->getService(PhpVersionProvider::class);
|
||||||
|
$phpVersionProvider->provide();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return Iterator<SmartFileInfo[]>
|
||||||
|
*/
|
||||||
|
public function provideInvalidConfigData(): Iterator
|
||||||
|
{
|
||||||
|
yield [new SmartFileInfo(__DIR__ . '/config/invalid_input.php')];
|
||||||
|
yield [new SmartFileInfo(__DIR__ . '/config/invalid_string_input.php')];
|
||||||
|
yield [new SmartFileInfo(__DIR__ . '/config/invalid_number_input.php')];
|
||||||
|
yield [new SmartFileInfo(__DIR__ . '/config/invalid_php_4_number.php')];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
11
tests/Php/config/invalid_input.php
Normal file
11
tests/Php/config/invalid_input.php
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Rector\Core\Configuration\Option;
|
||||||
|
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||||
|
|
||||||
|
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||||
|
$parameters = $containerConfigurator->parameters();
|
||||||
|
$parameters->set(Option::PHP_VERSION_FEATURES, '10000');
|
||||||
|
};
|
11
tests/Php/config/invalid_number_input.php
Normal file
11
tests/Php/config/invalid_number_input.php
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Rector\Core\Configuration\Option;
|
||||||
|
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||||
|
|
||||||
|
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||||
|
$parameters = $containerConfigurator->parameters();
|
||||||
|
$parameters->set(Option::PHP_VERSION_FEATURES, 291_084_902_184);
|
||||||
|
};
|
11
tests/Php/config/invalid_php_4_number.php
Normal file
11
tests/Php/config/invalid_php_4_number.php
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Rector\Core\Configuration\Option;
|
||||||
|
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||||
|
|
||||||
|
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||||
|
$parameters = $containerConfigurator->parameters();
|
||||||
|
$parameters->set(Option::PHP_VERSION_FEATURES, 40000);
|
||||||
|
};
|
11
tests/Php/config/invalid_string_input.php
Normal file
11
tests/Php/config/invalid_string_input.php
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Rector\Core\Configuration\Option;
|
||||||
|
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||||
|
|
||||||
|
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||||
|
$parameters = $containerConfigurator->parameters();
|
||||||
|
$parameters->set(Option::PHP_VERSION_FEATURES, '7.3');
|
||||||
|
};
|
11
tests/Php/config/valid_explicit_value.php
Normal file
11
tests/Php/config/valid_explicit_value.php
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Rector\Core\Configuration\Option;
|
||||||
|
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||||
|
|
||||||
|
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||||
|
$parameters = $containerConfigurator->parameters();
|
||||||
|
$parameters->set(Option::PHP_VERSION_FEATURES, 100000);
|
||||||
|
};
|
12
tests/Php/config/valid_minus_value.php
Normal file
12
tests/Php/config/valid_minus_value.php
Normal file
|
@ -0,0 +1,12 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
use Rector\Core\Configuration\Option;
|
||||||
|
use Rector\Core\ValueObject\PhpVersionFeature;
|
||||||
|
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
|
||||||
|
|
||||||
|
return static function (ContainerConfigurator $containerConfigurator): void {
|
||||||
|
$parameters = $containerConfigurator->parameters();
|
||||||
|
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersionFeature::ARRAY_KEY_FIRST_LAST - 1);
|
||||||
|
};
|
Loading…
Reference in New Issue
Block a user