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

feat(PhpdocToCommentFixer): Add option to handle return as valid docblock usage (#7401) #7402

Merged
merged 12 commits into from Dec 15, 2023
Merged

feat(PhpdocToCommentFixer): Add option to handle return as valid docblock usage (#7401) #7402

merged 12 commits into from Dec 15, 2023

Conversation

f1amy
Copy link
Contributor

@f1amy f1amy commented Nov 1, 2023

Fixes #7401
Supersedes #5109

@f1amy f1amy changed the title Fix "Fixer breaks PhpDoc before return" #7401 fix(PhpdocToCommentFixer): Handle return as valid language construct (#7401) Nov 1, 2023
@coveralls
Copy link

coveralls commented Nov 1, 2023

Coverage Status

coverage: 94.785% (-0.004%) from 94.789%
when pulling 6f4ca1b on f1amy:fix-phpdoc_to_comment-before-return
into 07cfaab on PHP-CS-Fixer:master.

@kubawerlos kubawerlos requested review from keradus and removed request for kubawerlos November 9, 2023 15:16
Wirone
Wirone previously requested changes Nov 11, 2023
Copy link
Member

@Wirone Wirone left a comment

Choose a reason for hiding this comment

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

@f1amy we had some conversation between maintainers, and here's the summary: we would like to introduce new config option for this fixer, e.g. allow_before_return_statement which should be set to false by default, to keep current behaviour intact, as we consider this as a feature request, not a bug fix. In Fixer v4 we will make it true by default (so you can leave such a comment there), because PHP's ecosystem supports it (like PHPStan, PHPStorm). Please introduce the config option, regenerate docs and provide test cases for both config values.

@Wirone
Copy link
Member

Wirone commented Nov 11, 2023

@keradus @kubawerlos FYI it was already proposed in the past (#5109), so we need to close one, but which one? This is most recent and up-to-date, but @bendavies' contribution would be rejected unfairly IMHO 🤷.

@f1amy f1amy changed the title fix(PhpdocToCommentFixer): Handle return as valid language construct (#7401) feature(PhpdocToCommentFixer): Add option to handle return as valid docblock usage (#7401) Nov 12, 2023
@f1amy f1amy changed the title feature(PhpdocToCommentFixer): Add option to handle return as valid docblock usage (#7401) feat(PhpdocToCommentFixer): Add option to handle return as valid docblock usage (#7401) Nov 12, 2023
@f1amy f1amy requested a review from Wirone November 12, 2023 13:54
@f1amy
Copy link
Contributor Author

f1amy commented Nov 12, 2023

  • Added option allow_before_return_statement and converted this PR into a feature.
  • Added TODO to make option true by default in 4.0

@keradus
Copy link
Member

keradus commented Nov 13, 2023

@Wirone - We merge one that is ready, when it's ready, and we close the other one.

@f1amy f1amy requested a review from keradus November 13, 2023 17:55
@f1amy
Copy link
Contributor Author

f1amy commented Nov 20, 2023

Rebased latest changes, list.rst file removed as in master.

@f1amy
Copy link
Contributor Author

f1amy commented Dec 1, 2023

I'm not sure if static code analysis fail is caused by this PR's code, should I just wait for new commits from master?

@Wirone
Copy link
Member

Wirone commented Dec 1, 2023

@f1amy The error is not related to your changes, but I can't reproduce it locally, though 🤔. Created PR with newest PHPStan, but error remains. Will investigate it later.

@mvorisek
Copy link
Contributor

mvorisek commented Dec 9, 2023

I agree with #5109 (comment), such @var should be replaced with phpstan ignore. Once the ignore is no longer needed, phpstan will warn about it, and it can be removed. @var introduces only more LOC that cannot be checked.

@f1amy
Copy link
Contributor Author

f1amy commented Dec 9, 2023

I agree with #5109 (comment), such @var should be replaced with phpstan ignore. Once the ignore is no longer needed, phpstan will warn about it, and it can be removed. @var introduces only more LOC that cannot be checked.

I agree that we should not need to correct types with @var, and phpstan ignore could be used instead. However in order to do that, we still need to add a phpdoc with @phpstan-ignore error.name above return for it to work.

@f1amy
Copy link
Contributor Author

f1amy commented Dec 14, 2023

@Wirone could you please check the status of this PR?

It says that 1 change is requested, but I thought I fixed it already, is it just waiting for review?

@Wirone Wirone dismissed their stale review December 14, 2023 16:13

Requires new review after changes.

@@ -65,7 +65,7 @@ public function isHeaderComment(Tokens $tokens, int $index): bool
*
* @see https://github.com/php-fig/fig-standards/blob/master/proposed/phpdoc.md#3-definitions
*/
public function isBeforeStructuralElement(Tokens $tokens, int $index): bool
public function isBeforeStructuralElement(Tokens $tokens, int $index, bool $allowReturnStatement = false): bool
Copy link
Contributor

Choose a reason for hiding this comment

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

From here, the new parameter does not make sense - isBeforeStructuralElement is clear, parameter allowReturnStatement allows cheating. Let's not do that and limit allow_before_return_statement functionality to only PhpdocToCommentFixer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Extracted into isBeforeReturn method

src/Fixer/Phpdoc/PhpdocToCommentFixer.php Show resolved Hide resolved
@f1amy
Copy link
Contributor Author

f1amy commented Dec 14, 2023

@kubawerlos ready for review again!

@@ -102,6 +119,8 @@ public function configure(array $configuration = null): void
static fn (string $tag): string => strtolower($tag),
$this->configuration['ignored_tags']
);

$this->allowBeforeReturnStatement = (bool) $this->configuration['allow_before_return_statement'];
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
$this->allowBeforeReturnStatement = (bool) $this->configuration['allow_before_return_statement'];
$this->allowBeforeReturnStatement = true === $this->configuration['allow_before_return_statement'];

We have no casting for configuration in other fixers, let's use comparing to true, or even true === $this->configuration['allow_before_return_statement'] directly in line 153.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed

return false;
}

return $this->isValidReturnStatement($tokens, $nextIndex);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
return $this->isValidReturnStatement($tokens, $nextIndex);
return $tokens[$nextIndex]->isGivenKind(T_RETURN);

No need for a separate function for such a simple check.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. fixed

@kubawerlos kubawerlos enabled auto-merge (squash) December 15, 2023 15:58
@kubawerlos kubawerlos merged commit dbb7816 into PHP-CS-Fixer:master Dec 15, 2023
26 checks passed
@kubawerlos
Copy link
Contributor

Thank you @f1amy 👍🏼

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.

Fixer breaks PhpDoc before return
6 participants