Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DeadCode] Anonymous class implementing an interface doesn't respect interface signature #1367

Merged
merged 2 commits into from
May 5, 2019

Conversation

pierredup
Copy link
Contributor

I found this while running the dead code level on a code base. I don't have a fix for it yet, but thought I'd open a PR so long with a failing test case while I investigate further (and maybe get some pointers to the right solution)

When instantiating an anonymous class that implements an interface, and the methods in the class is empty, then all the parameters in the method signature is removed. The signature that comes from the interface isn't respected. When running the test case in the PR, I get the following failure:

Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
         {
             $class = new class implements Foo
             {
-                public function bar($value1)
+                public function bar()
                 {
                 }
             };\n

but I expect the test to pass, since the function bar matches the signature in the interface it implements.

The strange thing, though, is that in the RemoveUnusedParameterRector class, the line $classNode = $node->getAttribute(AttributeKey::CLASS_NODE); returns the current class node, and not the anonymous class node. So I think the issue might lie when traversing the nodes, and not with the actual rector class. I'm still digging through everything, but any nudge in the right direction would be appreciated.

@TomasVotruba
Copy link
Member

TomasVotruba commented May 1, 2019

There are many edge-cases in anonymous classes, that are hard to resolve right.
I think anonymous classes should be skipped by this rule.

As for the incorrect class node: each node is decorated by it's parent node:

$node->setAttribute(AttributeKey::CLASS_NODE, $this->classNode);

The class is probably incorrectly resolved here:

if ($node instanceof Class_ && $this->isClassAnonymous($node)) {

@pierredup
Copy link
Contributor Author

Thanks @TomasVotruba, I updated the PR with a patch for handling methods on an anonymous class.

@TomasVotruba
Copy link
Member

Thank you, looks good 👍

@TomasVotruba TomasVotruba merged commit c63e86b into rectorphp:master May 5, 2019
@pierredup pierredup deleted the anonymous-class-in-method branch May 5, 2019 19:30
TomasVotruba added a commit that referenced this pull request Dec 2, 2021
rectorphp/rector-src@922e91d [Php81] Skip non private modifier on ReadOnlyPropertyRector (#1367)
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