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

Infer return type from reflection if no phpdoc given #906

Merged
merged 1 commit into from
Apr 21, 2020

Conversation

mfn
Copy link
Collaborator

@mfn mfn commented Apr 1, 2020

Up until now, to have the get…Attribute return a useful type information in the class phpdoc, it was necessary to specify the @return annotation; otherwise the result was always mixed.

This now adds the capability to derive the type from the reflection itself.

  • Example:
    function getFooAttribute(): string
    {
    }
  • before * property-read mixed $foo
  • after * property-read string $foo

Note that the docblock is still the authoritative source if provided. The reason is that phpdoc expressiveness / supporting tooling still exceeds PHPs type capabilities.

But this also means if the docblock is wrong, so will the type be and this PR won't fix that.

OTOH it removes the need for a docblock for the simple cases (e.g. no iterables of a type).

Tests with lots-o-variations included.

This is in fact already implemented for the elseif (!method_exists('Illuminate\Database\Eloquent\Model', $method) case, but this part in fact prefers the native type over the docblock; not sure why and didn't want to change that for now.

Comment on lines +819 to +822
$type = $this->getReturnTypeFromDocBlock($reflection);
if ($type) {
return $type;
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

As mentioned, same behaviour as before: prefer docblock

Comment on lines -50 to +54
public function getAttributeReturnsImportedClass(): DateTime
public function getAttributeReturnsImportedClassAttribute(): DateTime
{
}

public function getAttributeReturnsFqnClass(): \Illuminate\Support\Facades\Date
public function getAttributeReturnsFqnClassAttribute(): \Illuminate\Support\Facades\Date
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Bugfix of my own tests 😅

* @property-read mixed $attribute_return_type_int_or_null
* @property-read int|null $attribute_return_type_int_or_null
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This shows the improvement…

Comment on lines -70 to +83
* @property-read mixed $attribute_with_int_return_type
* @property-read int $attribute_with_int_return_type
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

…this one as well

@mfn
Copy link
Collaborator Author

mfn commented Apr 1, 2020

Not sure why travis doesn't show up integrated here anymore, PR tests are executed => https://travis-ci.org/github/barryvdh/laravel-ide-helper/builds/669882120

@mfn
Copy link
Collaborator Author

mfn commented Apr 1, 2020

ping @barryvdh thanks for considering 🙇

@belamov
Copy link
Contributor

belamov commented Apr 11, 2020

I don't think that documentation should be the authoritative source of truth and we should always prioritize code itself and fallback to documentation if no return type provided

@mfn
Copy link
Collaborator Author

mfn commented Apr 11, 2020

I don't think that documentation should be the authoritative source of truth and we should always prioritize code itself and fallback to documentation if no return type provided

Unfortunately this will not work in practice because PHPs type system is inferior in terms of what types and combinations you can express.

Thereof it's my opinion the only sensible thing to do it to make the phpdoc authoritative!

@belamov
Copy link
Contributor

belamov commented Apr 11, 2020

i guess your talking about something like this

/**
 * @return \App\Models\Post[]
 */
public function allPosts(): array
{
    // ...
}

didn't think of this, it makes sense in this example

@mfn
Copy link
Collaborator Author

mfn commented Apr 11, 2020

Yes, exactly!

@splinter89
Copy link

Wow, thanks @mfn, this is exactly what I was looking for.
@barryvdh Please check this out 🙏
These "mixed" are killing me, and if I manually fix them, I can't use "--reset" option anymore.

@mfn
Copy link
Collaborator Author

mfn commented Apr 17, 2020

These "mixed" are killing me, and if I manually fix them, I can't use "--reset" option anymore.

You can add an explicit function phpdoc @return, then it works also with reset (with that I mean you don't / shouldn't edit it directly in the class phpdoc)

But yeah, having this PR merged would be great 😄

@mfn
Copy link
Collaborator Author

mfn commented Apr 21, 2020

Friendly ping @barryvdh 🤗

@barryvdh barryvdh merged commit 8381a2e into barryvdh:master Apr 21, 2020
@mr-feek
Copy link
Contributor

mr-feek commented Apr 21, 2020

thanks @mfn , this will be nice!

@mfn mfn deleted the mfn-return-reflection branch April 22, 2020 19:26
@mfn
Copy link
Collaborator Author

mfn commented Apr 22, 2020

It is! Already deleted lots of now-redundant phpdocs in my projects

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.

5 participants