Skip to content

Commit a5cece4

Browse files
Fix error reported on the wrong line
1 parent bae39a9 commit a5cece4

File tree

5 files changed

+100
-1
lines changed

5 files changed

+100
-1
lines changed

src/Analyser/RuleErrorTransformer.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPStan\Fixable\PhpPrinter;
1515
use PHPStan\Fixable\PhpPrinterIndentationDetectorVisitor;
1616
use PHPStan\Fixable\ReplacingNodeVisitor;
17+
use PHPStan\Node\PropertyAssignNode;
1718
use PHPStan\Node\VirtualNode;
1819
use PHPStan\Rules\FileRuleError;
1920
use PHPStan\Rules\FixableNodeRuleError;
@@ -55,7 +56,6 @@ public function transform(
5556
Node $node,
5657
): Error
5758
{
58-
$line = $node->getStartLine();
5959
$canBeIgnored = true;
6060
$fileName = $scope->getFileDescription();
6161
$filePath = $scope->getFile();
@@ -75,6 +75,8 @@ public function transform(
7575
&& $ruleError->getLine() !== -1
7676
) {
7777
$line = $ruleError->getLine();
78+
} else {
79+
$line = $this->getLineFromNode($node);
7880
}
7981
if (
8082
$ruleError instanceof FileRuleError
@@ -166,4 +168,20 @@ public function transform(
166168
);
167169
}
168170

171+
private function getLineFromNode(Node $node): int
172+
{
173+
if ($node instanceof PropertyAssignNode) {
174+
return $this->getLineFromNode($node->getPropertyFetch());
175+
}
176+
177+
if (
178+
$node instanceof Node\Expr\PropertyFetch
179+
|| $node instanceof Node\Expr\MethodCall
180+
) {
181+
return $node->name->getStartLine();
182+
}
183+
184+
return $node->getStartLine();
185+
}
186+
169187
}

tests/PHPStan/Rules/Methods/CallMethodsRuleTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3844,6 +3844,19 @@ public function testBug12875(): void
38443844
$this->analyse([__DIR__ . '/data/bug-12875.php'], []);
38453845
}
38463846

3847+
public function testBug14150(): void
3848+
{
3849+
$this->checkThisOnly = false;
3850+
$this->checkNullables = true;
3851+
$this->checkUnionTypes = true;
3852+
$this->analyse([__DIR__ . '/data/bug-14150.php'], [
3853+
[
3854+
'Call to an undefined method Bug14150Method\HelloWorld::y().',
3855+
21,
3856+
],
3857+
]);
3858+
}
3859+
38473860
#[RequiresPhp('>= 8.1')]
38483861
public function testBug13805(): void
38493862
{
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug14150Method;
4+
5+
class HelloWorld
6+
{
7+
public int $x = 5;
8+
9+
/**
10+
* @return $this
11+
*/
12+
public function x(): static
13+
{
14+
return $this;
15+
}
16+
17+
public function testUnknownMethod(): void
18+
{
19+
$this
20+
->x()
21+
->y();
22+
}
23+
}

tests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -986,6 +986,22 @@ public function testBug13654(): void
986986
$this->analyse([__DIR__ . '/data/bug-13654.php'], []);
987987
}
988988

989+
public function testBug14150(): void
990+
{
991+
$this->checkExplicitMixed = true;
992+
$this->checkImplicitMixed = true;
993+
$this->analyse([__DIR__ . '/data/bug-14150.php'], [
994+
[
995+
'Property Bug14150Properties\HelloWorld::$x (int) does not accept null.',
996+
20,
997+
],
998+
[
999+
'Property Bug14150Properties\HelloWorld::$x (int) does not accept null.',
1000+
27,
1001+
],
1002+
]);
1003+
}
1004+
9891005
#[RequiresPhp('>= 8.5')]
9901006
public function testCloneWith(): void
9911007
{
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php declare(strict_types = 1);
2+
3+
namespace Bug14150Properties;
4+
5+
class HelloWorld
6+
{
7+
public int $x = 5;
8+
9+
/**
10+
* @return $this
11+
*/
12+
public function x(): static
13+
{
14+
return $this;
15+
}
16+
17+
public function test3(): void
18+
{
19+
$this
20+
->x = null;
21+
}
22+
23+
public function test4(): void
24+
{
25+
$this
26+
->x()
27+
->x = null;
28+
}
29+
}

0 commit comments

Comments
 (0)