55use PhpParser \Node ;
66use PHPStan \Analyser \NullsafeOperatorHelper ;
77use PHPStan \Analyser \Scope ;
8+ use PHPStan \Node \NoopExpressionNode ;
89use PHPStan \Rules \Rule ;
910use PHPStan \Rules \RuleErrorBuilder ;
1011use PHPStan \Rules \RuleLevelHelper ;
1415use function sprintf ;
1516
1617/**
17- * @implements Rule<Node\Stmt\Expression >
18+ * @implements Rule<NoopExpressionNode >
1819 */
1920final 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 [];
0 commit comments