Skip to content

Point method visibility errors at the method, not its first attribute - #6630

Merged
staabm merged 1 commit into
phpstan:2.2.xfrom
Cayan:fix-method-visibility-error-line
Sep 30, 2026
Merged

staabm merged 1 commit into
phpstan:2.2.xfrom
Cayan:fix-method-visibility-error-line

Conversation

@Cayan

@Cayan Cayan commented Sep 30, 2026

Copy link
Copy Markdown

Fixes phpstan/phpstan#14398

A method's start line includes its attribute groups, so "overriding public method should also be public" and "should be protected or public" were reported on the first attribute line (#[\Override] in the issue) instead of the method. MethodVisibilityComparisonHelper::compare() now takes the ClassMethod and reports both errors on the line of the method name, the same ->line($node->name->getStartLine()) other rules use for names. Its two callers, OverridingMethodRule and ConsistentConstructorRule, pass $node->getOriginalNode().

Without the change, the new tests get the attribute lines (39, 47, 54 and 24) instead of the method lines.

Other errors from OverridingMethodRule (final method, #[\Override], parameter and return types) still point at the attribute line. I kept this PR to the reported error and can move the others in a follow-up if you want them on the method line too.

- A method's start line includes its attribute groups, so "should also be public" and "should
  be protected or public" landed on the first attribute line. Both errors now use the line of
  the method name.
- MethodVisibilityComparisonHelper::compare() takes the ClassMethod node to read that line;
  OverridingMethodRule and ConsistentConstructorRule pass it.
- Tests cover an attribute, a docblock with a multi-line attribute, two attribute groups and a
  method without attributes, plus a consistent constructor with an attribute.

@staabm staabm left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm. maybe you are interessted in working on a similar problem in a new PR reported in phpstan/phpstan#10714

this PR merges cleanly into 2.3.x so its fine for 2.2.x.
please target future PRs on 2.3.x

thank you

@staabm

staabm commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

//cc @SanderMuller

@staabm
staabm merged commit 0ff0c25 into phpstan:2.2.x Sep 30, 2026
863 of 875 checks passed
@staabm

staabm commented Sep 30, 2026

Copy link
Copy Markdown
Contributor

Thank you

@Cayan

Cayan commented Sep 30, 2026

Copy link
Copy Markdown
Author

lgtm. maybe you are interessted in working on a similar problem in a new PR reported in phpstan/phpstan#10714

this PR merges cleanly into 2.3.x so its fine for 2.2.x. please target future PRs on 2.3.x

thank you

Thank you. I'll check it out

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.

3 participants