Conditions | 14 |
Paths | 163 |
Total Lines | 106 |
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 |
||
45 | public function execute(InputInterface $input, OutputInterface $output): void |
||
46 | { |
||
47 | $startDate = new \DateTime(); |
||
48 | |||
49 | $output->writeln(sprintf('[%s] <info>Checking listeners</info>', $startDate->format('r'))); |
||
50 | foreach ($this->getNotificationDispatcher()->getListeners() as $type => $listeners) { |
||
51 | $output->writeln(sprintf(' - %s', $type)); |
||
52 | foreach ($listeners as $listener) { |
||
53 | if (!$listener[0] instanceof ConsumerInterface) { |
||
54 | throw new \RuntimeException(sprintf( |
||
55 | 'The registered service does not implement the ConsumerInterface (class=%s', |
||
56 | \get_class($listener[0]) |
||
57 | )); |
||
58 | } |
||
59 | |||
60 | $output->writeln(sprintf(' > %s', \get_class($listener[0]))); |
||
61 | } |
||
62 | } |
||
63 | |||
64 | $type = $input->getOption('type'); |
||
65 | $showDetails = $input->getOption('show-details'); |
||
66 | |||
67 | $output->write(sprintf('[%s] <info>Retrieving backend</info> ...', $startDate->format('r'))); |
||
68 | $backend = $this->getBackend($type); |
||
69 | |||
70 | $output->writeln(''); |
||
71 | $output->write(sprintf('[%s] <info>Initialize backend</info> ...', $startDate->format('r'))); |
||
72 | |||
73 | // initialize the backend |
||
74 | $backend->initialize(); |
||
75 | |||
76 | $output->writeln(' done!'); |
||
77 | |||
78 | if (null === $type) { |
||
79 | $output->writeln(sprintf( |
||
80 | '[%s] <info>Starting the backend handler</info> - %s', |
||
81 | $startDate->format('r'), |
||
82 | \get_class($backend) |
||
83 | )); |
||
84 | } else { |
||
85 | $output->writeln(sprintf( |
||
86 | '[%s] <info>Starting the backend handler</info> - %s (type: %s)', |
||
87 | $startDate->format('r'), |
||
88 | \get_class($backend), |
||
89 | $type |
||
90 | )); |
||
91 | } |
||
92 | |||
93 | $startMemoryUsage = memory_get_usage(true); |
||
94 | $i = 0; |
||
95 | $iterator = $backend->getIterator(); |
||
96 | foreach ($iterator as $message) { |
||
97 | ++$i; |
||
98 | |||
99 | if (!$message instanceof MessageInterface) { |
||
100 | throw new \RuntimeException('The iterator must return a MessageInterface instance'); |
||
101 | } |
||
102 | |||
103 | if (!$message->getType()) { |
||
104 | $output->write('<error>Skipping : no type defined </error>'); |
||
105 | |||
106 | continue; |
||
107 | } |
||
108 | |||
109 | $date = new \DateTime(); |
||
110 | $output->write(sprintf('[%s] <info>%s</info> #%s: ', $date->format('r'), $message->getType(), $i)); |
||
111 | $memoryUsage = memory_get_usage(true); |
||
112 | |||
113 | try { |
||
114 | $start = microtime(true); |
||
115 | $returnInfos = $backend->handle($message, $this->getNotificationDispatcher()); |
||
116 | |||
117 | $currentMemory = memory_get_usage(true); |
||
118 | |||
119 | $output->writeln(sprintf( |
||
120 | '<comment>OK! </comment> - %0.04fs, %ss, %s, %s - %s = %s, %0.02f%%', |
||
121 | microtime(true) - $start, |
||
122 | $date->format('U') - $message->getCreatedAt()->format('U'), |
||
123 | $this->formatMemory($currentMemory - $memoryUsage), |
||
124 | $this->formatMemory($currentMemory), |
||
125 | $this->formatMemory($startMemoryUsage), |
||
126 | $this->formatMemory($currentMemory - $startMemoryUsage), |
||
127 | ($currentMemory - $startMemoryUsage) / $startMemoryUsage * 100 |
||
128 | )); |
||
129 | |||
130 | if ($showDetails && null !== $returnInfos) { |
||
131 | $output->writeln($returnInfos->getReturnMessage()); |
||
132 | } |
||
133 | } catch (HandlingException $e) { |
||
134 | $output->writeln(sprintf('<error>KO! - %s</error>', $e->getPrevious()->getMessage())); |
||
135 | } catch (\Exception $e) { |
||
136 | $output->writeln(sprintf('<error>KO! - %s</error>', $e->getMessage())); |
||
137 | } |
||
138 | |||
139 | $this->getEventDispatcher()->dispatch( |
||
140 | new IterateEvent($iterator, $backend, $message), |
||
141 | IterateEvent::EVENT_NAME |
||
142 | ); |
||
143 | |||
144 | if ($input->getOption('iteration') && $i >= (int) $input->getOption('iteration')) { |
||
145 | $output->writeln('End of iteration cycle'); |
||
146 | |||
147 | return; |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 | |||
225 |
This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.