Skip to content

Commit

Permalink
[Php82] Skip non-FilesystemIterator on FilesystemIteratorSkipDotsRect…
Browse files Browse the repository at this point in the history
…or (#3298)

* [Php82] Skip non-FilesystemIterator on FilesystemIteratorSkipDotsRector

* Fixed 🎉

* [ci-review] Rector Rectify

* skip use variable

* eol

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
samsonasik and actions-user committed Jan 21, 2023
1 parent 6ff4617 commit 9cb97aa
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

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

use Symfony\Component\Console\Style\SymfonyStyle;

final class SkipNonFileSystemIteratorInstance
{
public function run($input, $output)
{
return new SymfonyStyle($input, $output);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

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

use FilesystemIterator;

final class SkipUseVariable
{
public function getIterator(): FilesystemIterator
{
$variable = FilesystemIterator::SKIP_DOTS;
return new FilesystemIterator(__DIR__, $variable);
}
}

?>
8 changes: 7 additions & 1 deletion rules/Php82/Rector/New_/FilesystemIteratorSkipDotsRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use PhpParser\Node\Expr\ClassConstFetch;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name\FullyQualified;
use PHPStan\Type\ObjectType;
use Rector\Core\Rector\AbstractRector;
use Rector\Core\ValueObject\PhpVersionFeature;
use Rector\NodeNameResolver\NodeNameResolver\ClassConstFetchNameResolver;
Expand Down Expand Up @@ -53,6 +54,10 @@ public function getNodeTypes(): array
*/
public function refactor(Node $node): ?New_
{
if (! $this->isObjectType($node->class, new ObjectType('FilesystemIterator'))) {
return null;
}

if ($node->isFirstClassCallable()) {
return null;
}
Expand Down Expand Up @@ -98,7 +103,8 @@ private function isSkipDotsPresent(Expr $expr): bool
private function isSkipDots(Expr $expr): bool
{
if (! $expr instanceof ClassConstFetch) {
return false;
// can be anything
return true;
}

return $this->classConstFetchNameResolver->resolve($expr) === 'FilesystemIterator::SKIP_DOTS';
Expand Down

0 comments on commit 9cb97aa

Please sign in to comment.