| Conditions | 11 |
| Paths | 1 |
| Total Lines | 133 |
| Code Lines | 101 |
| 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 |
||
| 58 | public function addRepositoriesSection(ArrayNodeDefinition $rootNode) |
||
| 59 | { |
||
| 60 | $rootNode |
||
| 61 | ->children() |
||
| 62 | ->arrayNode('repositories') |
||
| 63 | ->info('Content repositories configuration') |
||
| 64 | ->example( |
||
| 65 | array( |
||
| 66 | 'main' => array( |
||
| 67 | 'storage' => array( |
||
| 68 | 'engine' => 'legacy', |
||
| 69 | 'connection' => 'my_doctrine_connection_name', |
||
| 70 | ), |
||
| 71 | ), |
||
| 72 | ) |
||
| 73 | ) |
||
| 74 | ->useAttributeAsKey('alias') |
||
| 75 | ->prototype('array') |
||
| 76 | ->beforeNormalization() |
||
| 77 | ->always( |
||
| 78 | // Handling deprecated structure by mapping it to new one |
||
| 79 | function ($v) { |
||
| 80 | if (isset($v['storage'])) { |
||
| 81 | return $v; |
||
| 82 | } |
||
| 83 | |||
| 84 | if (isset($v['engine'])) { |
||
| 85 | $v['storage']['engine'] = $v['engine']; |
||
| 86 | unset($v['engine']); |
||
| 87 | } |
||
| 88 | |||
| 89 | if (isset($v['connection'])) { |
||
| 90 | $v['storage']['connection'] = $v['connection']; |
||
| 91 | unset($v['connection']); |
||
| 92 | } |
||
| 93 | |||
| 94 | if (isset($v['config'])) { |
||
| 95 | $v['storage']['config'] = $v['config']; |
||
| 96 | unset($v['config']); |
||
| 97 | } |
||
| 98 | |||
| 99 | return $v; |
||
| 100 | } |
||
| 101 | ) |
||
| 102 | ->end() |
||
| 103 | ->beforeNormalization() |
||
| 104 | ->always( |
||
| 105 | // Setting default values |
||
| 106 | function ($v) { |
||
| 107 | if ($v === null) { |
||
| 108 | $v = array(); |
||
| 109 | } |
||
| 110 | |||
| 111 | if (!isset($v['storage'])) { |
||
| 112 | $v['storage'] = array(); |
||
| 113 | } |
||
| 114 | |||
| 115 | if (!isset($v['search'])) { |
||
| 116 | $v['search'] = array(); |
||
| 117 | } |
||
| 118 | |||
| 119 | if (!isset($v['fields_groups']['list'])) { |
||
| 120 | $v['fields_groups']['list'] = ['content']; |
||
| 121 | } |
||
| 122 | |||
| 123 | if (!isset($v['fields_groups']['default'])) { |
||
| 124 | $v['fields_groups']['default'] = 'content'; |
||
| 125 | } |
||
| 126 | |||
| 127 | if (!isset($v['options'])) { |
||
| 128 | $v['options'] = []; |
||
| 129 | } |
||
| 130 | |||
| 131 | return $v; |
||
| 132 | } |
||
| 133 | ) |
||
| 134 | ->end() |
||
| 135 | ->children() |
||
| 136 | ->arrayNode('storage') |
||
| 137 | ->children() |
||
| 138 | ->scalarNode('engine') |
||
| 139 | ->defaultValue('%ezpublish.api.storage_engine.default%') |
||
| 140 | ->info('The storage engine to use') |
||
| 141 | ->end() |
||
| 142 | ->scalarNode('connection') |
||
| 143 | ->defaultNull() |
||
| 144 | ->info('The connection name, if applicable (e.g. Doctrine connection name). If not set, the default connection will be used.') |
||
| 145 | ->end() |
||
| 146 | ->arrayNode('config') |
||
| 147 | ->info('Arbitrary configuration options, supported by your storage engine') |
||
| 148 | ->useAttributeAsKey('key') |
||
| 149 | ->prototype('variable')->end() |
||
| 150 | ->end() |
||
| 151 | ->end() |
||
| 152 | ->end() |
||
| 153 | ->arrayNode('search') |
||
| 154 | ->children() |
||
| 155 | ->scalarNode('engine') |
||
| 156 | ->defaultValue('%ezpublish.api.search_engine.default%') |
||
| 157 | ->info('The search engine to use') |
||
| 158 | ->end() |
||
| 159 | ->scalarNode('connection') |
||
| 160 | ->defaultNull() |
||
| 161 | ->info('The connection name, if applicable (e.g. Doctrine connection name). If not set, the default connection will be used.') |
||
| 162 | ->end() |
||
| 163 | ->arrayNode('config') |
||
| 164 | ->info('Arbitrary configuration options, supported by your search engine') |
||
| 165 | ->useAttributeAsKey('key') |
||
| 166 | ->prototype('variable')->end() |
||
| 167 | ->end() |
||
| 168 | ->end() |
||
| 169 | ->end() |
||
| 170 | ->arrayNode('fields_groups') |
||
| 171 | ->info('Definitions of fields groups.') |
||
| 172 | ->children() |
||
| 173 | ->arrayNode('list')->prototype('scalar')->end()->end() |
||
| 174 | ->scalarNode('default')->defaultValue('content')->end() |
||
| 175 | ->end() |
||
| 176 | ->end() |
||
| 177 | ->arrayNode('options') |
||
| 178 | ->info('Options for repository.') |
||
| 179 | ->children() |
||
| 180 | ->scalarNode('default_version_archive_limit') |
||
| 181 | ->defaultValue(5) |
||
| 182 | ->info('Default version archive limit (0-50), only enforced on publish, not on un-publish.') |
||
| 183 | ->end() |
||
| 184 | ->end() |
||
| 185 | ->end() |
||
| 186 | ->end() |
||
| 187 | ->end() |
||
| 188 | ->end() |
||
| 189 | ->end(); |
||
| 190 | } |
||
| 191 | |||
| 446 |
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: