Skip to content

Commit 126684a

Browse files
Rewrite rule
1 parent a08ba64 commit 126684a

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

src/Rules/Methods/CallToMethodStatementWithoutSideEffectsRule.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use PhpParser\Node;
66
use PHPStan\Analyser\NullsafeOperatorHelper;
77
use PHPStan\Analyser\Scope;
8+
use PHPStan\Node\NoopExpressionNode;
89
use PHPStan\Rules\Rule;
910
use PHPStan\Rules\RuleErrorBuilder;
1011
use PHPStan\Rules\RuleLevelHelper;
@@ -14,7 +15,7 @@
1415
use function sprintf;
1516

1617
/**
17-
* @implements Rule<Node\Stmt\Expression>
18+
* @implements Rule<NoopExpressionNode>
1819
*/
1920
final class CallToMethodStatementWithoutSideEffectsRule implements Rule
2021
{
@@ -25,18 +26,18 @@ public function __construct(private RuleLevelHelper $ruleLevelHelper)
2526

2627
public function getNodeType(): string
2728
{
28-
return Node\Stmt\Expression::class;
29+
return NoopExpressionNode::class;
2930
}
3031

3132
public function processNode(Node $node, Scope $scope): array
3233
{
33-
if ($node->expr instanceof Node\Expr\NullsafeMethodCall) {
34-
$scope = $scope->filterByTruthyValue(new Node\Expr\BinaryOp\NotIdentical($node->expr->var, new Node\Expr\ConstFetch(new Node\Name('null'))));
35-
} elseif (!$node->expr instanceof Node\Expr\MethodCall) {
34+
$methodCall = $node->getOriginalExpr();
35+
if ($methodCall instanceof Node\Expr\NullsafeMethodCall) {
36+
$scope = $scope->filterByTruthyValue(new Node\Expr\BinaryOp\NotIdentical($methodCall->var, new Node\Expr\ConstFetch(new Node\Name('null'))));
37+
} elseif (!$methodCall instanceof Node\Expr\MethodCall) {
3638
return [];
3739
}
3840

39-
$methodCall = $node->expr;
4041
if (!$methodCall->name instanceof Node\Identifier) {
4142
return [];
4243
}
@@ -61,8 +62,8 @@ public function processNode(Node $node, Scope $scope): array
6162
}
6263

6364
$method = $calledOnType->getMethod($methodName, $scope);
64-
if ($method->hasSideEffects()->no() || $node->expr->isFirstClassCallable()) {
65-
if (!$node->expr->isFirstClassCallable()) {
65+
if ($method->hasSideEffects()->no() || $methodCall->isFirstClassCallable()) {
66+
if (!$methodCall->isFirstClassCallable()) {
6667
$throwsType = $method->getThrowType();
6768
if ($throwsType !== null && !$throwsType->isVoid()->yes()) {
6869
return [];

tests/PHPStan/Rules/Methods/CallToMethodStatementWithoutSideEffectsRuleTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ protected function getRule(): Rule
2020
public function testRule(): void
2121
{
2222
$this->analyse([__DIR__ . '/data/method-call-statement-no-side-effects.php'], [
23+
[
24+
'Call to method DateTimeImmutable::modify() on a separate line has no effect.',
25+
15,
26+
],
2327
[
2428
'Call to static method DateTimeImmutable::createFromFormat() on a separate line has no effect.',
2529
16,

0 commit comments

Comments
 (0)