1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Padawan\Parser; |
4
|
|
|
|
5
|
|
|
use Padawan\Domain\Project\Node\MethodData; |
6
|
|
|
use Padawan\Domain\Project\Node\MethodParam; |
7
|
|
|
use Padawan\Domain\Project\Node\Comment; |
8
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
9
|
|
|
use PhpParser\Node\Param; |
10
|
|
|
use PhpParser\Node\Name; |
11
|
|
|
|
12
|
|
View Code Duplication |
class MethodParser { |
|
|
|
|
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Constructs |
16
|
|
|
* |
17
|
|
|
* @param UseParser $useParser |
18
|
|
|
*/ |
19
|
|
|
public function __construct( |
20
|
|
|
UseParser $useParser, |
21
|
|
|
CommentParser $commentParser, |
22
|
|
|
ParamParser $paramParser |
23
|
|
|
) |
24
|
|
|
{ |
25
|
|
|
$this->useParser = $useParser; |
26
|
|
|
$this->commentParser = $commentParser; |
27
|
|
|
$this->paramParser = $paramParser; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Parses ClassMethod node to MethodData |
32
|
|
|
* |
33
|
|
|
* @return MethodData |
34
|
|
|
*/ |
35
|
|
|
public function parse(ClassMethod $node) |
36
|
|
|
{ |
37
|
|
|
$method = new MethodData($node->name); |
38
|
|
|
$method->startLine = $node->getAttribute("startLine"); |
39
|
|
|
$method->endLine = $node->getAttribute("endLine"); |
40
|
|
|
$method->setType($node->type); |
41
|
|
|
$comments = $node->getAttribute("comments"); |
42
|
|
|
if (is_array($comments)) { |
43
|
|
|
/** @var Comment */ |
44
|
|
|
$comment = $this->commentParser->parse( |
45
|
|
|
$comments[count($comments) - 1]->getText() |
46
|
|
|
); |
47
|
|
|
if ($comment->isInheritDoc()) { |
48
|
|
|
$method->doc = Comment::INHERIT_MARK; |
49
|
|
|
} else { |
50
|
|
|
$method->doc = $comment->getDoc(); |
51
|
|
|
$method->return = $comment->getReturn(); |
52
|
|
|
foreach ($comment->getVars() as $var) { |
53
|
|
|
if ($var instanceof MethodParam) { |
54
|
|
|
$method->addParam($var); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
foreach ($node->params AS $child) { |
60
|
|
|
if ($child instanceof Param) { |
61
|
|
|
$method->addParam($this->parseMethodArgument($child)); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
return $method; |
65
|
|
|
} |
66
|
|
|
protected function parseMethodArgument(Param $node) { |
67
|
|
|
return $this->paramParser->parse($node); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** @var UseParser $useParser */ |
71
|
|
|
private $useParser; |
72
|
|
|
/** @property CommentParser $commentParser */ |
73
|
|
|
private $commentParser; |
74
|
|
|
/** @var ParamParser */ |
75
|
|
|
private $paramParser; |
76
|
|
|
} |
77
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.