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