Skip to content

Commit

Permalink
[CodeQuality] Skip possible data provider method called and used as a…
Browse files Browse the repository at this point in the history
…rray on YieldDataProviderRector (#404)

* [CodeQuality] Skip possible data provider method called and used as array on YieldDataProviderRector

* [ci-review] Rector Rectify

---------

Co-authored-by: GitHub Action <[email protected]>
  • Loading branch information
samsonasik and actions-user authored Nov 10, 2024
1 parent 38bdce4 commit 687747d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\YieldDataProviderRector\Fixture;

use PHPUnit\Framework\TestCase;

final class SkipUsedAsArray extends TestCase
{
#[\PHPUnit\Framework\Attributes\DataProvider('provideDataForProvider')]
public function test(string $filePath): void
{
}

public static function provideDataForProvider()
{
return [
['<?php implode("", $foo, );', '<?php implode($foo, "", );']
];
}

public function testOther()
{
$firstData = self::provideDataForProvider()[0];
}
}
13 changes: 10 additions & 3 deletions rules/CodeQuality/Rector/Class_/YieldDataProviderRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Analyser\Scope;
use PHPStan\PhpDocParser\Ast\PhpDoc\ReturnTagValueNode;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfoFactory;
use Rector\DeadCode\NodeAnalyzer\IsClassMethodUsedAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\PhpParser\NodeTransformer;
use Rector\PHPUnit\NodeAnalyzer\TestsNodeAnalyzer;
use Rector\PHPUnit\NodeFinder\DataProviderClassMethodFinder;
use Rector\Rector\AbstractRector;
use Rector\Rector\AbstractScopeAwareRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

Expand All @@ -26,13 +28,14 @@
*
* @see \Rector\PHPUnit\Tests\CodeQuality\Rector\Class_\YieldDataProviderRector\YieldDataProviderRectorTest
*/
final class YieldDataProviderRector extends AbstractRector
final class YieldDataProviderRector extends AbstractScopeAwareRector
{
public function __construct(
private readonly NodeTransformer $nodeTransformer,
private readonly TestsNodeAnalyzer $testsNodeAnalyzer,
private readonly DataProviderClassMethodFinder $dataProviderClassMethodFinder,
private readonly PhpDocInfoFactory $phpDocInfoFactory,
private readonly IsClassMethodUsedAnalyzer $isClassMethodUsedAnalyzer
) {
}

Expand Down Expand Up @@ -79,7 +82,7 @@ public function getNodeTypes(): array
/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Node
public function refactorWithScope(Node $node, Scope $scope): ?Class_
{
if (! $this->testsNodeAnalyzer->isInTestClass($node)) {
return null;
Expand All @@ -95,6 +98,10 @@ public function refactor(Node $node): ?Node
continue;
}

if ($this->isClassMethodUsedAnalyzer->isClassMethodUsed($node, $dataProviderClassMethod, $scope)) {
continue;
}

$this->transformArrayToYieldsOnMethodNode($dataProviderClassMethod, $array);
$hasChanged = true;
}
Expand Down

0 comments on commit 687747d

Please sign in to comment.