| Conditions | 1 |
| Paths | 1 |
| Total Lines | 78 |
| Code Lines | 74 |
| 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. If you change this the class must extend the default one.')->end() |
||
| 205 | ->arrayNode('default_attributes') |
||
| 206 | ->info('Set the default LDAP attributes mapped for the LDAP user provider class.') |
||
| 207 | ->addDefaultsIfNotSet() |
||
| 208 | ->children() |
||
| 209 | ->scalarNode('username')->defaultValue('username')->end() |
||
| 210 | ->scalarNode('accountNonLocked')->defaultValue('locked')->end() |
||
| 211 | ->scalarNode('accountNonExpired')->defaultValue('accountExpirationDate')->end() |
||
| 212 | ->scalarNode('enabled')->defaultValue('disabled')->end() |
||
| 213 | ->scalarNode('credentialsNonExpired')->defaultValue('passwordMustChange')->end() |
||
| 214 | ->scalarNode('groups')->defaultValue('groups')->end() |
||
| 215 | ->scalarNode('guid')->defaultValue('guid')->end() |
||
| 216 | ->scalarNode('stringRepresentation')->defaultValue('username')->end() |
||
| 217 | ->end() |
||
| 218 | ->end() |
||
| 219 | ->arrayNode('guard') |
||
| 220 | ->info('Guard specific configuration options.') |
||
| 221 | ->addDefaultsIfNotSet() |
||
| 222 | ->children() |
||
| 223 | ->scalarNode('start_path')->defaultValue('login') |
||
| 224 | ->info('The default entry point/starting path as a route name.')->end() |
||
| 225 | ->end() |
||
| 226 | ->end() |
||
| 227 | ->arrayNode('additional_attributes') |
||
| 228 | ->info('Any additional attribute values that should be available when the user is loaded.') |
||
| 229 | ->prototype('scalar')->end() |
||
| 230 | ->end() |
||
| 231 | ->arrayNode('roles') |
||
| 232 | ->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.') |
||
| 233 | ->useAttributeAsKey('name') |
||
| 234 | ->prototype('array') |
||
| 235 | ->beforeNormalization() |
||
| 236 | ->ifTrue(function ($v) { |
||
| 237 | return !is_array($v); |
||
| 238 | }) |
||
| 239 | ->then(function ($v) { |
||
| 240 | return [$v]; |
||
| 241 | }) |
||
| 242 | ->end() |
||
| 243 | ->prototype('scalar')->end() |
||
| 244 | ->end() |
||
| 245 | ->end() |
||
| 246 | ->scalarNode('role_ldap_type')->defaultValue('group') |
||
| 247 | ->info('The LdapTools object type for the groups used to check for roles.')->end() |
||
| 248 | ->arrayNode('role_attributes') |
||
| 249 | ->info('When searching for groups/roles for a user, map to these attributes for GUID, SID, members, or name.') |
||
| 250 | ->addDefaultsIfNotSet() |
||
| 251 | ->children() |
||
| 252 | ->scalarNode('name')->defaultValue('name')->end() |
||
| 253 | ->scalarNode('sid')->defaultValue('sid')->end() |
||
| 254 | ->scalarNode('guid')->defaultValue('guid')->end() |
||
| 255 | ->scalarNode('members')->defaultValue('members')->end() |
||
| 256 | ->end() |
||
| 257 | ->end() |
||
| 258 | ->booleanNode('refresh_user_attributes') |
||
| 259 | ->info('Set this to true if you want user attributes re-queried on a user refresh.')->defaultFalse()->end() |
||
| 260 | ->booleanNode('refresh_user_roles') |
||
| 261 | ->info('Set this to true if you want user roles re-queried on a user refresh.')->defaultFalse()->end() |
||
| 262 | ->end() |
||
| 263 | ->end() |
||
| 264 | ->end(); |
||
| 265 | } |
||
| 266 | } |
||
| 267 |
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: