Skip to content

Fix phpstan/phpstan#14398: Incorrect line number for "Private method overriding public method should be public"#5381

Closed
phpstan-bot wants to merge 3 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-1td7nd1
Closed

Fix phpstan/phpstan#14398: Incorrect line number for "Private method overriding public method should be public"#5381
phpstan-bot wants to merge 3 commits intophpstan:2.1.xfrom
phpstan-bot:create-pull-request/patch-1td7nd1

Conversation

@phpstan-bot
Copy link
Copy Markdown
Collaborator

Summary

When a method has attributes (like #[Override]), the "Private method overriding public/protected method" error was reported on the first attribute line instead of the actual method declaration line. This fix ensures the error points to the correct line.

Changes

  • Added optional $line parameter to MethodVisibilityComparisonHelper::compare() in src/Rules/Methods/MethodVisibilityComparisonHelper.php
  • Updated OverridingMethodRule::processNode() in src/Rules/Methods/OverridingMethodRule.php to pass $node->getOriginalNode()->name->getStartLine() as the line number
  • Updated ConsistentConstructorRule::processNode() in src/Rules/Methods/ConsistentConstructorRule.php similarly
  • Added regression test in tests/PHPStan/Rules/Methods/data/bug-14398.php
  • Added test method testBug14398() in tests/PHPStan/Rules/Methods/OverridingMethodRuleTest.php

Root cause

InClassMethodNode inherits its start line from the underlying ClassMethod AST node, which includes attribute lines. When rules return errors without an explicit .line() call, RuleErrorTransformer uses $node->getStartLine() as the default, which points to the first attribute rather than the function keyword. The fix explicitly sets the line on visibility errors to the method name's start line.

Test

The regression test defines a class with a #[Override] attribute on a private method that overrides a protected parent method. It verifies the error is reported on line 17 (the private function declaration) rather than line 16 (the #[Override] attribute).

Fixes phpstan/phpstan#14398

…y errors

- Added optional $line parameter to MethodVisibilityComparisonHelper::compare()
- Pass method name start line from OverridingMethodRule and ConsistentConstructorRule
- This ensures visibility errors point to the function declaration line, not the first attribute line
- New regression test in tests/PHPStan/Rules/Methods/data/bug-14398.php
Copy link
Copy Markdown
Contributor

@VincentLanglet VincentLanglet left a comment

Choose a reason for hiding this comment

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

Just add a non optional Node argument directly to the method.

…tead of optional line number

Address review feedback: replace optional `?int $line = null` parameter with
a required `Node $node` parameter, calling `$node->getStartLine()` inside the
helper. This simplifies callers and makes the API more explicit.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@phpstan-bot
Copy link
Copy Markdown
Collaborator Author

Full test suite passed as well. All changes are committed and pushed. Both make phpstan and make tests pass cleanly.

Copy link
Copy Markdown
Contributor

@VincentLanglet VincentLanglet left a comment

Choose a reason for hiding this comment

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

Check for other rule related to InClassMethodNode::class;

Instead of fixing individual rules, adjust InClassMethodNode to use the
method name's start line when attributes shift the node's start line.
This fixes incorrect error line numbers for ALL rules processing
InClassMethodNode when methods have attributes (e.g. #[Override]).

The range check ensures trait-aliased methods (where the name identifier
points to the alias location) keep their original line number.

Also reverts MethodVisibilityComparisonHelper to its simpler signature
since the line fix is now handled at the node level.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@phpstan-bot
Copy link
Copy Markdown
Collaborator Author

The background make phpstan also completed successfully (exit 0). Everything is pushed.

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