Conditions | 1 |
Paths | 1 |
Total Lines | 76 |
Code Lines | 72 |
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 |
||
188 | protected function addSecuritySection(ArrayNodeDefinition $node) |
||
189 | { |
||
190 | $node |
||
191 | ->children() |
||
192 | ->arrayNode('security') |
||
193 | ->addDefaultsIfNotSet() |
||
194 | ->children() |
||
195 | ->scalarNode('search_base') |
||
196 | ->info('The default DN to start the user search from.')->end() |
||
197 | ->scalarNode('ldap_object_type')->defaultValue('user') |
||
198 | ->info('The LdapTools object type for the user provider to search for.')->end() |
||
199 | ->scalarNode('default_role')->defaultValue('ROLE_USER') |
||
200 | ->info('Regardless of group membership this role will be assigned to the loaded user. Set it to null for no roles to be assigned by default.')->end() |
||
201 | ->booleanNode('check_groups_recursively') |
||
202 | ->info('If set to true then group membership will contain all groups, and nested groups, the user belongs to.')->defaultTrue()->end() |
||
203 | ->scalarNode('user')->defaultValue('\LdapTools\Bundle\LdapToolsBundle\Security\User\LdapUser') |
||
204 | ->info('The user class that the LDAP user provider will instantiate. It must implement the LdapUserInterface.')->end() |
||
205 | ->arrayNode('guard') |
||
206 | ->info('Guard specific configuration options.') |
||
207 | ->addDefaultsIfNotSet() |
||
208 | ->children() |
||
209 | ->scalarNode('login_path')->defaultValue('/login')->end() |
||
210 | ->scalarNode('default_target_path')->defaultValue('/')->end() |
||
211 | ->booleanNode('always_use_target_path')->defaultFalse()->end() |
||
212 | ->scalarNode('target_path_parameter')->defaultValue('_target_path')->end() |
||
213 | ->booleanNode('use_referrer')->defaultFalse()->end() |
||
214 | ->scalarNode('failure_path')->defaultNull()->end() |
||
215 | ->booleanNode('failure_forward')->defaultFalse()->end() |
||
216 | ->scalarNode('failure_path_parameter')->defaultValue('_failure_path')->end() |
||
217 | ->scalarNode('username_parameter')->defaultValue('_username')->end() |
||
218 | ->scalarNode('password_parameter')->defaultValue('_password')->end() |
||
219 | ->scalarNode('domain_parameter')->defaultValue('_ldap_domain')->end() |
||
220 | ->booleanNode('use_forward')->defaultFalse()->end() |
||
221 | ->booleanNode('post_only')->defaultFalse()->end() |
||
222 | ->booleanNode('remember_me')->defaultFalse()->end() |
||
223 | ->end() |
||
224 | ->end() |
||
225 | ->arrayNode('additional_attributes') |
||
226 | ->info('Any additional attribute values that should be available when the user is loaded.') |
||
227 | ->prototype('scalar')->end() |
||
228 | ->end() |
||
229 | ->arrayNode('roles') |
||
230 | ->info('Map LDAP group names to specific roles. If a user is a member of the group they will get the role mapped to it.') |
||
231 | ->useAttributeAsKey('name') |
||
232 | ->prototype('array') |
||
233 | ->beforeNormalization() |
||
234 | ->ifTrue(function ($v) { |
||
235 | return !is_array($v); |
||
236 | }) |
||
237 | ->then(function ($v) { |
||
238 | return [$v]; |
||
239 | }) |
||
240 | ->end() |
||
241 | ->prototype('scalar')->end() |
||
242 | ->end() |
||
243 | ->end() |
||
244 | ->scalarNode('role_ldap_type')->defaultValue('group') |
||
245 | ->info('The LdapTools object type for the groups used to check for roles.')->end() |
||
246 | ->arrayNode('role_attributes') |
||
247 | ->info('When searching for groups/roles for a user, map to these attributes for GUID, SID, members, or name.') |
||
248 | ->addDefaultsIfNotSet() |
||
249 | ->children() |
||
250 | ->scalarNode('name')->defaultValue('name')->end() |
||
251 | ->scalarNode('sid')->defaultValue('sid')->end() |
||
252 | ->scalarNode('guid')->defaultValue('guid')->end() |
||
253 | ->scalarNode('members')->defaultValue('members')->end() |
||
254 | ->end() |
||
255 | ->end() |
||
256 | ->booleanNode('refresh_user_attributes') |
||
257 | ->info('Set this to true if you want user attributes re-queried on a user refresh.')->defaultFalse()->end() |
||
258 | ->booleanNode('refresh_user_roles') |
||
259 | ->info('Set this to true if you want user roles re-queried on a user refresh.')->defaultFalse()->end() |
||
260 | ->end() |
||
261 | ->end() |
||
262 | ->end(); |
||
263 | } |
||
264 | } |
||
265 |
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: