33namespace ShipMonk \PHPStan \Visitor ;
44
55use PhpParser \Node ;
6- use PhpParser \Node \Arg ;
7- use PhpParser \Node \ArrayItem ;
8- use PhpParser \Node \Expr \ArrowFunction ;
9- use PhpParser \Node \Expr \Assign ;
10- use PhpParser \Node \Expr \AssignOp ;
11- use PhpParser \Node \Expr \BinaryOp \Coalesce ;
126use PhpParser \Node \Expr \Match_ ;
13- use PhpParser \Node \Expr \MethodCall ;
14- use PhpParser \Node \Expr \NullsafeMethodCall ;
15- use PhpParser \Node \Expr \Ternary ;
16- use PhpParser \Node \Expr \Throw_ ;
17- use PhpParser \Node \Expr \Yield_ ;
18- use PhpParser \Node \Expr \YieldFrom ;
19- use PhpParser \Node \MatchArm ;
20- use PhpParser \Node \Stmt \Return_ ;
7+ use PhpParser \Node \Stmt \Expression ;
218use PhpParser \NodeVisitorAbstract ;
229use function array_pop ;
2310use function end ;
@@ -44,18 +31,11 @@ public function beforeTraverse(array $nodes): ?array
4431
4532 public function enterNode (Node $ node ): ?Node
4633 {
47- if ($ this ->stack !== []) {
48- $ parent = end ($ this ->stack );
49-
50- if ($ node instanceof Match_ && $ this ->isUsed ($ parent )) {
51- $ node ->setAttribute (self ::MATCH_RESULT_USED , true );
52- }
53- }
54-
55- if ($ this ->shouldBuildStack ($ node )) {
56- $ this ->stack [] = $ node ;
34+ if ($ node instanceof Match_ && !$ this ->hasUnusedResult ()) {
35+ $ node ->setAttribute (self ::MATCH_RESULT_USED , true );
5736 }
5837
38+ $ this ->stack [] = $ node ;
5939 return null ;
6040 }
6141
@@ -65,30 +45,10 @@ public function leaveNode(Node $node): ?Node
6545 return null ;
6646 }
6747
68- private function shouldBuildStack (Node $ node ): bool
69- {
70- return $ this ->stack !== [] || $ this ->isUsed ($ node );
71- }
72-
73- /**
74- * Those parent nodes are marking the match as used
75- */
76- private function isUsed (Node $ parent ): bool
48+ private function hasUnusedResult (): bool
7749 {
78- return $ parent instanceof Throw_
79- || $ parent instanceof Assign
80- || $ parent instanceof AssignOp
81- || $ parent instanceof MethodCall
82- || $ parent instanceof Return_
83- || $ parent instanceof Arg
84- || $ parent instanceof Coalesce
85- || $ parent instanceof ArrayItem
86- || $ parent instanceof NullsafeMethodCall
87- || $ parent instanceof Ternary
88- || $ parent instanceof MatchArm
89- || $ parent instanceof Yield_
90- || $ parent instanceof YieldFrom
91- || $ parent instanceof ArrowFunction;
50+ $ parent = end ($ this ->stack );
51+ return $ parent === false || $ parent instanceof Expression;
9252 }
9353
9454}
0 commit comments