1 | <?php |
||
15 | class ClassPattern implements PatternInterface { |
||
16 | |||
17 | /** |
||
18 | * Result of this pattern will be body of the class |
||
19 | */ |
||
20 | const OUTPUT_BODY = 1; |
||
21 | |||
22 | /** |
||
23 | * Result of this pattern will be full class |
||
24 | */ |
||
25 | const OUTPUT_FULL = 2; |
||
26 | |||
27 | |||
28 | /** |
||
29 | * @var QueryStrategy |
||
30 | */ |
||
31 | private $nameQuery = null; |
||
32 | |||
33 | /** |
||
34 | * @var callable |
||
35 | */ |
||
36 | private $docCommentChecker; |
||
37 | |||
38 | /** |
||
39 | * @var callable[] |
||
40 | */ |
||
41 | private $modifierChecker; |
||
42 | |||
43 | /** |
||
44 | * @var int |
||
45 | */ |
||
46 | private $outputType = self::OUTPUT_BODY; |
||
47 | |||
48 | |||
49 | /** |
||
50 | * By default we search for classes with any name |
||
51 | */ |
||
52 | 53 | public function __construct() { |
|
57 | |||
58 | |||
59 | /** |
||
60 | * @codeCoverageIgnore |
||
61 | * @deprecated |
||
62 | * @param string $name |
||
63 | * @return $this |
||
64 | */ |
||
65 | public function nameIs($name) { |
||
69 | |||
70 | |||
71 | /** |
||
72 | * @param QueryStrategy|string $name |
||
73 | * @return $this |
||
74 | */ |
||
75 | 12 | public function withName($name) { |
|
76 | 10 | if (is_string($name)) { |
|
77 | 6 | $this->nameQuery = Strict::create()->valueIs($name); |
|
78 | 12 | } elseif ($name instanceof QueryStrategy) { |
|
79 | 5 | $this->nameQuery = $name; |
|
80 | 4 | } else { |
|
81 | 2 | throw new \InvalidArgumentException('Expect string or QueryInterface'); |
|
82 | 2 | } |
|
83 | |||
84 | 10 | return $this; |
|
85 | } |
||
86 | |||
87 | |||
88 | /** |
||
89 | * @return $this |
||
90 | */ |
||
91 | 3 | public function withDocComment() { |
|
99 | |||
100 | |||
101 | /** |
||
102 | * @return $this |
||
103 | */ |
||
104 | 53 | public function withPossibleDocComment() { |
|
105 | $this->docCommentChecker = function () { |
||
106 | |||
107 | 35 | }; |
|
108 | 53 | return $this; |
|
109 | } |
||
110 | |||
111 | |||
112 | /** |
||
113 | * @return $this |
||
114 | */ |
||
115 | 3 | public function withoutDocComment() { |
|
123 | |||
124 | |||
125 | /** |
||
126 | * @return $this |
||
127 | */ |
||
128 | 3 | public function outputBody() { |
|
132 | |||
133 | |||
134 | /** |
||
135 | * @return $this |
||
136 | */ |
||
137 | 19 | public function outputFull() { |
|
141 | |||
142 | |||
143 | /** |
||
144 | * @codeCoverageIgnore |
||
145 | * @deprecated |
||
146 | * @param QueryStrategy $strategy |
||
147 | * @return $this |
||
148 | */ |
||
149 | public function whereName(QueryStrategy $strategy) { |
||
153 | |||
154 | |||
155 | /** |
||
156 | * @inheritdoc |
||
157 | */ |
||
158 | 39 | public function __invoke(QuerySequence $querySequence) { |
|
159 | |||
160 | |||
161 | 36 | $comment = $querySequence->process(Possible::create()->typeIs(T_DOC_COMMENT)); |
|
162 | |||
163 | 36 | $querySequence->possible(T_WHITESPACE); |
|
164 | 36 | $modifier = $querySequence->process(Possible::create()->valueIs([ |
|
165 | 36 | 'abstract', |
|
166 | 36 | 'final', |
|
167 | 36 | ])); |
|
168 | |||
169 | 36 | $querySequence->possible(T_WHITESPACE); |
|
170 | 36 | $start = $querySequence->strict('class'); |
|
171 | 36 | $querySequence->strict(T_WHITESPACE); |
|
172 | 36 | $querySequence->process($this->nameQuery); |
|
173 | 36 | $startClassBody = $querySequence->search('{'); |
|
174 | 36 | $querySequence->moveToToken($startClassBody); |
|
175 | 36 | $body = $querySequence->section('{', '}'); |
|
176 | |||
177 | 36 | if ($modifier->isValid()) { |
|
178 | 14 | $start = $modifier; |
|
179 | 14 | } |
|
180 | |||
181 | 36 | if ($comment->isValid()) { |
|
182 | 12 | $start = $comment; |
|
183 | 10 | } |
|
184 | |||
185 | 36 | $docCommentChecker = $this->docCommentChecker; |
|
186 | 36 | $docCommentChecker($comment, $querySequence); |
|
187 | |||
188 | |||
189 | 36 | foreach ($this->modifierChecker as $checker) { |
|
190 | 36 | $checker($modifier, $querySequence); |
|
191 | 36 | } |
|
192 | |||
193 | |||
194 | 36 | if (!$querySequence->isValid()) { |
|
195 | 39 | return null; |
|
196 | } |
||
197 | |||
198 | |||
199 | 37 | if ($this->outputType === self::OUTPUT_BODY) { |
|
200 | 20 | return $body->extractItems(1, -1); |
|
201 | } |
||
202 | |||
203 | # self::OUTPUT_FULL |
||
204 | 14 | return $querySequence->getCollection()->extractByTokens($start, $body->getLast()); |
|
205 | } |
||
206 | |||
207 | |||
208 | /** |
||
209 | * @return $this |
||
210 | */ |
||
211 | 53 | public function withAnyModifier() { |
|
217 | |||
218 | |||
219 | /** |
||
220 | * @param string $modifier |
||
221 | * @return $this |
||
222 | */ |
||
223 | 5 | public function withModifier($modifier) { |
|
234 | |||
235 | |||
236 | /** |
||
237 | * @param string $modifier |
||
238 | * @return $this |
||
239 | */ |
||
240 | public function withoutModifier($modifier) { |
||
249 | |||
250 | |||
251 | } |