Conditions | 11 |
Paths | 53 |
Total Lines | 64 |
Code Lines | 42 |
Lines | 6 |
Ratio | 9.38 % |
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 |
||
91 | private function handleException($exception, Message $message, array $options) |
||
92 | { |
||
93 | $headers = $message->getProperties(); |
||
94 | if (isset($headers['headers']['x-death'])) { |
||
95 | $xDeathHeaders = $headers['headers']['x-death']; |
||
96 | // PhpAmqpLib compatibility |
||
97 | if ($xDeathHeaders instanceof AMQPArray) { |
||
98 | $xDeathHeaders = $headers['headers']['x-death']->getNativeData(); |
||
99 | } |
||
100 | |||
101 | $queueXDeathHeader = null; |
||
102 | View Code Duplication | foreach ($xDeathHeaders as $xDeathHeader) { |
|
103 | if (isset($xDeathHeader['queue']) && $xDeathHeader['queue'] === $this->queueName) { |
||
104 | $queueXDeathHeader = $xDeathHeader; |
||
105 | break; |
||
106 | } |
||
107 | } |
||
108 | |||
109 | if (null === $queueXDeathHeader) { |
||
110 | $this->logException( |
||
111 | $exception, |
||
112 | sprintf( |
||
113 | '[XDeathMaxLifetime] No x-death header found for queue name "%s". Do nothing.', |
||
114 | $this->queueName |
||
115 | ), |
||
116 | $options['x_death_max_lifetime_fail_log_levels_map'] |
||
117 | ); |
||
118 | } elseif (isset($queueXDeathHeader['time'])) { |
||
119 | $xDeathTimestamp = $queueXDeathHeader['time']; |
||
120 | // PhpAmqpLib compatibility |
||
121 | if ($xDeathTimestamp instanceof \DateTime) { |
||
122 | $xDeathTimestamp = $xDeathTimestamp->getTimestamp(); |
||
123 | } |
||
124 | $remainLifetime = $xDeathTimestamp - (time() - $options['x_death_max_lifetime']); |
||
125 | if ($remainLifetime <= 0) { |
||
126 | $this->logException( |
||
127 | $exception, |
||
128 | sprintf( |
||
129 | '[XDeathMaxLifetime] Max lifetime reached. %s/%s seconds exceed. Execute the configured callback.', |
||
130 | abs($remainLifetime), |
||
131 | $options['x_death_max_lifetime'] |
||
132 | ), |
||
133 | $options['x_death_max_lifetime_fail_log_levels_map'] |
||
134 | ); |
||
135 | |||
136 | if (null !== $return = \call_user_func($this->callback, $exception, $message, $options)) { |
||
137 | return $return; |
||
138 | } |
||
139 | } else { |
||
140 | $this->logException( |
||
141 | $exception, |
||
142 | sprintf( |
||
143 | '[XDeathMaxLifetime] Lifetime remain %d/%d seconds.', |
||
144 | $remainLifetime, |
||
145 | $options['x_death_max_lifetime'] |
||
146 | ), |
||
147 | $options['x_death_max_lifetime_log_levels_map'] |
||
148 | ); |
||
149 | } |
||
150 | } |
||
151 | } |
||
152 | |||
153 | throw $exception; |
||
154 | } |
||
155 | |||
183 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.