mirror of
https://github.com/rectorphp/rector.git
synced 2025-02-20 08:05:29 +01:00
[CodeQuality] Add AbsolutizeRequireAndIncludePathRector
This commit is contained in:
parent
48030e7257
commit
2b60867493
@ -47,3 +47,4 @@ services:
|
||||
Rector\CodeQuality\Rector\FuncCall\ArrayMergeOfNonArraysToSimpleArrayRector: ~
|
||||
Rector\CodeQuality\Rector\FuncCall\IntvalToTypeCastRector: ~
|
||||
Rector\CodeQuality\Rector\Ternary\ArrayKeyExistsTernaryThenValueToCoalescingRector: ~
|
||||
Rector\CodeQuality\Rector\Include_\AbsolutizeRequireAndIncludePathRector: ~
|
||||
|
@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Rector\Include_;
|
||||
|
||||
use Nette\Utils\Strings;
|
||||
use PhpParser\Node;
|
||||
use PhpParser\Node\Expr\BinaryOp\Concat;
|
||||
use PhpParser\Node\Expr\Include_;
|
||||
use PhpParser\Node\Scalar\String_;
|
||||
use Rector\Rector\AbstractRector;
|
||||
use Rector\RectorDefinition\CodeSample;
|
||||
use Rector\RectorDefinition\RectorDefinition;
|
||||
|
||||
/**
|
||||
* @see https://github.com/symplify/CodingStandard#includerequire-should-be-followed-by-absolute-path
|
||||
*
|
||||
* @see \Rector\CodeQuality\Tests\Rector\Include_\AbsolutizeRequireAndIncludePathRector\AbsolutizeRequireAndIncludePathRectorTest
|
||||
*/
|
||||
final class AbsolutizeRequireAndIncludePathRector extends AbstractRector
|
||||
{
|
||||
public function getDefinition(): RectorDefinition
|
||||
{
|
||||
return new RectorDefinition('include/require to absolute path', [
|
||||
new CodeSample(
|
||||
<<<'PHP'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
require 'autoload.php';
|
||||
|
||||
require $variable;
|
||||
}
|
||||
}
|
||||
PHP
|
||||
,
|
||||
<<<'PHP'
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
require __DIR__ . '/autoload.php';
|
||||
|
||||
require $variable;
|
||||
}
|
||||
}
|
||||
PHP
|
||||
|
||||
),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getNodeTypes(): array
|
||||
{
|
||||
return [Include_::class];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Include_ $node
|
||||
*/
|
||||
public function refactor(Node $node): ?Node
|
||||
{
|
||||
if (! $node->expr instanceof String_) {
|
||||
return null;
|
||||
}
|
||||
|
||||
/** @var string $includeValue */
|
||||
$includeValue = $this->getValue($node->expr);
|
||||
|
||||
// skip phar
|
||||
if (Strings::startsWith($includeValue, 'phar://')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// add preslash to string
|
||||
if (! Strings::startsWith($includeValue, '/')) {
|
||||
// keep dots
|
||||
if (! Strings::startsWith($includeValue, '.')) {
|
||||
$node->expr->value = '/' . $includeValue;
|
||||
}
|
||||
}
|
||||
|
||||
$node->expr = new Concat(new Node\Scalar\MagicConst\Dir(), $node->expr);
|
||||
|
||||
return $node;
|
||||
}
|
||||
}
|
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Include_\AbsolutizeRequireAndIncludePathRector;
|
||||
|
||||
use Iterator;
|
||||
use Rector\CodeQuality\Rector\Include_\AbsolutizeRequireAndIncludePathRector;
|
||||
use Rector\Testing\PHPUnit\AbstractRectorTestCase;
|
||||
|
||||
final class AbsolutizeRequireAndIncludePathRectorTest extends AbstractRectorTestCase
|
||||
{
|
||||
/**
|
||||
* @dataProvider provideDataForTest()
|
||||
*/
|
||||
public function test(string $file): void
|
||||
{
|
||||
$this->doTestFile($file);
|
||||
}
|
||||
|
||||
public function provideDataForTest(): Iterator
|
||||
{
|
||||
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
|
||||
}
|
||||
|
||||
protected function getRectorClass(): string
|
||||
{
|
||||
return AbsolutizeRequireAndIncludePathRector::class;
|
||||
}
|
||||
}
|
@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Include_\AbsolutizeRequireAndIncludePathRector\Fixture;
|
||||
|
||||
class Dots
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
require './vendor/autoload.php';
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Include_\AbsolutizeRequireAndIncludePathRector\Fixture;
|
||||
|
||||
class Dots
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
require __DIR__ . './vendor/autoload.php';
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Include_\AbsolutizeRequireAndIncludePathRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
require 'autoload.php';
|
||||
|
||||
require $variable;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Include_\AbsolutizeRequireAndIncludePathRector\Fixture;
|
||||
|
||||
class SomeClass
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
require __DIR__ . '/autoload.php';
|
||||
|
||||
require $variable;
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Include_\AbsolutizeRequireAndIncludePathRector\Fixture;
|
||||
|
||||
class VendorPhar
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
include '/vendor/autoload.php';
|
||||
|
||||
include_once 'phar://vendor/autoload.php';
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
-----
|
||||
<?php
|
||||
|
||||
namespace Rector\CodeQuality\Tests\Rector\Include_\AbsolutizeRequireAndIncludePathRector\Fixture;
|
||||
|
||||
class VendorPhar
|
||||
{
|
||||
public function run()
|
||||
{
|
||||
require __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
include __DIR__ . '/vendor/autoload.php';
|
||||
|
||||
include_once 'phar://vendor/autoload.php';
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
x
Reference in New Issue
Block a user