Skip to content

Update rule with proper syntax of ReturnArrayClassMethodToYieldRector#2277

Merged
TomasVotruba merged 1 commit intorectorphp:masterfrom
gnutix:patch-1
Nov 6, 2019
Merged

Update rule with proper syntax of ReturnArrayClassMethodToYieldRector#2277
TomasVotruba merged 1 commit intorectorphp:masterfrom
gnutix:patch-1

Conversation

@gnutix
Copy link
Copy Markdown
Contributor

@gnutix gnutix commented Nov 6, 2019

No description provided.

@gnutix
Copy link
Copy Markdown
Contributor Author

gnutix commented Nov 6, 2019

Also I was thinking : what about supporting methods referenced by the @dataProvider [methodName] PHPUnit annotation ? Do you think it should be implemented ? It would be more robust than the current provide* / dataProvider* matching, which isn't even a standard / recommended pattern from PHPUnit as far as I know ?

Example of a failing test case packages/CodingStyle/tests/Rector/ClassMethod/ReturnArrayClassMethodToYieldRector/Fixture/data_provider_via_annotation_only.php.inc :

<?php

namespace Rector\CodingStyle\Tests\Rector\ClassMethod\ReturnArrayClassMethodToYieldRector\Fixture;

use Rector\CodingStyle\Tests\Rector\ClassMethod\ReturnArrayClassMethodToYieldRector\Source\ParentTestCase;

class DataProviderViaAnnotationOnly extends ParentTestCase
{
    /**
     * @dataProvider someMethod
     */
    public function testSomething($arg1, $arg2, $arg3): void
    {
    }

    public function someMethod(): array
    {
        return [['some', 'test', 'arguments']];
    }
}

?>
-----
<?php

namespace Rector\CodingStyle\Tests\Rector\ClassMethod\ReturnArrayClassMethodToYieldRector\Fixture;

use Rector\CodingStyle\Tests\Rector\ClassMethod\ReturnArrayClassMethodToYieldRector\Source\ParentTestCase;

class DataProviderViaAnnotationOnly extends ParentTestCase
{
    /**
     * @dataProvider someMethod
     */
    public function testSomething($arg1, $arg2, $arg3): void
    {
    }

    public function someMethod(): \Iterator
    {
        yield ['some', 'test', 'arguments'];
    }
}

?>

Which would probably mean relying on a \PHPUnit\Framework\TestCase stub instead of this Source\ParentTestCase fake.

@gnutix gnutix requested a review from TomasVotruba November 6, 2019 22:21
@gnutix gnutix mentioned this pull request Nov 6, 2019
@TomasVotruba TomasVotruba merged commit a6eecff into rectorphp:master Nov 6, 2019
@TomasVotruba TomasVotruba deleted the patch-1 branch November 6, 2019 22:47
@TomasVotruba
Copy link
Copy Markdown
Member

Well, this rule is generic and configurable to any class/pattern, not just data providers (it's just an example).

If you want to add such a rule for data providers, you can re-use this in a new rule, that would collect all method names from @dataProvider annotation and then apply the logic on there class methdos.

TomasVotruba added a commit that referenced this pull request May 10, 2022
rectorphp/rector-src@30f2f91 [CodeQuality][DeadCode] Handle SimplifyUselessVariableRector+RemoveOverriddenValuesRector (#2277)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants