rectorphp / rector-phpunit

Rector upgrade rules for PHPUnit
http://getrector.com
MIT License
61 stars 46 forks source link

Handle non static methods in data providers #255

Closed themark147 closed 6 months ago

samsonasik commented 10 months ago

Could you try provide a patch as well, it should be something like this:

            $this->traverseNodesWithCallable((array) $dataProviderClassMethod->stmts, function (Node $subNode) {
                if ($subNode instanceof Class_ || $subNode instanceof Node\Stmt\Function_ || $subNode instanceof Node\Expr\Closure) {
                    return \PhpParser\NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
                }

                if (! $subNode instanceof Node\Expr\MethodCall) {
                    return null;
                }

                if ($subNode->isFirstClassCallable()) {
                    return null;
                }

                if (! $this->isName($subNode->var, 'this')) {
                    return null;
                }

                if (! $subNode->name instanceof Node\Identifier) {
                    return null;
                }

                // use self:: instead of static::
                return $this->nodeFactory->createStaticCall('self', $subNode->name->toString(), $subNode->getArgs());
            });

before set:

$hasChanged = true;

on line 108.

If you phpstan notice on code too complex, you can move to separate private method and call it.

After that, run:

vendor/bin/rector && composer fix-cs
themark147 commented 9 months ago

@samsonasik Hey, I already implemented a patch what u provide ... hope its fine :))

samsonasik commented 9 months ago

Could you CI error :) ? You can reproduce locally for that, first, install the dependencies:

export COMPOSER_ROOT_VERSION=dev-main && composer install

Then, run:

vendor/bin/phpunit

then patch it, after that, run:

vendor/bin/rector && composer fix-cs

then, verify if there is phpstan error:

vendor/bin/phpstan

if the error is code complexity, move the code to separate private method, then run once more:

vendor/bin/rector && composer fix-cs

Thank you.

themark147 commented 9 months ago

@samsonasik I already tried use that patch to refactor real project and it refactor well just called methods still remains NON static EDIT: I tried to used Rector\Php70\Rector\StaticCall\StaticCallOnNonStaticToInstanceCallRector as well

samsonasik commented 6 months ago

this currently skipped on latest main, so I am closing it.