Skip to content

Skip RemoveUnusedRequestParamRector on override abstract method - #394

Merged
samsonasik merged 1 commit into
rectorphp:mainfrom
JohJohan:366
May 1, 2023
Merged

Skip RemoveUnusedRequestParamRector on override abstract method#394
samsonasik merged 1 commit into
rectorphp:mainfrom
JohJohan:366

Conversation

@JohJohan

@JohJohan JohJohan commented Apr 26, 2023

Copy link
Copy Markdown
Contributor

Fixes #366

@ghost

ghost commented Apr 26, 2023

Copy link
Copy Markdown

Fixes #366

@ghost

ghost commented Apr 26, 2023

Copy link
Copy Markdown

I dont know how to fix the rector failing, that has probably nothing to do with my changes.

The PHPstan fails because undefined method but it does work can i add it to the ignores?

@TomasVotruba

Copy link
Copy Markdown
Member

@samsonasik It seems the BetterTokenIterator::getTokens() was removed. Do you have any idea how this happened?

@samsonasik

Copy link
Copy Markdown
Member

Latest php-doc-parser already have it, so I removed it as it extends the getTokens() in rector-src as extends it, see

php-doc-parser may need to be updated to at least version ^1.20.1

@samsonasik

Copy link
Copy Markdown
Member

I create PR to ensure rector-src got latest phpdoc-parser when loaded rectorphp/rector-src#3691

@TomasVotruba

Copy link
Copy Markdown
Member

Ah, I see now 😁

@samsonasik

Copy link
Copy Markdown
Member

Let's restart build.

@samsonasik samsonasik closed this Apr 27, 2023
@samsonasik samsonasik reopened this Apr 27, 2023
@samsonasik samsonasik self-assigned this Apr 27, 2023
@samsonasik

Copy link
Copy Markdown
Member

I will look into why it not load latest phpdoc-parser, @JohJohan I may cherry-pick your commit in case I can't push to your branch :)

@JohJohan

Copy link
Copy Markdown
Contributor Author

I will look into why it not load latest phpdoc-parser, @JohJohan I may cherry-pick your commit in case I can't push to your branch :)

Sounds good, good luck :)

@samsonasik

Copy link
Copy Markdown
Member

It seems the autoload is overlapped with phpstan.phar's inside build release 1.10.4 which still uses php-doc-parser pinned to 1.19.1, see https://github.com/phpstan/phpstan-src/blob/532094bc1b70b462c093bfc17eb47b149ff6cb0e/composer.json#L28

rector-src provide preload.php and preload-split-package.php, that's why it works there, I will check if it can be tweaked here :)

@samsonasik

Copy link
Copy Markdown
Member

Latest phpstan's dev-main already uses pinned php-doc-parser 1.20.3 https://github.com/phpstan/phpstan-src/blob/14f56abbd40a172259a6010820dc7e613ce7a516/composer.json#L28

kind of aliasing can work, but I will check if there is better solution.

@samsonasik

Copy link
Copy Markdown
Member

see rectorphp/rector-src#3692

@samsonasik

Copy link
Copy Markdown
Member

Let's restart build ...

@samsonasik samsonasik closed this Apr 27, 2023
@samsonasik samsonasik reopened this Apr 27, 2023
@samsonasik

Copy link
Copy Markdown
Member

@JohJohan I fixed the rector error 🎉 , I will let you fix the phpstan notice part :)

Comment thread phpstan.neon Outdated
- '#Parameter \#1 \$className of method Rector\\Core\\Reflection\\ReflectionResolver\:\:resolveMethodReflection\(\) expects class\-string, string given#'

-
message: '#Call to an undefined method PHPStan\\Reflection\\ExtendedMethodReflection\:\:isAbstract\(\)#'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be fixed with set actual object with isAbstract() method, you can debug actual object with dump($variable)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The actual class is PHPStan\Reflection\Php\PhpMethodReflection which has isAbstract but when i do something like assert($extendedMethodReflection instanceof PHPStan\Reflection\Php\PhpMethodReflection); i get error Class Rector\Symfony\Rector\ClassMethod\PHPStan\Reflection\Php\PhpMethodReflection not found.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to use FQCN, or that will refer current namespace:

-assert($extendedMethodReflection instanceof PHPStan\Reflection\Php\PhpMethodReflection); 
+assert($extendedMethodReflection instanceof \PHPStan\Reflection\Php\PhpMethodReflection); 

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aah thanks :)

@samsonasik samsonasik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please update PR title to something like:

Skip RemoveUnusedRequestParamRector on override abstract method

@JohJohan JohJohan changed the title Fixes 366 Skip RemoveUnusedRequestParamRector on override abstract method May 1, 2023
try {
$extendedMethodReflection = $reflectionParentClass->getNativeMethod($methodName);
assert($extendedMethodReflection instanceof PhpMethodReflection);
if (! $extendedMethodReflection->isAbstract()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice find, i thought there must have been something but i couldn't find it i keep my private function to do the reflection and checking see hasAbstractParentClassMethod is that okay?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's fine as you need to get ClassReflection instance 👍

abstract public function fooAction(Request $request): Response;
}

class DemoController extends AbstractController

@samsonasik samsonasik May 1, 2023

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs to be more specific name so it ease to look on the future, eg:

Suggested change
class DemoController extends AbstractController
class DontOverrideParentAbstractClassMethodController extends AbstractController

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++

@@ -0,0 +1,55 @@
<?php

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please rename fixture file to something like: do_not_override_parent_abstract_class_method.php.inc

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

++

# Failing Test for RemoveUnusedRequestParamRector
@samsonasik
samsonasik enabled auto-merge (squash) May 1, 2023 11:28
@samsonasik

Copy link
Copy Markdown
Member

Thank you @JohJohan

@samsonasik
samsonasik merged commit 7ace160 into rectorphp:main May 1, 2023
trandbert37 pushed a commit to trandbert37/rector-symfony that referenced this pull request May 3, 2023
…ectorphp#394)

# Failing Test for RemoveUnusedRequestParamRector

Co-authored-by: johan Vlaar <johan@adivare.nl>
@JohJohan
JohJohan deleted the 366 branch January 29, 2025 15:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

RemoveUnusedRequestParamRector causes bad PHP code when using abstracts

3 participants