Conditions | 1 |
Paths | 1 |
Total Lines | 122 |
Code Lines | 112 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 1 | 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 |
||
132 | private function getManagersNode() |
||
133 | { |
||
134 | $builder = new TreeBuilder(); |
||
135 | $node = $builder->root('managers'); |
||
136 | |||
137 | $node |
||
138 | ->isRequired() |
||
139 | ->requiresAtLeastOneElement() |
||
140 | ->useAttributeAsKey('name') |
||
141 | ->info('Maps managers to connections and bundles') |
||
142 | ->prototype('array') |
||
143 | ->children() |
||
144 | ->scalarNode('connection') |
||
145 | ->info('Sets connection for manager.') |
||
146 | ->end() |
||
147 | ->arrayNode('index') |
||
148 | ->children() |
||
149 | ->scalarNode('index_name') |
||
150 | ->isRequired() |
||
151 | ->info('Sets index name for connection.') |
||
152 | ->end() |
||
153 | ->arrayNode('hosts') |
||
154 | ->info('Defines hosts to connect to.') |
||
155 | ->defaultValue(['127.0.0.1:9200']) |
||
156 | ->prototype('scalar') |
||
157 | ->end() |
||
158 | ->end() |
||
159 | ->arrayNode('auth') |
||
160 | ->info('holds information for http authentication.') |
||
161 | ->children() |
||
162 | ->scalarNode('username') |
||
163 | ->isRequired() |
||
164 | ->example('john') |
||
165 | ->end() |
||
166 | ->scalarNode('password') |
||
167 | ->isRequired() |
||
168 | ->example('mytopsecretpassword') |
||
169 | ->end() |
||
170 | ->scalarNode('option') |
||
171 | ->defaultValue('Basic') |
||
172 | ->info('authentication type') |
||
173 | ->end() |
||
174 | ->end() |
||
175 | ->end() |
||
176 | ->arrayNode('settings') |
||
177 | ->defaultValue([]) |
||
178 | ->info('Sets index settings for connection.') |
||
179 | ->prototype('variable')->end() |
||
180 | ->end() |
||
181 | ->arrayNode('analysis') |
||
182 | ->addDefaultsIfNotSet() |
||
183 | ->info('Sets index analysis settings for connection.') |
||
184 | ->children() |
||
185 | ->arrayNode('tokenizer')->prototype('scalar')->defaultValue([])->end()->end() |
||
186 | ->arrayNode('filter')->prototype('scalar')->defaultValue([])->end()->end() |
||
187 | ->arrayNode('analyzer')->prototype('scalar')->defaultValue([])->end()->end() |
||
188 | ->end() |
||
189 | ->end() |
||
190 | ->end() |
||
191 | ->end() |
||
192 | ->integerNode('bulk_size') |
||
193 | ->min(0) |
||
194 | ->defaultValue(100) |
||
195 | ->info( |
||
196 | 'Maximum documents size in the bulk container. ' . |
||
197 | 'When the limit is reached it will auto-commit.' |
||
198 | ) |
||
199 | ->end() |
||
200 | ->enumNode('commit_mode') |
||
201 | ->values(['refresh', 'flush', 'none']) |
||
202 | ->defaultValue('refresh') |
||
203 | ->info( |
||
204 | 'The type of commit to the elasticsearch' |
||
205 | ) |
||
206 | ->end() |
||
207 | ->arrayNode('logger') |
||
208 | ->info('Enables elasticsearch queries logging') |
||
209 | ->addDefaultsIfNotSet() |
||
210 | ->beforeNormalization() |
||
211 | ->ifTrue( |
||
212 | function ($v) { |
||
213 | return is_bool($v); |
||
214 | } |
||
215 | ) |
||
216 | ->then( |
||
217 | function ($v) { |
||
218 | return ['enabled' => $v]; |
||
219 | } |
||
220 | ) |
||
221 | ->end() |
||
222 | ->children() |
||
223 | ->booleanNode('enabled') |
||
224 | ->info('enables logging') |
||
225 | ->defaultFalse() |
||
226 | ->end() |
||
227 | ->scalarNode('level') |
||
228 | ->info('Sets PSR logging level') |
||
229 | ->defaultValue(LogLevel::WARNING) |
||
230 | ->validate() |
||
231 | ->ifNotInArray((new \ReflectionClass('Psr\Log\LogLevel'))->getConstants()) |
||
232 | ->thenInvalid('Invalid PSR log level.') |
||
233 | ->end() |
||
234 | ->end() |
||
235 | ->scalarNode('log_file_name') |
||
236 | ->info('Log filename, by default it is a manager name') |
||
237 | ->defaultValue(null) |
||
238 | ->end() |
||
239 | ->end() |
||
240 | ->end() |
||
241 | ->arrayNode('mappings') |
||
242 | ->info('Maps manager to bundles. f.e. AcmeDemoBundle') |
||
243 | ->prototype('scalar')->end() |
||
244 | ->end() |
||
245 | ->booleanNode('force_commit') |
||
246 | ->info('Forces commit to elasticsearch on kernel terminate') |
||
247 | ->defaultTrue() |
||
248 | ->end() |
||
249 | ->end() |
||
250 | ->end(); |
||
251 | |||
252 | return $node; |
||
253 | } |
||
254 | } |
||
255 |
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: