| Conditions | 4 |
| Paths | 1 |
| Total Lines | 53 |
| Code Lines | 48 |
| 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 |
||
| 56 | public function getNodeDefinition(NodeDefinition $node) |
||
| 57 | { |
||
| 58 | $node->children() |
||
|
|
|||
| 59 | ->arrayNode($this->name()) |
||
| 60 | ->addDefaultsIfNotSet() |
||
| 61 | ->canBeEnabled() |
||
| 62 | ->validate() |
||
| 63 | ->ifTrue(function ($config) { |
||
| 64 | return true === $config['enabled'] && empty($config['realm']); |
||
| 65 | }) |
||
| 66 | ->thenInvalid('The option "realm" must be set.') |
||
| 67 | ->end() |
||
| 68 | ->validate() |
||
| 69 | ->ifTrue(function ($config) { |
||
| 70 | return true === $config['enabled'] && empty($config['repository']); |
||
| 71 | }) |
||
| 72 | ->thenInvalid('The option "repository" must be set.') |
||
| 73 | ->end() |
||
| 74 | ->validate() |
||
| 75 | ->ifTrue(function ($config) { |
||
| 76 | return true === $config['enabled'] && $config['max_length'] < $config['min_length']; |
||
| 77 | }) |
||
| 78 | ->thenInvalid('The option "max_length" must be greater than "min_length".') |
||
| 79 | ->end() |
||
| 80 | ->children() |
||
| 81 | ->booleanNode('required') |
||
| 82 | ->defaultFalse() |
||
| 83 | ->end() |
||
| 84 | ->scalarNode('realm') |
||
| 85 | ->defaultNull() |
||
| 86 | ->end() |
||
| 87 | ->booleanNode('authorization_header') |
||
| 88 | ->defaultTrue() |
||
| 89 | ->end() |
||
| 90 | ->booleanNode('query_string') |
||
| 91 | ->defaultFalse() |
||
| 92 | ->end() |
||
| 93 | ->booleanNode('request_body') |
||
| 94 | ->defaultFalse() |
||
| 95 | ->end() |
||
| 96 | ->integerNode('min_length') |
||
| 97 | ->defaultValue(50) |
||
| 98 | ->min(0) |
||
| 99 | ->end() |
||
| 100 | ->integerNode('max_length') |
||
| 101 | ->defaultValue(100) |
||
| 102 | ->min(1) |
||
| 103 | ->end() |
||
| 104 | ->scalarNode('repository') |
||
| 105 | ->defaultNull() |
||
| 106 | ->end() |
||
| 107 | ->end(); |
||
| 108 | } |
||
| 109 | |||
| 118 |
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: