|
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
|
|
|
|