| Conditions | 1 |
| Paths | 1 |
| Total Lines | 57 |
| Code Lines | 53 |
| Lines | 0 |
| Ratio | 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 |
||
| 87 | private function getConnectionsNode() |
||
| 88 | { |
||
| 89 | $builder = new TreeBuilder(); |
||
| 90 | $node = $builder->root('connections'); |
||
| 91 | |||
| 92 | $node |
||
| 93 | ->isRequired() |
||
| 94 | ->requiresAtLeastOneElement() |
||
| 95 | ->info('Defines connections to indexes and its settings.') |
||
| 96 | ->prototype('array') |
||
| 97 | ->children() |
||
| 98 | ->arrayNode('hosts') |
||
| 99 | ->info('Defines hosts to connect to.') |
||
| 100 | ->defaultValue(['127.0.0.1:9200']) |
||
| 101 | ->prototype('scalar') |
||
| 102 | ->end() |
||
| 103 | ->end() |
||
| 104 | ->arrayNode('auth') |
||
| 105 | ->info('holds information for http authentication.') |
||
| 106 | ->children() |
||
| 107 | ->scalarNode('username') |
||
| 108 | ->isRequired() |
||
| 109 | ->example('john') |
||
| 110 | ->end() |
||
| 111 | ->scalarNode('password') |
||
| 112 | ->isRequired() |
||
| 113 | ->example('mytopsecretpassword') |
||
| 114 | ->end() |
||
| 115 | ->scalarNode('option') |
||
| 116 | ->defaultValue('Basic') |
||
| 117 | ->info('authentication type') |
||
| 118 | ->end() |
||
| 119 | ->end() |
||
| 120 | ->end() |
||
| 121 | ->scalarNode('index_name') |
||
| 122 | ->isRequired() |
||
| 123 | ->info('Sets index name for connection.') |
||
| 124 | ->end() |
||
| 125 | ->arrayNode('settings') |
||
| 126 | ->defaultValue([]) |
||
| 127 | ->info('Sets index settings for connection.') |
||
| 128 | ->prototype('variable')->end() |
||
| 129 | ->end() |
||
| 130 | ->arrayNode('analysis') |
||
| 131 | ->addDefaultsIfNotSet() |
||
| 132 | ->info('Sets index analysis settings for connection.') |
||
| 133 | ->children() |
||
| 134 | ->arrayNode('tokenizer')->prototype('scalar')->defaultValue([])->end()->end() |
||
| 135 | ->arrayNode('filter')->prototype('scalar')->defaultValue([])->end()->end() |
||
| 136 | ->arrayNode('analyzer')->prototype('scalar')->defaultValue([])->end()->end() |
||
| 137 | ->end() |
||
| 138 | ->end() |
||
| 139 | ->end() |
||
| 140 | ->end(); |
||
| 141 | |||
| 142 | return $node; |
||
| 143 | } |
||
| 144 | |||
| 224 |
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: