[PHP 5.3] Add DirConstToFileConstRector (#6363)

This commit is contained in:
Tomas Votruba 2021-05-05 23:56:59 +02:00 committed by GitHub
parent 30cc794094
commit 5fe3eb555f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
22 changed files with 170 additions and 32 deletions

View File

@ -0,0 +1,15 @@
<?php
declare(strict_types=1);
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$parameters = $containerConfigurator->parameters();
$parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_53);
$services = $containerConfigurator->services();
$services->set(\Rector\DowngradePhp53\Rector\Dir\DirConstToFileConstRector::class);
};

View File

@ -4,9 +4,9 @@ declare(strict_types=1);
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
use Rector\Downgrade72\Rector\FuncCall\DowngradeStreamIsattyRector;
use Rector\DowngradePhp72\Rector\Class_\DowngradeParameterTypeWideningRector;
use Rector\DowngradePhp72\Rector\FuncCall\DowngradePregUnmatchedAsNullConstantRector;
use Rector\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector;
use Rector\DowngradePhp72\Rector\FunctionLike\DowngradeObjectTypeDeclarationRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

View File

@ -4,7 +4,7 @@ declare(strict_types=1);
use Rector\Core\Configuration\Option;
use Rector\Core\ValueObject\PhpVersion;
use Rector\Downgrade73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector;
use Rector\DowngradePhp73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector;
use Rector\DowngradePhp73\Rector\FuncCall\DowngradeTrailingCommasInFunctionCallsRector;
use Rector\DowngradePhp73\Rector\FuncCall\SetCookieOptionsArrayToArgumentsRector;
use Rector\DowngradePhp73\Rector\List_\DowngradeListReferenceAssignmentRector;

View File

@ -26,7 +26,7 @@
- [Downgrade72](#downgrade72) (1)
- [Downgrade73](#downgrade73) (1)
- [DowngradePhp73](#DowngradePhp73) (1)
- [DowngradePhp70](#downgradephp70) (6)
@ -4239,13 +4239,13 @@ Downgrade `stream_isatty()` function
<br>
## Downgrade73
## DowngradePhp73
### DowngradeArrayKeyFirstLastRector
Downgrade `array_key_first()` and `array_key_last()` functions
- class: [`Rector\Downgrade73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector`](../rules/Downgrade73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php)
- class: [`Rector\DowngradePhp73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector`](../rules/DowngradePhp73/Rector/FuncCall/DowngradeArrayKeyFirstLastRector.php)
```diff
class SomeClass

View File

@ -8,6 +8,11 @@ use Rector\Set\Contract\SetListInterface;
final class DowngradeSetList implements SetListInterface
{
/**
* @var string
*/
public const PHP_53 = __DIR__ . '/../../../config/set/downgrade-php53.php';
/**
* @var string
*/

View File

@ -112,11 +112,6 @@ final class SetList implements SetListInterface
*/
public const CODING_STYLE = __DIR__ . '/../../../config/set/coding-style.php';
/**
* @var string
*/
public const CONTRIBUTTE_TO_SYMFONY = __DIR__ . '/../../../config/set/contributte-to-symfony.php';
/**
* @var string
*/

View File

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Rector\Tests\DowngradePhp53\Rector\Dir\DirConstToFileConstRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
use Symplify\SmartFileSystem\SmartFileInfo;
final class DirConstToFileConstRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}
/**
* @return Iterator<SmartFileInfo>
*/
public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}
public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}

View File

@ -0,0 +1,27 @@
<?php
namespace Rector\Tests\DowngradePhp53\Rector\Dir\DirConstToFileConstRector\Fixture;
final class SomeClass
{
public function run()
{
return __DIR__;
}
}
?>
-----
<?php
namespace Rector\Tests\DowngradePhp53\Rector\Dir\DirConstToFileConstRector\Fixture;
final class SomeClass
{
public function run()
{
return dirname(__FILE__);
}
}
?>

View File

@ -2,10 +2,11 @@
declare(strict_types=1);
use Rector\NetteToSymfony\Rector\ClassMethod\RenameEventNamesInEventSubscriberRector;
use Rector\DowngradePhp53\Rector\Dir\DirConstToFileConstRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(RenameEventNamesInEventSubscriberRector::class);
$services->set(DirConstToFileConstRector::class);
};

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Rector\Tests\Downgrade72\Rector\FuncCall\DowngradeStreamIsattyRector;
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\Tests\Downgrade72\Rector\FuncCall\DowngradeStreamIsattyRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector\Fixture;
class SomeClass
{
@ -14,7 +14,7 @@ class SomeClass
-----
<?php
namespace Rector\Tests\Downgrade72\Rector\FuncCall\DowngradeStreamIsattyRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector\Fixture;
class SomeClass
{

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\Tests\Downgrade72\Rector\FuncCall\DowngradeStreamIsattyRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector\Fixture;
final class UnionOr
{
@ -25,7 +25,7 @@ final class UnionOr
-----
<?php
namespace Rector\Tests\Downgrade72\Rector\FuncCall\DowngradeStreamIsattyRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector\Fixture;
final class UnionOr
{

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\Tests\Downgrade72\Rector\FuncCall\DowngradeStreamIsattyRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector\Fixture;
class SomeClass
{
@ -14,7 +14,7 @@ class SomeClass
-----
<?php
namespace Rector\Tests\Downgrade72\Rector\FuncCall\DowngradeStreamIsattyRector\Fixture;
namespace Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector\Fixture;
class SomeClass
{

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
use Rector\Downgrade72\Rector\FuncCall\DowngradeStreamIsattyRector;
use Rector\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Rector\Tests\Downgrade73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector;
namespace Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector;
use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\Tests\Downgrade73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector\Fixture;
namespace Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector\Fixture;
class SomeClass
{
@ -14,7 +14,7 @@ class SomeClass
-----
<?php
namespace Rector\Tests\Downgrade73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector\Fixture;
namespace Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector\Fixture;
class SomeClass
{

View File

@ -1,6 +1,6 @@
<?php
namespace Rector\Tests\Downgrade73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector\Fixture;
namespace Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector\Fixture;
class SomeClass
{
@ -14,7 +14,7 @@ class SomeClass
-----
<?php
namespace Rector\Tests\Downgrade73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector\Fixture;
namespace Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector\Fixture;
class SomeClass
{

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
use Rector\Downgrade73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector;
use Rector\DowngradePhp73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
return static function (ContainerConfigurator $containerConfigurator): void {

View File

@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace Rector\DowngradePhp53\Rector\Dir;
use PhpParser\Node;
use PhpParser\Node\Scalar\MagicConst\Dir;
use PhpParser\Node\Scalar\MagicConst\File;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://github.com/nikic/PHP-Backporter/blob/master/lib/PHPBackporter/Converter/Dir.php
*
* @see \Rector\Tests\DowngradePhp53\Rector\Dir\DirConstToFileConstRector\DirConstToFileConstRectorTest
*/
final class DirConstToFileConstRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Refactor __DIR__ to dirname(__FILE__)', [
new CodeSample(
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run()
{
return __DIR__;
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
final class SomeClass
{
public function run()
{
return dirname(__FILE__);
}
}
CODE_SAMPLE
),
]);
}
/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Dir::class];
}
/**
* @param Dir $node
*/
public function refactor(Node $node): ?Node
{
return $this->nodeFactory->createFuncCall('dirname', [new File()]);
}
}

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Rector\Downgrade72\Rector\FuncCall;
namespace Rector\DowngradePhp72\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Expr\Assign;
@ -19,7 +19,7 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @changelog https://github.com/symfony/polyfill/commit/cc2bf55accd32b989348e2039e8c91cde46aebed
*
* @see \Rector\Tests\Downgrade72\Rector\FuncCall\DowngradeStreamIsattyRector\DowngradeStreamIsattyRectorTest
* @see \Rector\Tests\DowngradePhp72\Rector\FuncCall\DowngradeStreamIsattyRector\DowngradeStreamIsattyRectorTest
*/
final class DowngradeStreamIsattyRector extends AbstractRector
{
@ -64,7 +64,6 @@ class SomeClass
}
}
CODE_SAMPLE
),
]);
}

View File

@ -2,7 +2,7 @@
declare(strict_types=1);
namespace Rector\Downgrade73\Rector\FuncCall;
namespace Rector\DowngradePhp73\Rector\FuncCall;
use PhpParser\Node;
use PhpParser\Node\Expr\FuncCall;
@ -14,7 +14,7 @@ use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
/**
* @see https://wiki.php.net/rfc/array_key_first_last
*
* @see \Rector\Tests\Downgrade73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector\DowngradeArrayKeyFirstLastRectorTest
* @see \Rector\Tests\DowngradePhp73\Rector\FuncCall\DowngradeArrayKeyFirstLastRector\DowngradeArrayKeyFirstLastRectorTest
*/
final class DowngradeArrayKeyFirstLastRector extends AbstractRector
{
@ -43,7 +43,6 @@ class SomeClass
}
}
CODE_SAMPLE
),
]);
}