Skip to content

Commit

Permalink
[Php82] Skip usage of already RecursiveDirectoryIterator::SKIP_DOTS o…
Browse files Browse the repository at this point in the history
…n FilesystemIteratorSkipDotsRector (#5712)

* [Php82] Skip usage of RecursiveDirectoryIterator::SKIP_DOTS on FilesystemIteratorSkipDotsRector

* [Php82] Skip usage of RecursiveDirectoryIterator::SKIP_DOTS on FilesystemIteratorSkipDotsRector

* fix doc

* fix

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
samsonasik and actions-user authored Mar 11, 2024
1 parent 8ade0b9 commit 861a880
Show file tree
Hide file tree
Showing 14 changed files with 44 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDots;
namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDotsRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDots\Fixture;
namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDotsRector\Fixture;

use FilesystemIterator;

Expand All @@ -16,7 +16,7 @@ final class AppendConstantToFlag
-----
<?php

namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDots\Fixture;
namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDotsRector\Fixture;

use FilesystemIterator;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDots\Fixture;
namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDotsRector\Fixture;

use FilesystemIterator;

Expand All @@ -16,7 +16,7 @@ final class AppendConstantToMultipleFlags
-----
<?php

namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDots\Fixture;
namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDotsRector\Fixture;

use FilesystemIterator;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDots\Fixture;
namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDotsRector\Fixture;

final class AppendConstantToMultipleFlagsWithFqn
{
Expand All @@ -14,7 +14,7 @@ final class AppendConstantToMultipleFlagsWithFqn
-----
<?php

namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDots\Fixture;
namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDotsRector\Fixture;

final class AppendConstantToMultipleFlagsWithFqn
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDots\Fixture;
namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDotsRector\Fixture;

use FilesystemIterator;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDots\Fixture;
namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDotsRector\Fixture;

use FilesystemIterator;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDots\Fixture;
namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDotsRector\Fixture;

final class SkipConstantPresentWithFqn
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDots\Fixture;
namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDotsRector\Fixture;

use Symfony\Component\Console\Style\SymfonyStyle;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDotsRector\Fixture;

final class SkipRecursiveSkipDots
{
public function run()
{
new \RecursiveDirectoryIterator(
'/path/to/dir',
\RecursiveDirectoryIterator::SKIP_DOTS
);
}
}

?>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDots\Fixture;
namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDotsRector\Fixture;

use FilesystemIterator;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDots\Fixture;
namespace Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDotsRector\Fixture;

use FilesystemIterator;

Expand Down
23 changes: 14 additions & 9 deletions rules/Php82/Rector/New_/FilesystemIteratorSkipDotsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\Php82\Rector\New_;

use FilesystemIterator;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Expr;
Expand All @@ -13,31 +12,32 @@
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\ObjectType;
use Rector\NodeNameResolver\NodeNameResolver\ClassConstFetchNameResolver;
use Rector\PhpParser\Node\Value\ValueResolver;
use Rector\Rector\AbstractRector;
use Rector\ValueObject\PhpVersionFeature;
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see \Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDots\FilesystemIteratorSkipDotsRectorTest
* @see \Rector\Tests\Php82\Rector\New_\FilesystemIteratorSkipDotsRector\FilesystemIteratorSkipDotsRectorTest
*/
final class FilesystemIteratorSkipDotsRector extends AbstractRector implements MinPhpVersionInterface
{
public function __construct(
private readonly ClassConstFetchNameResolver $classConstFetchNameResolver
private readonly ValueResolver $valueResolver
) {
}

public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Prior PHP 8.2 FilesystemIterator::SKIP_DOTS was always set and could not be removed, therefore FilesystemIterator::SKIP_DOTS is added in order to keep this behaviour.',
[new CodeSample(
'new ' . FilesystemIterator::class . '(__DIR__, ' . FilesystemIterator::class . '::KEY_AS_FILENAME);',
'new ' . FilesystemIterator::class . '(__DIR__, ' . FilesystemIterator::class . '::KEY_AS_FILENAME | ' . FilesystemIterator::class . '::SKIP_DOTS);'
),
[
new CodeSample(
'new FilesystemIterator(__DIR__, FilesystemIterator::KEY_AS_FILENAME);',
'new FilesystemIterator(__DIR__, FilesystemIterator::KEY_AS_FILENAME | FilesystemIterator::SKIP_DOTS);'
),
]
);
}
Expand Down Expand Up @@ -108,6 +108,11 @@ private function isSkipDots(Expr $expr): bool
return true;
}

return $this->classConstFetchNameResolver->resolve($expr, null) === 'FilesystemIterator::SKIP_DOTS';
if (! defined('FilesystemIterator::SKIP_DOTS')) {
return true;
}

$value = constant('FilesystemIterator::SKIP_DOTS');
return $this->valueResolver->isValue($expr, $value);
}
}
3 changes: 1 addition & 2 deletions src/Autoloading/BootstrapFilesIncluder.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

namespace Rector\Autoloading;

use FilesystemIterator;
use Rector\Configuration\Option;
use Rector\Configuration\Parameter\SimpleParameterProvider;
use Rector\Exception\ShouldNotHappenException;
Expand Down Expand Up @@ -50,7 +49,7 @@ private function requireRectorStubs(): void

$dir = new RecursiveDirectoryIterator(
$stubsRectorDirectory,
RecursiveDirectoryIterator::SKIP_DOTS | FilesystemIterator::SKIP_DOTS
RecursiveDirectoryIterator::SKIP_DOTS
);
/** @var SplFileInfo[] $stubs */
$stubs = new RecursiveIteratorIterator($dir);
Expand Down

0 comments on commit 861a880

Please sign in to comment.