Conditions | 1 |
Paths | 1 |
Total Lines | 82 |
Code Lines | 52 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 0 | Features | 1 |
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 |
||
182 | protected function getQueueNode() |
||
183 | { |
||
184 | $treeBuilder = new TreeBuilder(); |
||
185 | $node = $treeBuilder->root('queues'); |
||
186 | |||
187 | $queuesInfo = <<<EOF |
||
188 | Example for using RabbitMQ |
||
189 | - { queue: myQueue, recover: true, default: false, routing_key: the_routing_key, dead_letter_exchange: 'my.dead.letter.exchange' } |
||
190 | - { queue: catchall, default: true } |
||
191 | |||
192 | Example for using Doctrine |
||
193 | - { queue: sonata_page, types: [sonata.page.create_snapshot, sonata.page.create_snapshots] } |
||
194 | - { queue: catchall, default: true } |
||
195 | EOF; |
||
196 | |||
197 | $routingKeyInfo = <<<EOF |
||
198 | Only used by RabbitMQ |
||
199 | |||
200 | Direct exchange with routing_key |
||
201 | EOF; |
||
202 | |||
203 | $recoverInfo = <<<EOF |
||
204 | Only used by RabbitMQ |
||
205 | |||
206 | If set to true, the consumer will respond with a `basic.recover` when an exception occurs, |
||
207 | otherwise it will not respond at all and the message will be unacknowledged |
||
208 | EOF; |
||
209 | |||
210 | $deadLetterExchangeInfo = <<<EOF |
||
211 | Only used by RabbitMQ |
||
212 | |||
213 | If is set, failed messages will be rejected and sent to this exchange |
||
214 | EOF; |
||
215 | |||
216 | $typesInfo = <<<EOF |
||
217 | Only used by Doctrine |
||
218 | |||
219 | Defines types handled by the message backend |
||
220 | EOF; |
||
221 | |||
222 | $connectionNode = $node |
||
|
|||
223 | ->info($queuesInfo) |
||
224 | ->requiresAtLeastOneElement() |
||
225 | ->prototype('array') |
||
226 | ; |
||
227 | |||
228 | $connectionNode |
||
229 | ->children() |
||
230 | ->scalarNode('queue') |
||
231 | ->info('The name of the queue') |
||
232 | ->cannotBeEmpty() |
||
233 | ->isRequired() |
||
234 | ->end() |
||
235 | ->booleanNode('default') |
||
236 | ->info('Set the name of the default queue') |
||
237 | ->defaultValue(false) |
||
238 | ->end() |
||
239 | |||
240 | // RabbitMQ configuration |
||
241 | ->scalarNode('routing_key') |
||
242 | ->info($routingKeyInfo) |
||
243 | ->defaultValue('') |
||
244 | ->end() |
||
245 | ->booleanNode('recover') |
||
246 | ->info($recoverInfo) |
||
247 | ->defaultValue(false) |
||
248 | ->end() |
||
249 | ->scalarNode('dead_letter_exchange') |
||
250 | ->info($deadLetterExchangeInfo) |
||
251 | ->defaultValue(null) |
||
252 | ->end() |
||
253 | |||
254 | // Database configuration (Doctrine) |
||
255 | ->arrayNode('types') |
||
256 | ->info($typesInfo) |
||
257 | ->defaultValue(array()) |
||
258 | ->prototype('scalar')->end() |
||
259 | ->end() |
||
260 | ->end(); |
||
261 | |||
262 | return $node; |
||
263 | } |
||
264 | } |
||
265 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: