Conditions | 20 |
Paths | 192 |
Total Lines | 139 |
Code Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
131 | public function processFile($file, $contents) |
||
132 | { |
||
133 | $this->getLogger()->debug("Processing `$file`"); |
||
134 | $file = realpath($file); |
||
135 | |||
136 | $ignoreFile = sprintf('%s/.signalignore', dirname($file)); |
||
137 | |||
138 | if (file_exists($ignoreFile)) |
||
139 | { |
||
140 | $this->getLogger()->notice("Skipping `$file` because of `$ignoreFile`" . PHP_EOL); |
||
141 | return; |
||
142 | } |
||
143 | |||
144 | $this->paths[] = $file; |
||
145 | // Remove initial `\` from namespace |
||
146 | try |
||
147 | { |
||
148 | $annotated = AnnotationUtility::rawAnnotate($file); |
||
149 | } |
||
150 | catch (ParseException $e) |
||
151 | { |
||
152 | $this->err($e, $file); |
||
153 | return; |
||
154 | } |
||
155 | catch (UnexpectedValueException $e) |
||
156 | { |
||
157 | $this->log($e, $file); |
||
158 | return; |
||
159 | } |
||
160 | $namespace = preg_replace('~^\\\\+~', '', $annotated['namespace']); |
||
161 | $className = $annotated['className']; |
||
162 | |||
163 | |||
164 | // Use fully qualified name, class must autoload |
||
165 | $fqn = $namespace . '\\' . $className; |
||
166 | NameNormalizer::normalize($fqn); |
||
167 | |||
168 | try |
||
169 | { |
||
170 | $info = new ReflectionClass($fqn); |
||
171 | } |
||
172 | catch (ReflectionException $e) |
||
173 | { |
||
174 | $this->getLogger()->debug("Could not autoload $fqn"); |
||
175 | return; |
||
176 | } |
||
177 | $isAnnotated = $info->implementsInterface(AnnotatedInterface::class); |
||
178 | $hasSignals = $this->hasSignals($contents); |
||
179 | $isAbstract = $info->isAbstract() || $info->isInterface(); |
||
180 | |||
181 | if ($isAnnotated) |
||
182 | { |
||
183 | $this->getLogger()->debug("Annotated: $info->name"); |
||
184 | } |
||
185 | else |
||
186 | { |
||
187 | $this->getLogger()->debug("Not annotated: $info->name"); |
||
188 | } |
||
189 | |||
190 | // Old classes must now implement interface |
||
191 | // Brake BC! |
||
192 | if ($hasSignals && !$isAnnotated && !$isAbstract) |
||
193 | { |
||
194 | throw new UnexpectedValueException(sprintf('Class %s must implement %s to use signals', $fqn, AnnotatedInterface::class)); |
||
195 | } |
||
196 | |||
197 | // Skip not annotated class |
||
198 | if (!$isAnnotated) |
||
199 | { |
||
200 | return; |
||
201 | } |
||
202 | |||
203 | // Skip abstract classes |
||
204 | if ($isAbstract) |
||
205 | { |
||
206 | return; |
||
207 | } |
||
208 | try |
||
209 | { |
||
210 | // Discard notices (might be the case when outdated cache?) |
||
211 | $level = error_reporting(); |
||
212 | error_reporting(E_WARNING); |
||
213 | $meta = SignalsMeta::create($fqn); |
||
214 | error_reporting($level); |
||
215 | } |
||
216 | catch (ParseException $e) |
||
217 | { |
||
218 | $this->err($e, $file); |
||
219 | return; |
||
220 | } |
||
221 | catch (UnexpectedValueException $e) |
||
222 | { |
||
223 | $this->err($e, $file); |
||
224 | return; |
||
225 | } |
||
226 | |||
227 | /* @var $typeMeta DocumentTypeMeta */ |
||
228 | $typeMeta = $meta->type(); |
||
229 | |||
230 | // Signals |
||
231 | foreach ($typeMeta->signalFor as $slot) |
||
232 | { |
||
233 | $this->getLogger()->debug("Signal: $slot:$fqn"); |
||
234 | $this->data[Signal::Slots][$slot][$fqn] = true; |
||
235 | } |
||
236 | |||
237 | // Slots |
||
238 | // For constructor injection |
||
239 | foreach ($typeMeta->slotFor as $slot) |
||
240 | { |
||
241 | $key = implode('::', [$fqn, '__construct']) . '()'; |
||
242 | $this->getLogger()->debug("Slot: $slot:$fqn$key"); |
||
243 | $this->data[Signal::Signals][$slot][$fqn][$key] = true; |
||
244 | } |
||
245 | |||
246 | // For method injection |
||
247 | foreach ($meta->methods() as $methodName => $method) |
||
248 | { |
||
249 | /* @var $method DocumentMethodMeta */ |
||
250 | foreach ($method->slotFor as $slot) |
||
251 | { |
||
252 | $key = implode('::', [$fqn, $methodName]) . '()'; |
||
253 | $this->getLogger()->debug("Slot: $slot:$fqn$key"); |
||
254 | $this->data[Signal::Signals][$slot][$fqn][$key] = sprintf('%s()', $methodName); |
||
255 | } |
||
256 | } |
||
257 | |||
258 | // For property injection |
||
259 | foreach ($meta->fields() as $fieldName => $field) |
||
260 | { |
||
261 | /* @var $field DocumentPropertyMeta */ |
||
262 | foreach ($field->slotFor as $slot) |
||
263 | { |
||
264 | $key = implode('::$', [$fqn, $fieldName]); |
||
265 | $this->getLogger()->debug("Slot: $slot:$fqn$key"); |
||
266 | $this->data[Signal::Signals][$slot][$fqn][$key] = sprintf('%s', $fieldName); |
||
267 | } |
||
268 | } |
||
269 | } |
||
270 | |||
298 |