| Conditions | 1 |
| Paths | 1 |
| Total Lines | 73 |
| Code Lines | 63 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 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 |
||
| 150 | private function getManagersNode() |
||
| 151 | { |
||
| 152 | $builder = new TreeBuilder(); |
||
| 153 | $node = $builder->root('managers'); |
||
| 154 | |||
| 155 | $node |
||
| 156 | ->isRequired() |
||
| 157 | ->requiresAtLeastOneElement() |
||
| 158 | ->info('Maps managers to connections and bundles') |
||
| 159 | ->prototype('array') |
||
| 160 | ->children() |
||
| 161 | ->scalarNode('connection') |
||
| 162 | ->isRequired() |
||
| 163 | ->info('Sets connection for manager.') |
||
| 164 | ->end() |
||
| 165 | ->integerNode('bulk_size') |
||
| 166 | ->min(0) |
||
| 167 | ->defaultValue(100) |
||
| 168 | ->info( |
||
| 169 | 'Maximum documents size in the bulk container. ' . |
||
| 170 | 'When the limit is reached it will auto-commit.' |
||
| 171 | ) |
||
| 172 | ->end() |
||
| 173 | ->enumNode('commit_mode') |
||
| 174 | ->values(['refresh', 'flush', 'none']) |
||
| 175 | ->defaultValue('refresh') |
||
| 176 | ->info( |
||
| 177 | 'The type of commit to the elasticsearch' |
||
| 178 | ) |
||
| 179 | ->end() |
||
| 180 | ->arrayNode('logger') |
||
| 181 | ->info('Enables elasticsearch queries logging') |
||
| 182 | ->addDefaultsIfNotSet() |
||
| 183 | ->beforeNormalization() |
||
| 184 | ->ifTrue( |
||
| 185 | function ($v) { |
||
| 186 | return is_bool($v); |
||
| 187 | } |
||
| 188 | ) |
||
| 189 | ->then( |
||
| 190 | function ($v) { |
||
| 191 | return ['enabled' => $v]; |
||
| 192 | } |
||
| 193 | ) |
||
| 194 | ->end() |
||
| 195 | ->children() |
||
| 196 | ->booleanNode('enabled') |
||
| 197 | ->info('enables logging') |
||
| 198 | ->defaultFalse() |
||
| 199 | ->end() |
||
| 200 | ->scalarNode('level') |
||
| 201 | ->info('Sets PSR logging level') |
||
| 202 | ->defaultValue(LogLevel::WARNING) |
||
| 203 | ->validate() |
||
| 204 | ->ifNotInArray((new \ReflectionClass('Psr\Log\LogLevel'))->getConstants()) |
||
| 205 | ->thenInvalid('Invalid PSR log level.') |
||
| 206 | ->end() |
||
| 207 | ->end() |
||
| 208 | ->scalarNode('log_file_name') |
||
| 209 | ->info('Log filename, by default it is a manager name') |
||
| 210 | ->defaultValue(null) |
||
| 211 | ->end() |
||
| 212 | ->end() |
||
| 213 | ->end() |
||
| 214 | ->arrayNode('mappings') |
||
| 215 | ->info('Maps manager to bundles. f.e. AcmeDemoBundle') |
||
| 216 | ->prototype('scalar')->end() |
||
| 217 | ->end() |
||
| 218 | ->end() |
||
| 219 | ->end(); |
||
| 220 | |||
| 221 | return $node; |
||
| 222 | } |
||
| 223 | } |
||
| 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: