Completed
Pull Request — master (#80)
by
unknown
03:32 queued 01:07
created

InlineTypeHintParser   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 60
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
D parse() 0 34 9
1
<?php
2
3
namespace Padawan\Parser;
4
5
use Padawan\Domain\Project\Node\TypeHint;
6
use Padawan\Domain\Project\Node\Comment;
7
use PhpParser\Node\Expr\Assign;
8
use PhpParser\Node\Expr\Closure;
9
use PhpParser\Node\Stmt\Foreach_;
10
use PhpParser\Node\Expr\FuncCall;
11
use Psr\Log\LoggerInterface;
12
13
class InlineTypeHintParser {
14
15
    /**
16
     * Constructs
17
     *
18
     */
19
    public function __construct(
20
        LoggerInterface $logger,
21
        CommentParser $commentParser
22
    )
23
    {
24
        $this->logger        = $logger;
25
        $this->commentParser = $commentParser;
26
    }
27
28
    /**
29
     * Parses inline type hint
30
     *
31
     * @return TypeHint[]
32
     */
33
    public function parse($node)
34
    {
35
        $result = [];
36
37
        if (empty($node->stmts)) {
38
            return $result;
39
        }
40
        foreach ($node->stmts AS $stmt) {
41
            if (!empty($stmt->stmts)) {
42
                $result = array_merge($result, $this->parse($stmt));
43
            }
44
            $comments = $stmt->getAttribute('comments');
45
            if (empty($comments)) {
46
                continue;
47
            }
48
            foreach ($comments as $comment) {
49
                $text = trim($comment->getText());
50
                if (!empty($text)) {
51
                    if (strpos($text, '/** @var ') !== 0) {
52
                        // only parse inline type hint
53
                        continue;
54
                    }
55
                    $comment = $this->commentParser->parse($text);
56
                    foreach ($comment->getVars() as $variable) {
57
                        $result[] = TypeHint::create(
58
                            $variable, $stmt->getAttribute('startLine')
59
                        );
60
                    }
61
                }
62
            }
63
        }
64
65
        return $result;
66
    }
67
68
    /** @property CommentParser $commentParser */
69
    private $commentParser;
70
    /** @property LoggerInterface */
71
    private $logger;
72
}
73