1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Padawan\Parser; |
4
|
|
|
|
5
|
|
|
use Padawan\Domain\Project; |
6
|
|
|
use Padawan\Domain\Project\Node; |
7
|
|
|
use PhpParser\Node\Stmt\Class_; |
8
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
9
|
|
|
use PhpParser\Node\Stmt\Property; |
10
|
|
|
use PhpParser\Node\Stmt\PropertyProperty; |
11
|
|
|
use PhpParser\Node\Stmt\ClassConst; |
12
|
|
|
use PhpParser\Node\Name; |
13
|
|
|
|
14
|
|
|
class ClassParser |
15
|
|
|
{ |
16
|
|
|
private $docParser; |
17
|
|
|
private $methodsParser; |
18
|
|
|
private $propertiesParser; |
19
|
|
|
private $useParser; |
20
|
|
|
|
21
|
|
|
public function __construct( |
22
|
|
|
CommentParser $docParser, |
23
|
|
|
MethodParser $methodsParser, |
24
|
|
|
PropertyParser $propertiesParser, |
25
|
|
|
UseParser $useParser |
26
|
|
|
) |
27
|
|
|
{ |
28
|
|
|
$this->docParser = $docParser; |
29
|
|
|
$this->methodsParser = $methodsParser; |
30
|
|
|
$this->propertiesParser = $propertiesParser; |
31
|
|
|
$this->useParser = $useParser; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function parse(Class_ $node, Project\FQN $fqn, $file) { |
35
|
|
|
$fqcn = new Project\FQCN($node->name, $fqn); |
36
|
|
|
$classData = new Node\ClassData($fqcn, $file); |
37
|
|
|
if ($node->extends instanceof Name) { |
38
|
|
|
$classData->setParent( |
39
|
|
|
$this->useParser->getFQCN($node->extends) |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
foreach ($node->implements AS $interfaceName) { |
43
|
|
|
$classData->addInterface( |
44
|
|
|
$this->useParser->getFQCN($interfaceName) |
45
|
|
|
); |
46
|
|
|
} |
47
|
|
|
$classData->startLine = $node->getAttribute("startLine"); |
48
|
|
|
$this->parseDocComments( |
49
|
|
|
$classData, |
50
|
|
|
$node->getAttribute("comments") |
51
|
|
|
); |
52
|
|
|
foreach ($node->stmts AS $child) { |
53
|
|
|
if ($child instanceof ClassMethod) { |
54
|
|
|
$classData->addMethod($this->parseMethod($child)); |
55
|
|
|
} |
56
|
|
|
elseif ($child instanceof Property) { |
57
|
|
|
foreach ($child->props AS $prop) { |
58
|
|
|
$classData->addProp( |
59
|
|
|
$this->parseProperty($prop, $child->type, $child->getAttribute('comments')) |
60
|
|
|
); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
elseif ($child instanceof ClassConst) { |
64
|
|
|
foreach ($child->consts AS $const) { |
65
|
|
|
$classData->addConst($const->name); |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
return $classData; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Parses Method node though $methodsParser |
74
|
|
|
* |
75
|
|
|
* @return Node\MethodData |
76
|
|
|
*/ |
77
|
|
|
protected function parseMethod(ClassMethod $node) |
78
|
|
|
{ |
79
|
|
|
return $this->methodsParser->parse($node); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Parses Property node through $propertiesParser |
84
|
|
|
* |
85
|
|
|
* @return Node\ClassProperty |
86
|
|
|
*/ |
87
|
|
|
protected function parseProperty(PropertyProperty $node, $modifier, $comments) |
88
|
|
|
{ |
89
|
|
|
return $this->propertiesParser->parse($node, $modifier, $comments); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Parses doc comments trough $docParser |
95
|
|
|
* |
96
|
|
|
* @param Node\ClassData $classData |
97
|
|
|
*/ |
98
|
|
|
protected function parseDocComments($classData, $node) |
99
|
|
|
{ |
100
|
|
|
/** @var Node\Comment $comment */ |
101
|
|
|
$comment = $this->docParser->parse($node); |
102
|
|
|
$classData->doc = $comment->getDoc(); |
103
|
|
|
foreach ($comment->getProperties() as $prop) { |
104
|
|
|
$classData->addProp($prop); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|