| Conditions | 26 |
| Paths | 216 |
| Total Lines | 182 |
| Code Lines | 90 |
| 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 |
||
| 175 | public function processFile($file, $contents) |
||
| 176 | { |
||
| 177 | $this->getLogger()->debug("Processing `$file`"); |
||
| 178 | $file = realpath($file); |
||
| 179 | |||
| 180 | self::$file = $file; |
||
| 181 | |||
| 182 | $ignoreFile = sprintf('%s/.signalignore', dirname($file)); |
||
| 183 | |||
| 184 | if (file_exists($ignoreFile)) |
||
| 185 | { |
||
| 186 | $this->getLogger()->notice("Skipping `$file` because of `$ignoreFile`" . PHP_EOL); |
||
| 187 | return; |
||
| 188 | } |
||
| 189 | |||
| 190 | $this->paths[] = $file; |
||
| 191 | // Remove initial `\` from namespace |
||
| 192 | try |
||
| 193 | { |
||
| 194 | $annotated = AnnotationUtility::rawAnnotate($file); |
||
| 195 | } |
||
| 196 | catch(NoClassInFileException $e) |
||
| 197 | { |
||
| 198 | $this->log($e, $file); |
||
| 199 | return; |
||
| 200 | } |
||
| 201 | catch (ClassNotFoundException $e) |
||
| 202 | { |
||
| 203 | $this->log($e, $file); |
||
| 204 | return; |
||
| 205 | } |
||
| 206 | catch (ParseException $e) |
||
| 207 | { |
||
| 208 | $this->err($e, $file); |
||
| 209 | return; |
||
| 210 | } |
||
| 211 | catch (UnexpectedValueException $e) |
||
| 212 | { |
||
| 213 | $this->err($e, $file); |
||
| 214 | return; |
||
| 215 | } |
||
| 216 | catch (Exception $e) |
||
| 217 | { |
||
| 218 | $this->err($e, $file); |
||
| 219 | return; |
||
| 220 | } |
||
| 221 | |||
| 222 | $namespace = preg_replace('~^\\\\+~', '', $annotated['namespace']); |
||
| 223 | $className = $annotated['className']; |
||
| 224 | |||
| 225 | |||
| 226 | // Use fully qualified name, class must autoload |
||
| 227 | $fqn = $namespace . '\\' . $className; |
||
| 228 | NameNormalizer::normalize($fqn); |
||
| 229 | |||
| 230 | try |
||
| 231 | { |
||
| 232 | // NOTE: This autoloader must be registered on ReflectionClass |
||
| 233 | // creation ONLY! That's why register/unregister. |
||
| 234 | // This will detect not found depending classes |
||
| 235 | // (base classes,interfaces,traits etc.) |
||
| 236 | $autoload = static::class . '::autoloadHandler'; |
||
| 237 | spl_autoload_register($autoload); |
||
| 238 | eval('$info = new ReflectionClass($fqn);'); |
||
| 239 | spl_autoload_unregister($autoload); |
||
| 240 | echo "$file\n"; |
||
| 241 | } |
||
| 242 | catch (ParseError $e) |
||
| 243 | { |
||
| 244 | $this->err($e, $file); |
||
| 245 | return; |
||
| 246 | } |
||
| 247 | catch (ClassNotFoundException $e) |
||
| 248 | { |
||
| 249 | $this->log($e, $file); |
||
| 250 | return; |
||
| 251 | } |
||
| 252 | catch (ReflectionException $e) |
||
| 253 | { |
||
| 254 | $this->err($e, $file); |
||
| 255 | return; |
||
| 256 | } |
||
| 257 | // $info is created in `eval` |
||
| 258 | /* @var $info ReflectionClass */ |
||
| 259 | $isAnnotated = $info->implementsInterface(AnnotatedInterface::class); |
||
| 260 | $hasSignals = $this->hasSignals($contents); |
||
| 261 | $isAbstract = $info->isAbstract() || $info->isInterface(); |
||
| 262 | |||
| 263 | if ($isAnnotated) |
||
| 264 | { |
||
| 265 | $this->getLogger()->debug("Annotated: $info->name"); |
||
| 266 | } |
||
| 267 | else |
||
| 268 | { |
||
| 269 | $this->getLogger()->debug("Not annotated: $info->name"); |
||
| 270 | } |
||
| 271 | |||
| 272 | // Old classes must now implement interface |
||
| 273 | // Brake BC! |
||
| 274 | if ($hasSignals && !$isAnnotated && !$isAbstract) |
||
| 275 | { |
||
| 276 | throw new UnexpectedValueException(sprintf('Class %s must implement %s to use signals', $fqn, AnnotatedInterface::class)); |
||
| 277 | } |
||
| 278 | |||
| 279 | // Skip not annotated class |
||
| 280 | if (!$isAnnotated) |
||
| 281 | { |
||
| 282 | return; |
||
| 283 | } |
||
| 284 | |||
| 285 | // Skip abstract classes |
||
| 286 | if ($isAbstract) |
||
| 287 | { |
||
| 288 | return; |
||
| 289 | } |
||
| 290 | try |
||
| 291 | { |
||
| 292 | // Discard notices (might be the case when outdated cache?) |
||
| 293 | $level = error_reporting(); |
||
| 294 | error_reporting(E_WARNING); |
||
| 295 | $meta = SignalsMeta::create($fqn); |
||
| 296 | error_reporting($level); |
||
| 297 | } |
||
| 298 | catch (ParseException $e) |
||
| 299 | { |
||
| 300 | $this->err($e, $file); |
||
| 301 | return; |
||
| 302 | } |
||
| 303 | catch (ClassNotFoundException $e) |
||
| 304 | { |
||
| 305 | $this->log($e, $file); |
||
| 306 | return; |
||
| 307 | } |
||
| 308 | catch (UnexpectedValueException $e) |
||
| 309 | { |
||
| 310 | $this->err($e, $file); |
||
| 311 | return; |
||
| 312 | } |
||
| 313 | |||
| 314 | /* @var $typeMeta DocumentTypeMeta */ |
||
| 315 | $typeMeta = $meta->type(); |
||
| 316 | |||
| 317 | // Signals |
||
| 318 | foreach ($typeMeta->signalFor as $slot) |
||
| 319 | { |
||
| 320 | $this->getLogger()->debug("Signal: $slot:$fqn"); |
||
| 321 | $this->data[Signal::Slots][$slot][$fqn] = true; |
||
| 322 | } |
||
| 323 | |||
| 324 | // Slots |
||
| 325 | // For constructor injection |
||
| 326 | foreach ($typeMeta->slotFor as $slot) |
||
| 327 | { |
||
| 328 | $key = implode('::', [$fqn, '__construct']) . '()'; |
||
| 329 | $this->getLogger()->debug("Slot: $slot:$fqn$key"); |
||
| 330 | $this->data[Signal::Signals][$slot][$fqn][$key] = true; |
||
| 331 | } |
||
| 332 | |||
| 333 | // For method injection |
||
| 334 | foreach ($meta->methods() as $methodName => $method) |
||
| 335 | { |
||
| 336 | /* @var $method DocumentMethodMeta */ |
||
| 337 | foreach ($method->slotFor as $slot) |
||
| 338 | { |
||
| 339 | $key = implode('::', [$fqn, $methodName]) . '()'; |
||
| 340 | $this->getLogger()->debug("Slot: $slot:$fqn$key"); |
||
| 341 | $this->data[Signal::Signals][$slot][$fqn][$key] = sprintf('%s()', $methodName); |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | // For property injection |
||
| 346 | foreach ($meta->fields() as $fieldName => $field) |
||
| 347 | { |
||
| 348 | /* @var $field DocumentPropertyMeta */ |
||
| 349 | foreach ($field->slotFor as $slot) |
||
| 350 | { |
||
| 351 | $key = implode('::$', [$fqn, $fieldName]); |
||
| 352 | $this->getLogger()->debug("Slot: $slot:$fqn$key"); |
||
| 353 | $this->data[Signal::Signals][$slot][$fqn][$key] = sprintf('%s', $fieldName); |
||
| 354 | } |
||
| 355 | } |
||
| 356 | } |
||
| 357 | |||
| 422 |