| Conditions | 1 |
| Paths | 1 |
| Total Lines | 119 |
| 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 |
||
| 186 | protected function getQueueNode() |
||
| 187 | { |
||
| 188 | $treeBuilder = new TreeBuilder('queues'); |
||
| 189 | |||
| 190 | $node = $treeBuilder->getRootNode(); |
||
| 191 | |||
| 192 | $queuesInfo = <<<'EOF' |
||
| 193 | Example for using RabbitMQ |
||
| 194 | - { queue: myQueue, recover: true, default: false, routing_key: the_routing_key, dead_letter_exchange: 'my.dead.letter.exchange' } |
||
| 195 | - { queue: catchall, default: true } |
||
| 196 | |||
| 197 | Example for using Doctrine |
||
| 198 | - { queue: sonata_page, types: [sonata.page.create_snapshot, sonata.page.create_snapshots] } |
||
| 199 | - { queue: catchall, default: true } |
||
| 200 | EOF; |
||
| 201 | |||
| 202 | $routingKeyInfo = <<<'EOF' |
||
| 203 | Only used by RabbitMQ |
||
| 204 | |||
| 205 | Direct exchange with routing_key |
||
| 206 | EOF; |
||
| 207 | |||
| 208 | $recoverInfo = <<<'EOF' |
||
| 209 | Only used by RabbitMQ |
||
| 210 | |||
| 211 | If set to true, the consumer will respond with a `basic.recover` when an exception occurs, |
||
| 212 | otherwise it will not respond at all and the message will be unacknowledged |
||
| 213 | EOF; |
||
| 214 | |||
| 215 | $deadLetterExchangeInfo = <<<'EOF' |
||
| 216 | Only used by RabbitMQ |
||
| 217 | |||
| 218 | If is set, failed messages will be rejected and sent to this exchange |
||
| 219 | EOF; |
||
| 220 | |||
| 221 | $deadLetterRoutingKeyInfo = <<<'EOF' |
||
| 222 | Only used by RabbitMQ |
||
| 223 | |||
| 224 | If set, failed messages will be routed to the queue using this key by dead-letter-exchange, |
||
| 225 | otherwise it will be requeued to the original queue if `dead-letter-exchange` is set. |
||
| 226 | |||
| 227 | If set, the queue must be configured with this key as `routing_key`. |
||
| 228 | EOF; |
||
| 229 | |||
| 230 | $ttlInfo = <<<'EOF' |
||
| 231 | Only used by RabbitMQ |
||
| 232 | |||
| 233 | Defines the per-queue message time-to-live (milliseconds) |
||
| 234 | EOF; |
||
| 235 | |||
| 236 | $prefetchCountInfo = <<<'EOF' |
||
| 237 | Only used by RabbitMQ |
||
| 238 | |||
| 239 | Defines the number of messages which will be delivered to the customer at a time. |
||
| 240 | EOF; |
||
| 241 | |||
| 242 | $typesInfo = <<<'EOF' |
||
| 243 | Only used by Doctrine |
||
| 244 | |||
| 245 | Defines types handled by the message backend |
||
| 246 | EOF; |
||
| 247 | |||
| 248 | $connectionNode = $node |
||
| 249 | ->info($queuesInfo) |
||
| 250 | ->requiresAtLeastOneElement() |
||
| 251 | ->prototype('array') |
||
| 252 | ; |
||
| 253 | |||
| 254 | $connectionNode |
||
| 255 | ->children() |
||
| 256 | ->scalarNode('queue') |
||
| 257 | ->info('The name of the queue') |
||
| 258 | ->cannotBeEmpty() |
||
| 259 | ->isRequired() |
||
| 260 | ->end() |
||
| 261 | ->booleanNode('default') |
||
| 262 | ->info('Set the name of the default queue') |
||
| 263 | ->defaultValue(false) |
||
| 264 | ->end() |
||
| 265 | |||
| 266 | // RabbitMQ configuration |
||
| 267 | ->scalarNode('routing_key') |
||
| 268 | ->info($routingKeyInfo) |
||
| 269 | ->defaultValue('') |
||
| 270 | ->end() |
||
| 271 | ->booleanNode('recover') |
||
| 272 | ->info($recoverInfo) |
||
| 273 | ->defaultValue(false) |
||
| 274 | ->end() |
||
| 275 | ->scalarNode('dead_letter_exchange') |
||
| 276 | ->info($deadLetterExchangeInfo) |
||
| 277 | ->defaultValue(null) |
||
| 278 | ->end() |
||
| 279 | ->scalarNode('dead_letter_routing_key') |
||
| 280 | ->info($deadLetterRoutingKeyInfo) |
||
| 281 | ->defaultValue(null) |
||
| 282 | ->end() |
||
| 283 | ->integerNode('ttl') |
||
| 284 | ->info($ttlInfo) |
||
| 285 | ->min(0) |
||
| 286 | ->defaultValue(null) |
||
| 287 | ->end() |
||
| 288 | ->integerNode('prefetch_count') |
||
| 289 | ->info($prefetchCountInfo) |
||
| 290 | ->min(0) |
||
| 291 | ->max(65535) |
||
| 292 | ->defaultValue(null) |
||
| 293 | ->end() |
||
| 294 | |||
| 295 | // Database configuration (Doctrine) |
||
| 296 | ->arrayNode('types') |
||
| 297 | ->info($typesInfo) |
||
| 298 | ->defaultValue([]) |
||
| 299 | ->prototype('scalar')->end() |
||
| 300 | ->end() |
||
| 301 | ->end(); |
||
| 302 | |||
| 303 | return $node; |
||
| 304 | } |
||
| 305 | } |
||
| 306 |
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: