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] Skip @template tag on RemoveUselessVarTagRector #6396

Merged
merged 8 commits into from
Oct 26, 2024

Conversation

samsonasik
Copy link
Member

@samsonasik samsonasik marked this pull request as draft October 24, 2024 10:53
Comment on lines 39 to 43
// NonExistingObjectType may refer to @template tag defined in class
if ($docType instanceof NonExistingObjectType && ! str_contains($docType->getClassName(), '\\')) {
dump($docType);
return false;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

@dragosprotung this can be tweak solution, but it will cause invalid skipped real non existing object type, see fixture /~https://github.com/rectorphp/rector-src/pull/6396/files#diff-d9d0d07e6a9125b667f68974544d4c6567c6b31da986c9644d725c188257f378

There was 1 failure:

1) Rector\Tests\DeadCode\Rector\Property\RemoveUselessVarTagRector\RemoveUselessVarTagRectorTest::test with data set #3 ('/home/runner/work/rector-src/...hp.inc')
Failed on fixture file "non_existing_object.php.inc"
Failed asserting that string matches format description.
--- Expected
+++ Actual
@@ @@
 
 final class NonExistingObject
 {
+    /** @var SomeNonExistingObject */
     private Properties|null $properties;

Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know if this is something that is acceptable. If the referenced type is somehow not discoverable by Rector, is the job of DeadVarTagValueNodeAnalyzer to remove the tag ?

Also, could we not check if $docType is a template NonExistingObjectType->getClassName() = TType?

Copy link
Member Author

Choose a reason for hiding this comment

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

The patch seems needs to be in ObjectTypeSpecifier to locate @template if exists before marking as NonExistingObjectType

// probably in same namespace
if ($scope instanceof Scope) {
$namespaceName = $scope->getNamespace();
if ($namespaceName !== null) {
$newClassName = $namespaceName . '\\' . $className;
if ($this->reflectionProvider->hasClass($newClassName)) {
return new FullyQualifiedObjectType($newClassName);
}
}
}
// invalid type
return new NonExistingObjectType($className);

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, you might be right. Unfortunatly I do not have enought knowlage of PHPStan to find out if the type is a template or not.

Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be a problem if the unknown type is not removed ?

Copy link
Member Author

Choose a reason for hiding this comment

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

@template tag should be detected, unknown type can be just typo missing FQCN on class name, which should be removed as pointed to invalid class target.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed by get @template from ClassReflection 02b566a

current limitation is currently only detect single @template that can be improved in separate PR when possible :)

Copy link
Member Author

Choose a reason for hiding this comment

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

It seems cause invalid rectify 366194b , I will check more :)

Copy link
Member Author

Choose a reason for hiding this comment

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

should be fixed now :)

Comment on lines 39 to 43
// NonExistingObjectType may refer to @template tag defined in class
if ($docType instanceof NonExistingObjectType && ! str_contains($docType->getClassName(), '\\')) {
dump($docType);
return false;
}
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't know if this is something that is acceptable. If the referenced type is somehow not discoverable by Rector, is the job of DeadVarTagValueNodeAnalyzer to remove the tag ?

Also, could we not check if $docType is a template NonExistingObjectType->getClassName() = TType?

@samsonasik samsonasik marked this pull request as ready for review October 26, 2024 11:14
@samsonasik
Copy link
Member Author

All checks have passed 🎉 @TomasVotruba I am merging it to have faster feedback to test ;)

@samsonasik samsonasik merged commit 6e1af03 into main Oct 26, 2024
36 checks passed
@samsonasik samsonasik deleted the skip-templaet-tag branch October 26, 2024 11:37
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.

False positive of RemoveUselessVarTagRector
3 participants