| Conditions | 7 |
| Paths | 7 |
| Total Lines | 56 |
| Code Lines | 50 |
| 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 |
||
| 74 | public function addConfiguration(NodeDefinition $node, $type) |
||
| 75 | { |
||
| 76 | switch ($type) { |
||
| 77 | case 'mail': |
||
| 78 | $node |
||
|
|
|||
| 79 | ->children() |
||
| 80 | ->scalarNode('default_sender')->defaultValue('')->end() |
||
| 81 | ->arrayNode('cc')->end() |
||
| 82 | ->arrayNode('bcc')->end() |
||
| 83 | ->end(); |
||
| 84 | break; |
||
| 85 | |||
| 86 | case 'database': |
||
| 87 | $node |
||
| 88 | ->children() |
||
| 89 | ->scalarNode('entity')->defaultValue('AppBundle:Notification')->end() |
||
| 90 | ->end(); |
||
| 91 | break; |
||
| 92 | case 'pusher': |
||
| 93 | $node |
||
| 94 | ->children() |
||
| 95 | ->scalarNode('auth_key')->defaultValue('')->end() |
||
| 96 | ->scalarNode('secret')->defaultValue('')->end() |
||
| 97 | ->scalarNode('app_id')->defaultValue('')->end() |
||
| 98 | ->scalarNode('cluster')->defaultValue('')->end() |
||
| 99 | ->booleanNode('encrypted')->defaultTrue()->end() |
||
| 100 | ->scalarNode('channel_name')->defaultValue('')->end() |
||
| 101 | ->scalarNode('event')->defaultValue('')->end() |
||
| 102 | ->end(); |
||
| 103 | break; |
||
| 104 | case 'nexmo': |
||
| 105 | $node |
||
| 106 | ->children() |
||
| 107 | ->scalarNode('api_key')->defaultValue('')->end() |
||
| 108 | ->scalarNode('api_secret')->defaultValue('')->end() |
||
| 109 | ->scalarNode('from')->defaultValue('')->end() |
||
| 110 | ->end(); |
||
| 111 | break; |
||
| 112 | case 'slack': |
||
| 113 | $node |
||
| 114 | ->children() |
||
| 115 | ->scalarNode('webhook')->defaultNull()->end() |
||
| 116 | ->end(); |
||
| 117 | break; |
||
| 118 | case 'logger': |
||
| 119 | $node |
||
| 120 | ->children() |
||
| 121 | ->scalarNode('type')->defaultValue('info')->end() |
||
| 122 | ->end(); |
||
| 123 | break; |
||
| 124 | default: |
||
| 125 | // Should allow any config |
||
| 126 | // @TODO: The type key to define the allowed configuration |
||
| 127 | break; |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 |
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: