1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PhpUnitGen\Model; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\Common\Collections\Collection; |
7
|
|
|
use PhpUnitGen\Annotation\AnnotationInterface\AnnotationInterface; |
8
|
|
|
use PhpUnitGen\Annotation\AssertionAnnotation; |
9
|
|
|
use PhpUnitGen\Annotation\GetterAnnotation; |
10
|
|
|
use PhpUnitGen\Annotation\MockAnnotation; |
11
|
|
|
use PhpUnitGen\Annotation\SetterAnnotation; |
12
|
|
|
use PhpUnitGen\Model\ModelInterface\FunctionModelInterface; |
13
|
|
|
use PhpUnitGen\Model\ModelInterface\ParameterModelInterface; |
14
|
|
|
use PhpUnitGen\Model\ModelInterface\ReturnModelInterface; |
15
|
|
|
use PhpUnitGen\Model\PropertyTrait\AbstractTrait; |
16
|
|
|
use PhpUnitGen\Model\PropertyTrait\DocumentationTrait; |
17
|
|
|
use PhpUnitGen\Model\PropertyTrait\FinalTrait; |
18
|
|
|
use PhpUnitGen\Model\PropertyTrait\NameTrait; |
19
|
|
|
use PhpUnitGen\Model\PropertyTrait\NodeTrait; |
20
|
|
|
use PhpUnitGen\Model\PropertyTrait\StaticTrait; |
21
|
|
|
use PhpUnitGen\Model\PropertyTrait\VisibilityTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class FunctionModel. |
25
|
|
|
* |
26
|
|
|
* @author Paul Thébaud <[email protected]>. |
27
|
|
|
* @copyright 2017-2018 Paul Thébaud <[email protected]>. |
28
|
|
|
* @license https://opensource.org/licenses/MIT The MIT license. |
29
|
|
|
* @link https://github.com/paul-thebaud/phpunit-generator |
30
|
|
|
* @since Class available since Release 2.0.0. |
31
|
|
|
*/ |
32
|
|
|
class FunctionModel implements FunctionModelInterface |
33
|
|
|
{ |
34
|
|
|
use NameTrait; |
35
|
|
|
use VisibilityTrait; |
36
|
|
|
use StaticTrait; |
37
|
|
|
use FinalTrait; |
38
|
|
|
use AbstractTrait; |
39
|
|
|
use NodeTrait; |
40
|
|
|
use DocumentationTrait; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var ParameterModelInterface[]|Collection $parameters The function methods. |
44
|
|
|
*/ |
45
|
|
|
private $parameters; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var ReturnModelInterface $return The function return. |
49
|
|
|
*/ |
50
|
|
|
private $return; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @var bool $isGlobal Tells if the function is global. |
54
|
|
|
*/ |
55
|
|
|
private $isGlobal = false; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* FunctionModel constructor. |
59
|
|
|
*/ |
60
|
|
|
public function __construct() |
61
|
|
|
{ |
62
|
|
|
$this->parameters = new ArrayCollection(); |
63
|
|
|
$this->annotations = new ArrayCollection(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
|
public function addParameter(ParameterModelInterface $parameter): void |
70
|
|
|
{ |
71
|
|
|
$this->parameters->add($parameter); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
|
|
public function getParameters(): Collection |
78
|
|
|
{ |
79
|
|
|
return $this->parameters; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function setReturn(ReturnModelInterface $return): void |
86
|
|
|
{ |
87
|
|
|
$this->return = $return; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
public function getReturn(): ReturnModelInterface |
94
|
|
|
{ |
95
|
|
|
return $this->return; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
public function setIsGlobal(bool $isGlobal): void |
102
|
|
|
{ |
103
|
|
|
$this->isGlobal = true; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* {@inheritdoc} |
108
|
|
|
*/ |
109
|
|
|
public function isGlobal(): bool |
110
|
|
|
{ |
111
|
|
|
return $this->isGlobal; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return GetterAnnotation|null The getter annotation, null if none. |
116
|
|
|
*/ |
117
|
|
|
public function getGetterAnnotation(): ?GetterAnnotation |
118
|
|
|
{ |
119
|
|
|
$annotations = $this->annotations->filter(function (AnnotationInterface $annotation) { |
120
|
|
|
return $annotation->getType() === AnnotationInterface::TYPE_GETTER; |
121
|
|
|
}); |
122
|
|
|
if ($annotations->isEmpty()) { |
123
|
|
|
return null; |
124
|
|
|
} |
125
|
|
|
return $annotations->first(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return SetterAnnotation|null The setter annotation, null if none. |
130
|
|
|
*/ |
131
|
|
|
public function getSetterAnnotation(): ?SetterAnnotation |
132
|
|
|
{ |
133
|
|
|
$annotations = $this->annotations->filter(function (AnnotationInterface $annotation) { |
134
|
|
|
return $annotation->getType() === AnnotationInterface::TYPE_SETTER; |
135
|
|
|
}); |
136
|
|
|
if ($annotations->isEmpty()) { |
137
|
|
|
return null; |
138
|
|
|
} |
139
|
|
|
return $annotations->first(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @return Collection|AssertionAnnotation[] The assertion annotations. |
144
|
|
|
*/ |
145
|
|
|
public function getAssertAnnotations(): Collection |
146
|
|
|
{ |
147
|
|
|
return $this->annotations->filter(function (AnnotationInterface $annotation) { |
148
|
|
|
return $annotation->getType() === AnnotationInterface::TYPE_ASSERT; |
149
|
|
|
}); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return Collection|MockAnnotation[] The mock annotations. |
154
|
|
|
*/ |
155
|
|
|
public function getMockAnnotations(): Collection |
156
|
|
|
{ |
157
|
|
|
return $this->annotations->filter(function (AnnotationInterface $annotation) { |
158
|
|
|
return $annotation->getType() === AnnotationInterface::TYPE_MOCK; |
159
|
|
|
}); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|