-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
allow @var phpdocs before return statements #5109
allow @var phpdocs before return statements #5109
Conversation
Alternative proposal related to this: #4649. |
Similar was rejected before #3611 |
seems like a slightly different use case? |
It's not a problem of 3rd party annotations, but interpretation of phpDocumentor specs. In linked ticket, it was interpreted that not specifying structural element in inline phpDoc, it's not conforming with phpDocumentor specs. In case of
|
I see. Thanks for the clarification
…On Tue, 11 Aug 2020, 17:52 Gabriel Ostrolucký, ***@***.***> wrote:
It's not a problem of 3rd party annotations, but interpretation of
phpDocumentor specs. In linked ticket, it was interpreted that not
specifying structural element in inline phpDoc, it's not conforming with
phpDocumentor specs. In case of @var, this is even more strictly
prohibitted. From
https://docs.phpdoc.org/latest/references/phpdoc/tags/var.html
Syntax
@var [“Type”] [$element_name] [<description>]
The @var tag MUST contain the name of the element it documents.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#5109 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAEYV4CR7PCBCTCZXAWK5DTSAFZLPANCNFSM4P3DDDQA>
.
|
I would just point that that phpstorm, phpstan and psalm all support this syntax. |
I think this fixer should just be turned off tbh, if you want that. This fixer was designed to enforce PSR-5 doc positioning. If you want to document types for static analyzers, then you probably don't want this fixer. I no longer use it, myself. |
But on the next line there is following:
A return statement operating only single property at once, so there is no need in name. |
A return statement is not a single property. A single property is this: class Foo
{
/** @var string */
public $foo; // <- i am a single property
} The phpdocumentor spec does not allow documentation above return statements. |
As I said before, in a different issue, at length, we have to decide if we want to follow phpdocumentor or static analyzers. phpdocumentor will never support stuff that makes the code easier to analyzem because its job is to document the public interface of stuff, not the internals. phpdocumentor says phpdoc can only appear before structural elements. |
You could argue that static analyzers started to miss-use phpdoc for this, but there's really no alternative and that's how "it evolved". The practical choice would be to go separate paths, but what about interop then? PS: I'm leaning towards being practical, but… |
I think PHP-CS-Fixer should follow the fairly-widely-adopted community standard. But I also think it's a shame that function returnsString(EncoderInterface $encoder): string
{
/** @infer string */
return $encoder->encode(
['invoice_number' => 1],
);
} is much more appropriate. |
People are used to write I dislike inline |
They're also used to will nonexistent variables into existence when used in the global scope, typically view files. A total mess :) |
Yeah, that's why I think |
If there is typehinted a parent class as return value in a parent method's signature, how would I tell to PHPStan that I 100% sure that in the subclass I will get a subclass of return value? // find() is overwritten above to ignore soft-deleted entities
public function findDeleted($id, $lockMode = null, $lockVersion = null): ?Project
{
// it returns object|null by default,
// I will get "should return Project|null but returns object|null" from PHPStan
return parent::find($id, $lockMode, $lockVersion);
} |
@fluffycondor You have many tools at your disposal: All of them are better than copying |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This PR changes the behaviour for a fixer, not fixing an issue, for many people the "fix" that this PR disables may be very much expected.
I would suggest adding an option ignore_before_return
(defaults to false
to keep backward compatibility) for anyone wanting changes from this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO this is a de facto standard so we should support it. But I agree with @kubawerlos, we should add an option for this behavior change.
@@ -190,6 +190,10 @@ private function isStructuralElement(Token $token) | |||
*/ | |||
private function isValidControl(Tokens $tokens, Token $docsToken, $controlIndex) | |||
{ | |||
if ($tokens[$controlIndex]->isGivenKind(T_RETURN) && false !== stripos($docsToken->getContent(), '@var')) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This condition will be true for any tag that contains @var
, e.g. @variable
. Please add such tag as a new test case to make sure it is still changed.
I agree this is a de facto standard so we should support it, I see little value in having it behind some option as I consider this a bug fix. |
I agree now, too. Disregard my earlier comments, or consider them in the context of 2020. ;) |
closing due to merge of #7402 |
This PR stops phpdocs before return statements from being converted to comments, if they are a
@var
definition.This is a common requirement with SA tooling to assist the tool with the return type.
e.g:
https://psalm.dev/r/0de99e28de
We don't want
/** @var string */
converted to// @var string
in this instance.Thanks.