| Conditions | 10 |
| Paths | 1 |
| Total Lines | 120 |
| Code Lines | 94 |
| Lines | 6 |
| Ratio | 5 % |
| 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 |
||
| 45 | public function getConfigTreeBuilder() |
||
| 46 | { |
||
| 47 | $treeBuilder = new TreeBuilder(); |
||
| 48 | $rootNode = $treeBuilder->root('liip_imagine', 'array'); |
||
| 49 | |||
| 50 | $resolversPrototypeNode = $rootNode |
||
|
|
|||
| 51 | ->children() |
||
| 52 | ->arrayNode('resolvers') |
||
| 53 | ->useAttributeAsKey('name') |
||
| 54 | ->prototype('array') |
||
| 55 | ->performNoDeepMerging() |
||
| 56 | ; |
||
| 57 | $this->addResolversSections($resolversPrototypeNode); |
||
| 58 | |||
| 59 | $loadersPrototypeNode = $rootNode |
||
| 60 | ->children() |
||
| 61 | ->arrayNode('loaders') |
||
| 62 | ->useAttributeAsKey('name') |
||
| 63 | ->prototype('array') |
||
| 64 | ; |
||
| 65 | $this->addLoadersSections($loadersPrototypeNode); |
||
| 66 | |||
| 67 | $rootNode |
||
| 68 | ->beforeNormalization() |
||
| 69 | ->ifTrue(function ($v) { |
||
| 70 | return |
||
| 71 | empty($v['loaders']) || |
||
| 72 | empty($v['loaders']['default']) || |
||
| 73 | empty($v['resolvers']) || |
||
| 74 | empty($v['resolvers']['default']) |
||
| 75 | ; |
||
| 76 | }) |
||
| 77 | ->then(function ($v) { |
||
| 78 | if (empty($v['loaders'])) { |
||
| 79 | $v['loaders'] = []; |
||
| 80 | } |
||
| 81 | |||
| 82 | if (false == is_array($v['loaders'])) { |
||
| 83 | throw new \LogicException('Loaders has to be array'); |
||
| 84 | } |
||
| 85 | |||
| 86 | View Code Duplication | if (false == array_key_exists('default', $v['loaders'])) { |
|
| 87 | $v['loaders']['default'] = ['filesystem' => null]; |
||
| 88 | } |
||
| 89 | |||
| 90 | if (empty($v['resolvers'])) { |
||
| 91 | $v['resolvers'] = []; |
||
| 92 | } |
||
| 93 | |||
| 94 | if (false == is_array($v['resolvers'])) { |
||
| 95 | throw new \LogicException('Resolvers has to be array'); |
||
| 96 | } |
||
| 97 | |||
| 98 | View Code Duplication | if (false == array_key_exists('default', $v['resolvers'])) { |
|
| 99 | $v['resolvers']['default'] = ['web_path' => null]; |
||
| 100 | } |
||
| 101 | |||
| 102 | return $v; |
||
| 103 | }) |
||
| 104 | ->end() |
||
| 105 | ; |
||
| 106 | |||
| 107 | $rootNode |
||
| 108 | ->fixXmlConfig('filter_set', 'filter_sets') |
||
| 109 | ->children() |
||
| 110 | ->scalarNode('driver')->defaultValue('gd') |
||
| 111 | ->validate() |
||
| 112 | ->ifTrue(function ($v) { |
||
| 113 | return !in_array($v, ['gd', 'imagick', 'gmagick']); |
||
| 114 | }) |
||
| 115 | ->thenInvalid('Invalid imagine driver specified: %s') |
||
| 116 | ->end() |
||
| 117 | ->end() |
||
| 118 | ->scalarNode('cache')->defaultValue('default')->end() |
||
| 119 | ->scalarNode('cache_base_path')->defaultValue('')->end() |
||
| 120 | ->scalarNode('data_loader')->defaultValue('default')->end() |
||
| 121 | ->scalarNode('default_image')->defaultNull()->end() |
||
| 122 | ->arrayNode('controller') |
||
| 123 | ->addDefaultsIfNotSet() |
||
| 124 | ->children() |
||
| 125 | ->scalarNode('filter_action')->defaultValue('liip_imagine.controller:filterAction')->end() |
||
| 126 | ->scalarNode('filter_runtime_action')->defaultValue('liip_imagine.controller:filterRuntimeAction')->end() |
||
| 127 | ->end() |
||
| 128 | ->end() |
||
| 129 | ->arrayNode('filter_sets') |
||
| 130 | ->useAttributeAsKey('name') |
||
| 131 | ->prototype('array') |
||
| 132 | ->fixXmlConfig('filter', 'filters') |
||
| 133 | ->children() |
||
| 134 | ->scalarNode('quality')->defaultValue(100)->end() |
||
| 135 | ->scalarNode('jpeg_quality')->defaultNull()->end() |
||
| 136 | ->scalarNode('png_compression_level')->defaultNull()->end() |
||
| 137 | ->scalarNode('png_compression_filter')->defaultNull()->end() |
||
| 138 | ->scalarNode('format')->defaultNull()->end() |
||
| 139 | ->booleanNode('animated')->defaultFalse()->end() |
||
| 140 | ->scalarNode('cache')->defaultNull()->end() |
||
| 141 | ->scalarNode('data_loader')->defaultNull()->end() |
||
| 142 | ->scalarNode('default_image')->defaultNull()->end() |
||
| 143 | ->arrayNode('filters') |
||
| 144 | ->useAttributeAsKey('name') |
||
| 145 | ->prototype('array') |
||
| 146 | ->useAttributeAsKey('name') |
||
| 147 | ->prototype('variable')->end() |
||
| 148 | ->end() |
||
| 149 | ->end() |
||
| 150 | ->arrayNode('post_processors') |
||
| 151 | ->defaultValue([]) |
||
| 152 | ->useAttributeAsKey('name') |
||
| 153 | ->prototype('array') |
||
| 154 | ->useAttributeAsKey('name') |
||
| 155 | ->prototype('variable')->end() |
||
| 156 | ->end() |
||
| 157 | ->end() |
||
| 158 | ->end() |
||
| 159 | ->end() |
||
| 160 | ->end() |
||
| 161 | ->end(); |
||
| 162 | |||
| 163 | return $treeBuilder; |
||
| 164 | } |
||
| 165 | |||
| 193 |
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: