| Conditions | 14 |
| Paths | 163 |
| Total Lines | 89 |
| Code Lines | 56 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| Bugs | 1 | Features | 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 |
||
| 42 | public function execute(InputInterface $input, OutputInterface $output) |
||
| 43 | { |
||
| 44 | $startDate = new \DateTime(); |
||
| 45 | |||
| 46 | $output->writeln(sprintf('[%s] <info>Checking listeners</info>', $startDate->format('r'))); |
||
| 47 | foreach ($this->getNotificationDispatcher()->getListeners() as $type => $listeners) { |
||
| 48 | $output->writeln(sprintf(' - %s', $type)); |
||
| 49 | foreach ($listeners as $listener) { |
||
| 50 | if (!$listener[0] instanceof ConsumerInterface) { |
||
| 51 | throw new \RuntimeException(sprintf('The registered service does not implement the ConsumerInterface (class=%s', get_class($listener[0]))); |
||
| 52 | } |
||
| 53 | |||
| 54 | $output->writeln(sprintf(' > %s', get_class($listener[0]))); |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | $type = $input->getOption('type'); |
||
| 59 | $showDetails = $input->getOption('show-details'); |
||
| 60 | |||
| 61 | $output->write(sprintf('[%s] <info>Retrieving backend</info> ...', $startDate->format('r'))); |
||
| 62 | $backend = $this->getBackend($type); |
||
| 63 | |||
| 64 | $output->writeln(''); |
||
| 65 | $output->write(sprintf('[%s] <info>Initialize backend</info> ...', $startDate->format('r'))); |
||
| 66 | |||
| 67 | // initialize the backend |
||
| 68 | $backend->initialize(); |
||
| 69 | |||
| 70 | $output->writeln(' done!'); |
||
| 71 | |||
| 72 | if ($type === null) { |
||
| 73 | $output->writeln(sprintf('[%s] <info>Starting the backend handler</info> - %s', $startDate->format('r'), get_class($backend))); |
||
| 74 | } else { |
||
| 75 | $output->writeln(sprintf('[%s] <info>Starting the backend handler</info> - %s (type: %s)', $startDate->format('r'), get_class($backend), $type)); |
||
| 76 | } |
||
| 77 | |||
| 78 | $startMemoryUsage = memory_get_usage(true); |
||
| 79 | $i = 0; |
||
| 80 | $iterator = $backend->getIterator(); |
||
| 81 | foreach ($iterator as $message) { |
||
| 82 | ++$i; |
||
| 83 | |||
| 84 | if (!$message instanceof MessageInterface) { |
||
| 85 | throw new \RuntimeException('The iterator must return a MessageInterface instance'); |
||
| 86 | } |
||
| 87 | |||
| 88 | if (!$message->getType()) { |
||
| 89 | $output->write('<error>Skipping : no type defined </error>'); |
||
| 90 | continue; |
||
| 91 | } |
||
| 92 | |||
| 93 | $date = new \DateTime(); |
||
| 94 | $output->write(sprintf('[%s] <info>%s</info> #%s: ', $date->format('r'), $message->getType(), $i)); |
||
| 95 | $memoryUsage = memory_get_usage(true); |
||
| 96 | |||
| 97 | try { |
||
| 98 | $start = microtime(true); |
||
| 99 | $returnInfos = $backend->handle($message, $this->getNotificationDispatcher()); |
||
| 100 | |||
| 101 | $currentMemory = memory_get_usage(true); |
||
| 102 | |||
| 103 | $output->writeln(sprintf('<comment>OK! </comment> - %0.04fs, %ss, %s, %s - %s = %s, %0.02f%%', |
||
| 104 | microtime(true) - $start, |
||
| 105 | $date->format('U') - $message->getCreatedAt()->format('U'), |
||
| 106 | $this->formatMemory($currentMemory - $memoryUsage), |
||
| 107 | $this->formatMemory($currentMemory), |
||
| 108 | $this->formatMemory($startMemoryUsage), |
||
| 109 | $this->formatMemory($currentMemory - $startMemoryUsage), |
||
| 110 | ($currentMemory - $startMemoryUsage) / $startMemoryUsage * 100 |
||
| 111 | )); |
||
| 112 | |||
| 113 | if ($showDetails && null !== $returnInfos) { |
||
| 114 | $output->writeln($returnInfos->getReturnMessage()); |
||
| 115 | } |
||
| 116 | } catch (HandlingException $e) { |
||
| 117 | $output->writeln(sprintf('<error>KO! - %s</error>', $e->getPrevious()->getMessage())); |
||
| 118 | } catch (\Exception $e) { |
||
| 119 | $output->writeln(sprintf('<error>KO! - %s</error>', $e->getMessage())); |
||
| 120 | } |
||
| 121 | |||
| 122 | $this->getEventDispatcher()->dispatch(IterateEvent::EVENT_NAME, new IterateEvent($iterator, $backend, $message)); |
||
| 123 | |||
| 124 | if ($input->getOption('iteration') && $i >= (int) $input->getOption('iteration')) { |
||
| 125 | $output->writeln('End of iteration cycle'); |
||
| 126 | |||
| 127 | return; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | |||
| 200 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: