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

fix: wrong return annotation #258

Merged
merged 2 commits into from
Jun 4, 2024
Merged

fix: wrong return annotation #258

merged 2 commits into from
Jun 4, 2024

Conversation

Mohammad-Alavi
Copy link
Contributor

@Mohammad-Alavi Mohammad-Alavi commented Jun 1, 2024

Summary

The incorrect annotation is causing confusion in static analysis.
The create method actually returns \Spatie\Fractal\Fractal, not \Spatie\Fractalistic\Fractal. When Fractal::create is called, it invokes the parent (Fractalistic) create method using parent::create. This parent method, in turn, uses the new static keyword to return an instance of the subclass when called from that subclass. Therefore, the return type of Fractal::create is actually Fractal, not Fractalistic.
So, by changing the annotated return type to @return static, we can easily extend \Spatie\Fractal\Fractal and the return type problem will be solved.

Problem

Consider this example: You've created a macro named ok on Fractal. However, your IDE complains that the ok method doesn't exist on Fractalistic. The issue lies in the @return annotation, which incorrectly indicates that create returns Fractalistic instead of Fractal. By correcting this annotation, static analyzers will recognize the macro on Fractal.

image

@freekmurze freekmurze merged commit 0a30630 into spatie:main Jun 4, 2024
1 check passed
@freekmurze
Copy link
Member

Thanks!

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