1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpUnitGen\Parser\NodeParser; |
4
|
|
|
|
5
|
|
|
use PhpParser\Node; |
6
|
|
|
use PhpUnitGen\Annotation\GetterAnnotation; |
7
|
|
|
use PhpUnitGen\Annotation\SetterAnnotation; |
8
|
|
|
use PhpUnitGen\Model\FunctionModel; |
9
|
|
|
use PhpUnitGen\Model\ModelInterface\InterfaceModelInterface; |
10
|
|
|
use PhpUnitGen\Parser\NodeParserUtil\MethodVisibilityHelper; |
11
|
|
|
use Respect\Validation\Validator; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class MethodNodeParser. |
15
|
|
|
* |
16
|
|
|
* @author Paul Thébaud <[email protected]>. |
17
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
18
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
19
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
20
|
|
|
* @since Class available since Release 2.0.0. |
21
|
|
|
*/ |
22
|
|
|
class MethodNodeParser extends AbstractFunctionNodeParser |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* Parse a node to update the parent node model. |
26
|
|
|
* |
27
|
|
|
* @param Node\Stmt\ClassMethod $node The node to parse. |
28
|
|
|
* @param InterfaceModelInterface $parent The parent node. |
29
|
|
|
* |
30
|
|
|
* @return InterfaceModelInterface The updated parent. |
31
|
|
|
*/ |
32
|
|
|
public function invoke(Node\Stmt\ClassMethod $node, InterfaceModelInterface $parent): InterfaceModelInterface |
33
|
|
|
{ |
34
|
|
|
$function = new FunctionModel(); |
35
|
|
|
$function->setParentNode($parent); |
36
|
|
|
$function->setName($node->name); |
37
|
|
|
$function->setIsFinal($node->isFinal()); |
38
|
|
|
$function->setIsStatic($node->isStatic()); |
39
|
|
|
$function->setIsAbstract($node->isAbstract()); |
40
|
|
|
$function->setVisibility(MethodVisibilityHelper::getMethodVisibility($node)); |
41
|
|
|
|
42
|
|
|
$function = $this->parseFunction($node, $function); |
43
|
|
|
|
44
|
|
|
$parent->addFunction($function); |
45
|
|
|
|
46
|
|
|
if ($this->config->hasAuto()) { |
47
|
|
|
if ($this->getter($function)) { |
48
|
|
|
return $parent; |
49
|
|
|
} |
50
|
|
|
if ($this->setter($function)) { |
51
|
|
|
return $parent; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
if (($documentation = $node->getDocComment()) !== null) { |
55
|
|
|
$this->documentationNodeParser->invoke($documentation, $function); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $parent; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function getter(FunctionModel $function): bool |
62
|
|
|
{ |
63
|
|
|
// Check if function name matches |
64
|
|
|
preg_match('/^get(.+)$/', $function->getName(), $matches); |
65
|
|
|
|
66
|
|
|
if (Validator::arrayType()->length(2, 2)->validate($matches)) { |
67
|
|
|
// Check if property exists |
68
|
|
|
$property = lcfirst($matches[1]); |
69
|
|
|
if ($function->getParentNode()->hasAttribute($property, $function->isStatic())) { |
|
|
|
|
70
|
|
|
$annotation = new GetterAnnotation(); |
71
|
|
|
$annotation->setName('@PhpUnitGen\\getter'); |
72
|
|
|
$annotation->compile(); |
73
|
|
|
$function->addAnnotation($annotation); |
74
|
|
|
|
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return false; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
private function setter(FunctionModel $function): bool |
83
|
|
|
{ |
84
|
|
|
// Check if function name matches |
85
|
|
|
preg_match('/^set(.+)$/', $function->getName(), $matches); |
86
|
|
|
|
87
|
|
|
if (Validator::arrayType()->length(2, 2)->validate($matches)) { |
88
|
|
|
// Check if property exists |
89
|
|
|
$property = lcfirst($matches[1]); |
90
|
|
|
if ($function->getParentNode()->hasAttribute($property, $function->isStatic())) { |
91
|
|
|
$annotation = new SetterAnnotation(); |
92
|
|
|
$annotation->setName('@PhpUnitGen\\setter'); |
93
|
|
|
$annotation->compile(); |
94
|
|
|
$function->addAnnotation($annotation); |
95
|
|
|
|
96
|
|
|
return true; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return false; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|