-
Notifications
You must be signed in to change notification settings - Fork 569
Fix error reported on the wrong line #4991
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
Merged
Merged
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a5cece4
Fix error reported on the wrong line
VincentLanglet c720728
Refactor
VincentLanglet 42bf0a3
Rework
VincentLanglet 6adc439
Fix
VincentLanglet c3da32a
Handle more cases
VincentLanglet 154a83e
More
VincentLanglet af0f984
Another strategy
VincentLanglet 13fdcd9
Fix
VincentLanglet 23963be
Revert for binary
VincentLanglet ebd0f63
More
VincentLanglet 37eeade
Fix
VincentLanglet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| use PhpParser\Node; | ||
| use PhpParser\Node\Expr; | ||
| use PHPStan\Analyser\MutatingScope; | ||
| use PHPStan\Analyser\RuleErrorTransformer; | ||
| use PHPStan\Analyser\Scope; | ||
| use PHPStan\DependencyInjection\AutowiredParameter; | ||
| use PHPStan\DependencyInjection\AutowiredService; | ||
|
|
@@ -89,11 +90,7 @@ public function check( | |
| string $namedArgumentMessage, | ||
| ): array | ||
| { | ||
| if ($funcCall instanceof Node\Expr\MethodCall || $funcCall instanceof Node\Expr\StaticCall || $funcCall instanceof Node\Expr\FuncCall) { | ||
| $funcCallLine = $funcCall->name->getStartLine(); | ||
| } else { | ||
| $funcCallLine = $funcCall->getStartLine(); | ||
| } | ||
| $funcCallLine = RuleErrorTransformer::getStartLineFromNode($funcCall); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we still need this line?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, because
|
||
|
|
||
| $functionParametersMinCount = 0; | ||
| $functionParametersMaxCount = 0; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| namespace Bug14150NullsafeMethod; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| public int $x = 5; | ||
|
|
||
| /** | ||
| * @return $this | ||
| */ | ||
| public function x() | ||
| { | ||
| return $this; | ||
| } | ||
|
|
||
| public function testUnknownMethod(): void | ||
| { | ||
| $this | ||
| ->x() | ||
| ?->y(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php | ||
|
|
||
| namespace Bug14150MethodStatic; | ||
|
|
||
| final class HelloWorld | ||
| { | ||
| public int $x = 5; | ||
|
|
||
| /** | ||
| * @return $this | ||
| */ | ||
| public static function x() | ||
| { | ||
| return new self(); | ||
| } | ||
|
|
||
| public function testUnknownMethod(): void | ||
| { | ||
| (new HelloWorld()) | ||
| ::x() | ||
| ::y(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug14150Method; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| public int $x = 5; | ||
|
|
||
| /** | ||
| * @return $this | ||
| */ | ||
| public function x() | ||
| { | ||
| return $this; | ||
| } | ||
|
|
||
| public function testUnknownMethod(): void | ||
| { | ||
| $this | ||
| ->x() | ||
| ->y(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
tests/PHPStan/Rules/Properties/data/bug-14150-nullsafe.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| namespace Bug14150NullsafeProperty; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| public int $x = 5; | ||
|
|
||
| /** | ||
| * @return $this | ||
| */ | ||
| public function x() | ||
| { | ||
| return $this; | ||
| } | ||
|
|
||
| public function test3(): void | ||
| { | ||
| $this | ||
| ?->x; | ||
| } | ||
|
|
||
| public function test4(): void | ||
| { | ||
| $this | ||
| ->x() | ||
| ?->x; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| <?php declare(strict_types = 1); | ||
|
|
||
| namespace Bug14150Properties; | ||
|
|
||
| class HelloWorld | ||
| { | ||
| public int $x = 5; | ||
|
|
||
| /** | ||
| * @return $this | ||
| */ | ||
| public function x() | ||
| { | ||
| return $this; | ||
| } | ||
|
|
||
| public function test3(): void | ||
| { | ||
| $this | ||
|
VincentLanglet marked this conversation as resolved.
|
||
| ->x = null; | ||
| } | ||
|
|
||
| public function test4(): void | ||
| { | ||
| $this | ||
| ->x() | ||
| ->x = null; | ||
|
VincentLanglet marked this conversation as resolved.
|
||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
I just had a look thru the codebase.
we have already
PhpDocLineHelper::detectLine()which is called likeline(PhpDocLineHelper::detectLine($node, $phpDocTag)).this makes me think we should not have this special fallback case here, but call something like
NodeChainLineHelper::detectLine($node)right at the ErrorBuilder call-sites where->line()is invoked.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.
Lot of existing rules are not calling
line()and rely on the defaut fallback which was already implemented in RuleErrortransformer$node->getStartLine().There is tons of rule to fix, so it would be easier to modify the fallback.
Even binary operator are wrong
https://phpstan.org/r/090b54c3-0255-4027-9934-491d8ab962b7
And yes PipeOperator has the same issue too https://phpstan.org/r/7ab46b15-18c9-4d5b-bfc9-27bc78efe3d4
But there is so much to fix, I feel like we should first:
I start to feel like we should fix rules one by one...
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.
agree - thanks for looking into it
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.
I tried another strategy with af0f984
I feel like it's safer to avoid a general strategy in RuleTransformer and instead to fix rules one by one. WDYT ?
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.
yes, thats what I had in mind with my comment