Skip to content

Commit 45a62ae

Browse files
Fix error reported on the wrong line (#4991)
1 parent ff98196 commit 45a62ae

27 files changed

+369
-35
lines changed

src/Rules/Methods/CallPrivateMethodThroughStaticRule.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ public function processNode(Node $node, Scope $scope): array
5757
'Unsafe call to private method %s::%s() through static::.',
5858
$method->getDeclaringClass()->getDisplayName(),
5959
$method->getName(),
60-
))->identifier('staticClassAccess.privateMethod')->build(),
60+
))
61+
->line($node->name->getStartLine())
62+
->identifier('staticClassAccess.privateMethod')
63+
->build(),
6164
];
6265
}
6366

src/Rules/Methods/CallStaticMethodsRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public function processNode(Node $node, Scope $scope): array
6565
*/
6666
private function processSingleMethodCall(Scope $scope, StaticCall $node, string $methodName): array
6767
{
68-
[$errors, $method] = $this->methodCallCheck->check($scope, $methodName, $node->class);
68+
[$errors, $method] = $this->methodCallCheck->check($scope, $methodName, $node->class, $node->name);
6969
if ($method === null) {
7070
return $errors;
7171
}

src/Rules/Methods/MethodCallCheck.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,10 @@ public function check(
7272
'Cannot call method %s() on %s.',
7373
$methodName,
7474
$typeForDescribe->describe(VerbosityLevel::typeOnly()),
75-
))->identifier('method.nonObject')->build(),
75+
))
76+
->line($astName->getStartLine())
77+
->identifier('method.nonObject')
78+
->build(),
7679
],
7780
null,
7881
];
@@ -106,7 +109,10 @@ public function check(
106109
'Call to private method %s() of parent class %s.',
107110
$methodReflection->getName(),
108111
$parentClassReflection->getDisplayName(),
109-
))->identifier('method.private')->build(),
112+
))
113+
->line($astName->getStartLine())
114+
->identifier('method.private')
115+
->build(),
110116
],
111117
$methodReflection,
112118
];
@@ -133,7 +139,10 @@ public function check(
133139
'Call to an undefined method %s::%s().',
134140
$typeForDescribe->describe(VerbosityLevel::typeOnly()),
135141
$methodName,
136-
))->identifier('method.notFound')->build(),
142+
))
143+
->line($astName->getStartLine())
144+
->identifier('method.notFound')
145+
->build(),
137146
],
138147
null,
139148
];
@@ -150,6 +159,7 @@ public function check(
150159
$methodReflection->getName(),
151160
$declaringClass->getDisplayName(),
152161
))
162+
->line($astName->getStartLine())
153163
->identifier(sprintf('method.%s', $methodReflection->isPrivate() ? 'private' : 'protected'))
154164
->build();
155165
}
@@ -161,7 +171,10 @@ public function check(
161171
) {
162172
$errors[] = RuleErrorBuilder::message(
163173
sprintf('Call to method %s with incorrect case: %s', $messagesMethodName, $methodName),
164-
)->identifier('method.nameCase')->build();
174+
)
175+
->line($astName->getStartLine())
176+
->identifier('method.nameCase')
177+
->build();
165178
}
166179

167180
return [$errors, $methodReflection];

src/Rules/Methods/NullsafeMethodCallRule.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public function processNode(Node $node, Scope $scope): array
3131

3232
return [
3333
RuleErrorBuilder::message(sprintf('Using nullsafe method call on non-nullable type %s. Use -> instead.', $calledOnType->describe(VerbosityLevel::typeOnly())))
34+
->line($node->name->getStartLine())
3435
->identifier('nullsafe.neverNull')
3536
->build(),
3637
];

src/Rules/Methods/StaticMethodCallCheck.php

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use DOMDocument;
66
use PhpParser\Node\Expr;
7+
use PhpParser\Node\Identifier;
78
use PhpParser\Node\Name;
89
use PHPStan\Analyser\NullsafeOperatorHelper;
910
use PHPStan\Analyser\Scope;
@@ -61,6 +62,7 @@ public function check(
6162
Scope $scope,
6263
string $methodName,
6364
$class,
65+
Identifier|Expr $astName,
6466
): array
6567
{
6668
$errors = [];
@@ -81,7 +83,9 @@ public function check(
8183
'Calling %s::%s() outside of class scope.',
8284
$className,
8385
$methodName,
84-
))->identifier(sprintf('outOfClass.%s', $lowercasedClassName))->build(),
86+
))
87+
->line($astName->getStartLine())
88+
->identifier(sprintf('outOfClass.%s', $lowercasedClassName))->build(),
8589
],
8690
null,
8791
];
@@ -95,7 +99,10 @@ public function check(
9599
'Calling %s::%s() outside of class scope.',
96100
$className,
97101
$methodName,
98-
))->identifier(sprintf('outOfClass.parent'))->build(),
102+
))
103+
->line($astName->getStartLine())
104+
->identifier(sprintf('outOfClass.parent'))
105+
->build(),
99106
],
100107
null,
101108
];
@@ -110,7 +117,10 @@ public function check(
110117
$scope->getFunctionName(),
111118
$methodName,
112119
$scope->getClassReflection()->getDisplayName(),
113-
))->identifier('class.noParent')->build(),
120+
))
121+
->line($astName->getStartLine())
122+
->identifier('class.noParent')
123+
->build(),
114124
],
115125
null,
116126
];
@@ -132,6 +142,7 @@ public function check(
132142
$methodName,
133143
$className,
134144
))
145+
->line($astName->getStartLine())
135146
->identifier('class.notFound');
136147

137148
if ($this->discoveringSymbolsTip) {
@@ -206,7 +217,10 @@ public function check(
206217
'Cannot call static method %s() on %s.',
207218
$methodName,
208219
$typeForDescribe->describe(VerbosityLevel::typeOnly()),
209-
))->identifier('staticMethod.nonObject')->build(),
220+
))
221+
->line($astName->getStartLine())
222+
->identifier('staticMethod.nonObject')
223+
->build(),
210224
]),
211225
null,
212226
];
@@ -232,7 +246,10 @@ public function check(
232246
'Call to an undefined static method %s::%s().',
233247
$typeForDescribe->describe(VerbosityLevel::typeOnly()),
234248
$methodName,
235-
))->identifier('staticMethod.notFound')->build(),
249+
))
250+
->line($astName->getStartLine())
251+
->identifier('staticMethod.notFound')
252+
->build(),
236253
]),
237254
null,
238255
];
@@ -265,7 +282,10 @@ public function check(
265282
'Static call to instance method %s::%s().',
266283
$method->getDeclaringClass()->getDisplayName(),
267284
$method->getName(),
268-
))->identifier('method.staticCall')->build(),
285+
))
286+
->line($astName->getStartLine())
287+
->identifier('method.staticCall')
288+
->build(),
269289
]),
270290
$method,
271291
];
@@ -281,6 +301,7 @@ public function check(
281301
$method->getName(),
282302
$method->getDeclaringClass()->getDisplayName(),
283303
))
304+
->line($astName->getStartLine())
284305
->identifier(sprintf('staticMethod.%s', $method->isPrivate() ? 'private' : 'protected'))
285306
->build(),
286307
]);
@@ -294,10 +315,12 @@ public function check(
294315
$method->isStatic() ? ' static' : '',
295316
$method->getDeclaringClass()->getDisplayName(),
296317
$method->getName(),
297-
))->identifier(sprintf(
298-
'%s.callToAbstract',
299-
$method->isStatic() ? 'staticMethod' : 'method',
300-
))->build(),
318+
))
319+
->line($astName->getStartLine())
320+
->identifier(sprintf(
321+
'%s.callToAbstract',
322+
$method->isStatic() ? 'staticMethod' : 'method',
323+
))->build(),
301324
],
302325
$method,
303326
];
@@ -317,7 +340,10 @@ public function check(
317340
'Call to %s with incorrect case: %s',
318341
$lowercasedMethodName,
319342
$methodName,
320-
))->identifier('staticMethod.nameCase')->build();
343+
))
344+
->line($astName->getStartLine())
345+
->identifier('staticMethod.nameCase')
346+
->build();
321347
}
322348

323349
return [$errors, $method];

src/Rules/Methods/StaticMethodCallableRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function processNode(Node $node, Scope $scope): array
4646

4747
$methodNameName = $methodName->toString();
4848

