Skip to content

Commit

Permalink
Merge FollowRequireByDirRector to almost identical AbsolutizeRequireA…
Browse files Browse the repository at this point in the history
…ndIncludePathRector (#2048)
  • Loading branch information
TomasVotruba committed Apr 10, 2022
1 parent 0227d24 commit 60f1dad
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 175 deletions.
2 changes: 0 additions & 2 deletions config/set/coding-style.php
Expand Up @@ -20,7 +20,6 @@
use Rector\CodingStyle\Rector\FuncCall\ConsistentPregDelimiterRector;
use Rector\CodingStyle\Rector\FuncCall\VersionCompareFuncCallToConstantRector;
use Rector\CodingStyle\Rector\If_\NullableCompareToNullRector;
use Rector\CodingStyle\Rector\Include_\FollowRequireByDirRector;
use Rector\CodingStyle\Rector\Plus\UseIncrementAssignRector;
use Rector\CodingStyle\Rector\PostInc\PostIncDecToPreIncDecRector;
use Rector\CodingStyle\Rector\Property\AddFalseDefaultToBoolPropertyRector;
Expand All @@ -44,7 +43,6 @@
$services->set(SplitGroupedConstantsAndPropertiesRector::class);
$services->set(StringClassNameToClassConstantRector::class);
$services->set(ConsistentPregDelimiterRector::class);
$services->set(FollowRequireByDirRector::class);
$services->set(CatchExceptionNameMatchingTypeRector::class);
$services->set(UseIncrementAssignRector::class);
$services->set(SplitDoubleAssignRector::class);
Expand Down
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Include_\FollowRequireByDirRector\Fixture;
namespace Rector\Tests\CodeQuality\Rector\Include_\AbsolutizeRequireAndIncludePathRector\Fixture;

class ExtraDot
{
Expand All @@ -15,7 +15,7 @@ class ExtraDot
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\Include_\FollowRequireByDirRector\Fixture;
namespace Rector\Tests\CodeQuality\Rector\Include_\AbsolutizeRequireAndIncludePathRector\Fixture;

class ExtraDot
{
Expand Down
@@ -1,8 +1,8 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Include_\FollowRequireByDirRector\Fixture;
namespace Rector\Tests\CodeQuality\Rector\Include_\AbsolutizeRequireAndIncludePathRector\Fixture;

class Fixture
final class FullList
{
public function run()
{
Expand All @@ -12,7 +12,7 @@ class Fixture
require_once 'autoload.php';
include 'autoload.php';
include_once 'autoload.php';
require 'path/' . $variable;

require $variable;
require __DIR__ . $variable;
require __DIR__ . 'string';
Expand All @@ -23,9 +23,9 @@ class Fixture
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\Include_\FollowRequireByDirRector\Fixture;
namespace Rector\Tests\CodeQuality\Rector\Include_\AbsolutizeRequireAndIncludePathRector\Fixture;

class Fixture
final class FullList
{
public function run()
{
Expand All @@ -35,7 +35,7 @@ class Fixture
require_once __DIR__ . '/autoload.php';
include __DIR__ . '/autoload.php';
include_once __DIR__ . '/autoload.php';
require __DIR__ . '/path/' . $variable;

require $variable;
require __DIR__ . $variable;
require __DIR__ . 'string';
Expand Down
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Include_\AbsolutizeRequireAndIncludePathRector\Fixture;

final class InterVariable
{
public function run(string $variable)
{
require 'path/' . $variable;
}
}

?>
-----
<?php

namespace Rector\Tests\CodeQuality\Rector\Include_\AbsolutizeRequireAndIncludePathRector\Fixture;

final class InterVariable
{
public function run(string $variable)
{
require __DIR__ . '/path/' . $variable;
}
}

?>
@@ -1,13 +1,12 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Include_\FollowRequireByDirRector\Fixture;
namespace Rector\Tests\CodeQuality\Rector\Include_\AbsolutizeRequireAndIncludePathRector\Fixture;

class Phar
{
public function run()
{
require 'vendor/autoload.php';
require '/vendor/autoload.php';
require 'phar://vendor/autoload.php';
}
}
Expand All @@ -16,13 +15,12 @@ class Phar
-----
<?php

namespace Rector\Tests\CodingStyle\Rector\Include_\FollowRequireByDirRector\Fixture;
namespace Rector\Tests\CodeQuality\Rector\Include_\AbsolutizeRequireAndIncludePathRector\Fixture;

class Phar
{
public function run()
{
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/vendor/autoload.php';
require 'phar://vendor/autoload.php';
}
Expand Down
@@ -0,0 +1,11 @@
<?php

namespace Rector\Tests\CodeQuality\Rector\Include_\AbsolutizeRequireAndIncludePathRector\Fixture;

final class SkipAbsolutePaths
{
public function run()
{
require '/vendor/autoload.php';
}
}

This file was deleted.

This file was deleted.

Expand Up @@ -68,10 +68,22 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?Node
{
if ($node->expr instanceof Concat && $node->expr->left instanceof String_ && $this->isRefactorableStringPath(
$node->expr->left
)) {
$node->expr->left = $this->prefixWithDirConstant($node->expr->left);

return $node;
}

if (! $node->expr instanceof String_) {
return null;
}

if (! $this->isRefactorableStringPath($node->expr)) {
return null;
}

/** @var string $includeValue */
$includeValue = $this->valueResolver->getValue($node->expr);

Expand All @@ -96,8 +108,42 @@ public function refactor(Node $node): ?Node
$node->expr->value = '/' . $includeValue;
}

$node->expr = new Concat(new Dir(), $node->expr);
$node->expr = $this->prefixWithDirConstant($node->expr);

return $node;
}

private function isRefactorableStringPath(String_ $string): bool
{
return ! \str_starts_with($string->value, 'phar://');
}

private function prefixWithDirConstant(String_ $string): Concat
{
$this->removeExtraDotSlash($string);
$this->prependSlashIfMissing($string);

return new Concat(new Dir(), $string);
}

/**
* Remove "./" which would break the path
*/
private function removeExtraDotSlash(String_ $string): void
{
if (! \str_starts_with($string->value, './')) {
return;
}

$string->value = Strings::replace($string->value, '#^\.\/#', '/');
}

private function prependSlashIfMissing(String_ $string): void
{
if (\str_starts_with($string->value, '/')) {
return;
}

$string->value = '/' . $string->value;
}
}
116 changes: 0 additions & 116 deletions rules/CodingStyle/Rector/Include_/FollowRequireByDirRector.php

This file was deleted.

0 comments on commit 60f1dad

Please sign in to comment.