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