49-
[$errors, $methodReflection] = $this->methodCallCheck->check($scope, $methodNameName, $node->getClass());
49+
[$errors, $methodReflection] = $this->methodCallCheck->check($scope, $methodNameName, $node->getClass(), $node->getName());
5050
if ($methodReflection === null) {
5151
return $errors;
5252
}

src/Rules/Operators/PipeOperatorRule.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ public function processNode(Node $node, Scope $scope): array
5454
RuleErrorBuilder::message(sprintf(
5555
'Parameter #1%s of callable on the right side of pipe operator is passed by reference.',
5656
$parameter->getName() !== '' ? ' $' . $parameter->getName() : '',
57-
))->identifier('pipe.byRef')
57+
))
58+
->line($node->right->getStartLine())
59+
->identifier('pipe.byRef')
5860
->build(),
5961
];
6062
}

src/Rules/Properties/AccessPrivatePropertyThroughStaticRule.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ public function processNode(Node $node, Scope $scope): array
5656
'Unsafe access to private property %s::$%s through static::.',
5757
$property->getDeclaringClass()->getDisplayName(),
5858
$propertyName,
59-
))->identifier('staticClassAccess.privateProperty')->build(),
59+
))
60+
->line($node->name->getStartLine())
61+
->identifier('staticClassAccess.privateProperty')
62+
->build(),
6063
];
6164
}
6265

src/Rules/Properties/AccessPropertiesCheck.php

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public function check(PropertyFetch $node, Scope $scope, bool $write): array
7878
$originalNameType = $scope->getType($node->name);
7979
$className = $scope->getType($node->var)->describe(VerbosityLevel::typeOnly());
8080
$errors[] = RuleErrorBuilder::message(sprintf('Property name for %s must be a string, but %s was given.', $className, $originalNameType->describe(VerbosityLevel::precise())))
81+
->line($node->name->getStartLine())
8182
->identifier('property.nameNotString')
8283
->build();
8384
}
@@ -124,7 +125,10 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
124125
'Cannot access property $%s on %s.',
125126
$name,
126127
$typeForDescribe->describe(VerbosityLevel::typeOnly()),
127-
))->identifier('property.nonObject')->build(),
128+
))
129+
->line($node->name->getStartLine())
130+
->identifier('property.nonObject')
131+
->build(),
128132
];
129133
}
130134

@@ -183,7 +187,10 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
183187
'Access to private property $%s of parent class %s.',
184188
$name,
185189
$parentClassReflection->getDisplayName(),
186-
))->identifier('property.private')->build(),
190+
))
191+
->line($node->name->getStartLine())
192+
->identifier('property.private')
193+
->build(),
187194
];
188195
}
189196

@@ -208,7 +215,10 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
208215
'Non-static access to static property %s::$%s.',
209216
$type->getStaticProperty($name, $scope)->getDeclaringClass()->getDisplayName(),
210217
$name,
211-
))->identifier('staticProperty.nonStaticAccess')->build(),
218+
))
219+
->line($node->name->getStartLine())
220+
->identifier('staticProperty.nonStaticAccess')
221+
->build(),
212222
];
213223
}
214224

@@ -255,7 +265,10 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
255265
$propertyReflection->isPrivate() ? 'private' : 'protected',
256266
$type->describe(VerbosityLevel::typeOnly()),
257267
$name,
258-
))->identifier(sprintf('property.%s', $propertyReflection->isPrivate() ? 'private' : 'protected'))->build(),
268+
))
269+
->line($node->name->getStartLine())
270+
->identifier(sprintf('property.%s', $propertyReflection->isPrivate() ? 'private' : 'protected'))
271+
->build(),
259272
];
260273
}
261274

@@ -265,7 +278,10 @@ private function processSingleProperty(Scope $scope, PropertyFetch $node, string
265278
$propertyReflection->isPrivateSet() ? 'private(set)' : 'protected(set)',
266279
$type->describe(VerbosityLevel::typeOnly()),
267280
$name,
268-
))->identifier(sprintf('assign.property%s', $propertyReflection->isPrivateSet() ? 'PrivateSet' : 'ProtectedSet'))->build(),
281+
))
282+
->line($node->name->getStartLine())
283+
->identifier(sprintf('assign.property%s', $propertyReflection->isPrivateSet() ? 'PrivateSet' : 'ProtectedSet'))
284+
->build(),
269285
];
270286
}
271287

0 commit comments

Comments
 (0)