Completed
Push — develop ( 10d533...fd1d54 )
by Paul
02:00
created

FunctionModel::getParamsAnnotation()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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