1 | <?php |
||
14 | class MethodPattern implements PatternInterface { |
||
15 | |||
16 | const OUTPUT_BODY = 0; |
||
17 | |||
18 | const OUTPUT_FULL = 1; |
||
19 | |||
20 | const OUTPUT_DOC_COMMENT = 2; |
||
21 | |||
22 | |||
23 | /** |
||
24 | * @var QueryStrategy |
||
25 | */ |
||
26 | private $nameQuery; |
||
27 | |||
28 | /** |
||
29 | * @var callable[] |
||
30 | */ |
||
31 | private $modifierChecker = []; |
||
32 | |||
33 | /** |
||
34 | * @var callable |
||
35 | */ |
||
36 | private $bodyChecker; |
||
37 | |||
38 | /** |
||
39 | * @var callable |
||
40 | */ |
||
41 | private $docCommentChecker; |
||
42 | |||
43 | /** |
||
44 | * @var int |
||
45 | */ |
||
46 | private $outputType = self::OUTPUT_BODY; |
||
47 | |||
48 | |||
49 | /** |
||
50 | * |
||
51 | */ |
||
52 | 19 | public function __construct() { |
|
67 | |||
68 | |||
69 | /** |
||
70 | * @return $this |
||
71 | */ |
||
72 | 3 | public function outputFull() { |
|
76 | |||
77 | |||
78 | /** |
||
79 | * @return $this |
||
80 | */ |
||
81 | 3 | public function outputBody() { |
|
85 | |||
86 | |||
87 | /** |
||
88 | * @return $this |
||
89 | */ |
||
90 | 3 | public function outputDocComment() { |
|
94 | |||
95 | |||
96 | /** |
||
97 | * @param string|QueryStrategy $name |
||
98 | * @return $this |
||
99 | */ |
||
100 | 19 | public function withName($name) { |
|
101 | 16 | if (is_string($name)) { |
|
102 | 2 | $this->nameQuery = Strict::create()->valueIs($name); |
|
103 | 10 | } elseif ($name instanceof QueryStrategy) { |
|
104 | 19 | $this->nameQuery = $name; |
|
105 | 8 | } else { |
|
106 | 2 | throw new \InvalidArgumentException('Invalid name format. Expect string or Query'); |
|
107 | 3 | } |
|
108 | |||
109 | 19 | return $this; |
|
110 | } |
||
111 | |||
112 | |||
113 | /** |
||
114 | * @param callable $check |
||
115 | * @return $this |
||
116 | */ |
||
117 | 19 | public function withBody(callable $check) { |
|
118 | 19 | $this->bodyChecker = $check; |
|
119 | 19 | return $this; |
|
120 | 3 | } |
|
121 | |||
122 | |||
123 | /** |
||
124 | * @param Collection $body |
||
125 | * @return bool |
||
126 | * @throws \Exception |
||
127 | */ |
||
128 | 15 | private function isValidBody(Collection $body) { |
|
129 | 15 | $checker = $this->bodyChecker; |
|
130 | 14 | $result = $checker($body); |
|
131 | 14 | if (!is_bool($result)) { |
|
132 | throw new \Exception('Body checker should return boolean result'); |
||
133 | } |
||
134 | |||
135 | 15 | return $result; |
|
136 | 1 | } |
|
137 | |||
138 | |||
139 | /** |
||
140 | * @param callable $check |
||
141 | * @return $this |
||
142 | */ |
||
143 | 19 | public function withDocComment(callable $check = null) { |
|
144 | |||
145 | 19 | if ($check === null) { |
|
146 | $check = function (Token $token) { |
||
147 | 2 | return $token->getType() === T_DOC_COMMENT; |
|
148 | 2 | }; |
|
149 | 1 | } |
|
150 | 19 | $this->docCommentChecker = $check; |
|
151 | |||
152 | 19 | return $this; |
|
153 | 3 | } |
|
154 | |||
155 | |||
156 | /** |
||
157 | * Find functions without doc comments |
||
158 | */ |
||
159 | 3 | public function withoutDocComment() { |
|
166 | |||
167 | |||
168 | /** |
||
169 | * @return $this |
||
170 | */ |
||
171 | 19 | public function withAnyModifier() { |
|
172 | 19 | $this->modifierChecker = []; |
|
173 | $this->modifierChecker[] = function () { |
||
174 | 15 | return true; |
|
175 | 3 | }; |
|
176 | 19 | return $this; |
|
177 | } |
||
178 | |||
179 | |||
180 | /** |
||
181 | * @param string $modifier |
||
182 | * @return $this |
||
183 | */ |
||
184 | 2 | public function withModifier($modifier) { |
|
191 | |||
192 | |||
193 | /** |
||
194 | * @param string $modifier |
||
195 | * @return $this |
||
196 | */ |
||
197 | public function withoutModifier($modifier) { |
||
205 | |||
206 | |||
207 | /** |
||
208 | * @param array $modifiers Array<String> |
||
209 | * @return bool |
||
210 | * @throws \Exception |
||
211 | */ |
||
212 | 15 | private function isValidModifiers(array $modifiers) { |
|
226 | |||
227 | |||
228 | /** |
||
229 | * @param QuerySequence $querySequence |
||
230 | * @return Collection|null |
||
231 | */ |
||
232 | 16 | public function __invoke(QuerySequence $querySequence) { |
|
325 | |||
326 | |||
327 | /** |
||
328 | * @param Token $token |
||
329 | * @return mixed |
||
330 | * @throws \Exception |
||
331 | */ |
||
332 | 15 | private function isValidDocComment(Token $token) { |
|
341 | |||
342 | |||
343 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.