Passed
Pull Request — master (#16)
by Pol
13:11
created

AfterExpressionAnalysisProvider::getNextClosure()

Size

Total Lines 14
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 14

1 Method

Rating   Name   Duplication   Size   Complexity  
A AfterExpressionAnalysisProvider.php$0 ➔ enterNode() 0 2 2
1
<?php
2
3
/**
4
 * For the full copyright and license information, please view
5
 * the LICENSE file that was distributed with this source code.
6
 */
7
8
declare(strict_types=1);
9
10
namespace loophp\fpt\Psalm\EventHandler\ComposeSimple;
11
12
use PhpParser\Node;
13
use PhpParser\Node\FunctionLike;
14
use PhpParser\NodeTraverser;
15
use PhpParser\NodeVisitorAbstract;
16
use Psalm\Internal\Analyzer\MethodAnalyzer;
17
use Psalm\Plugin\EventHandler\AfterFunctionLikeAnalysisInterface;
18
use Psalm\Plugin\EventHandler\Event\AfterFunctionLikeAnalysisEvent;
19
20
final class AfterExpressionAnalysisProvider implements AfterFunctionLikeAnalysisInterface
21
{
22
23
    public static function afterStatementAnalysis(AfterFunctionLikeAnalysisEvent $event): ?bool {
24
        $stmtSource = $event->getStatementsSource();
25
26
        if (false === ($stmtSource instanceof MethodAnalyzer)) {
27
            return false;
28
        }
29
30
        if ('of' !== $stmtSource->getMethodName()) {
31
            return false;
32
        }
33
34
        $ast = $event->getStmt();
35
36
        if (false === ($ast instanceof FunctionLike)) {
37
            return false;
38
        }
39
40
        $closure = self::getNextClosure($ast);
41
42
        // This is the $fs param of ComposeSimple::of()(callable ...$fs);
43
        // ----------------------------------------------------------^
44
        $param = current($closure->getParams());
0 ignored issues
show
Unused Code introduced by
The assignment to $param is dead and can be removed.
Loading history...
45
46
        // Questions
47
        // 1. What to do from there?
48
49
        return true;
50
    }
51
52
    private static function getNextClosure($ast): Node\Expr\Closure
53
    {
54
        $traverser = new NodeTraverser();
55
56
        $traverser->addVisitor(new class extends NodeVisitorAbstract {
57
            public function enterNode(Node $node): ?Node\Stmt\Return_ {
58
                return $node instanceof Node\Stmt\Return_ ? $node : null;
59
            }
60
        });
61
62
        /** @var array<int, \PhpParser\Node\Stmt\Return_> $returnStatement */
63
        $returnStatement = $traverser->traverse($ast->getStmts());
64
65
        return current($returnStatement)->expr;
66
    }
67
}
68