Complex classes like LdapUserProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use LdapUserProvider, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
33 | class LdapUserProvider implements UserProviderInterface |
||
34 | { |
||
35 | /** |
||
36 | * @var LdapManager |
||
37 | */ |
||
38 | protected $ldap; |
||
39 | |||
40 | /** |
||
41 | * @var EventDispatcherInterface |
||
42 | */ |
||
43 | protected $dispatcher; |
||
44 | |||
45 | /** |
||
46 | * @var array The role to LDAP group name map. |
||
47 | */ |
||
48 | protected $roleMap = []; |
||
49 | |||
50 | /** |
||
51 | * @var array Map names to their LDAP attribute names when querying for LDAP groups used for roles. |
||
52 | */ |
||
53 | protected $roleAttrMap = [ |
||
54 | 'name' => 'name', |
||
55 | 'sid' => 'sid', |
||
56 | 'guid' => 'guid', |
||
57 | 'members' => 'members', |
||
58 | ]; |
||
59 | |||
60 | /** |
||
61 | * @var array Default attributes selected for the Advanced User Interface. |
||
62 | */ |
||
63 | protected $defaultAttributes = [ |
||
64 | 'username', |
||
65 | 'guid', |
||
66 | 'accountExpirationDate', |
||
67 | 'enabled', |
||
68 | 'groups', |
||
69 | 'locked', |
||
70 | 'passwordMustChange', |
||
71 | ]; |
||
72 | |||
73 | /** |
||
74 | * @var array Any additional LDAP attributes to select. |
||
75 | */ |
||
76 | protected $additionalAttributes = []; |
||
77 | |||
78 | /** |
||
79 | * @var string |
||
80 | */ |
||
81 | protected $userClass = LdapUser::class; |
||
82 | |||
83 | /** |
||
84 | * @var bool Whether or not to check group membership recursively when checking role membership. |
||
85 | */ |
||
86 | protected $checkGroupsRecursively; |
||
87 | |||
88 | /** |
||
89 | * @var string|null The default role to be assigned to a user. |
||
90 | */ |
||
91 | protected $defaultRole; |
||
92 | |||
93 | /** |
||
94 | * @var string The object type to search LDAP for. |
||
95 | */ |
||
96 | protected $ldapObjectType = LdapObjectType::USER; |
||
97 | |||
98 | /** |
||
99 | * @var string The group object type when searching group membership. |
||
100 | */ |
||
101 | protected $groupObjectType = LdapObjectType::GROUP; |
||
102 | |||
103 | /** |
||
104 | * @var string The container/OU to search for the user under. |
||
105 | */ |
||
106 | protected $searchBase; |
||
107 | |||
108 | /** |
||
109 | * @var bool Whether or not user attributes should be re-queried on a refresh. |
||
110 | */ |
||
111 | protected $refreshAttributes = true; |
||
112 | |||
113 | /** |
||
114 | * @var bool Whether or not user roles should be re-queried on a refresh. |
||
115 | */ |
||
116 | protected $refreshRoles = true; |
||
117 | |||
118 | /** |
||
119 | * @param LdapManager $ldap |
||
120 | * @param EventDispatcherInterface $dispatcher |
||
121 | * @param array $roleMap |
||
122 | * @param bool $checkGroupsRecursively |
||
123 | */ |
||
124 | public function __construct(LdapManager $ldap, EventDispatcherInterface $dispatcher, array $roleMap, $checkGroupsRecursively = true) |
||
131 | |||
132 | /** |
||
133 | * Set the default role to add to a LDAP user. |
||
134 | * |
||
135 | * @param string|null $role |
||
136 | */ |
||
137 | public function setDefaultRole($role) |
||
144 | |||
145 | /** |
||
146 | * Set the user class to be instantiated and returned from the LDAP provider. |
||
147 | * |
||
148 | * @param string $class |
||
149 | */ |
||
150 | public function setUserClass($class) |
||
162 | |||
163 | /** |
||
164 | * Set any additional attributes to be selected for the LDAP user. |
||
165 | * |
||
166 | * @param array $attributes |
||
167 | */ |
||
168 | public function setAttributes(array $attributes) |
||
172 | |||
173 | /** |
||
174 | * Set the LDAP object type that will be searched for. |
||
175 | * |
||
176 | * @param string $type |
||
177 | */ |
||
178 | public function setLdapObjectType($type) |
||
182 | |||
183 | /** |
||
184 | * Set the LdapTools object type to search for group membership. |
||
185 | * |
||
186 | * @param string $type |
||
187 | */ |
||
188 | public function setRoleLdapType($type) |
||
192 | |||
193 | /** |
||
194 | * Set the attribute name to LDAP name attributes used in querying LDAP groups for roles. |
||
195 | * |
||
196 | * @param array $map |
||
197 | */ |
||
198 | public function setRoleAttributeMap(array $map) |
||
202 | |||
203 | /** |
||
204 | * @param string $searchBase |
||
205 | */ |
||
206 | public function setSearchBase($searchBase) |
||
210 | |||
211 | /** |
||
212 | * @param bool $refreshRoles |
||
213 | */ |
||
214 | public function setRefreshRoles($refreshRoles) |
||
218 | |||
219 | /** |
||
220 | * @param bool $refreshAttributes |
||
221 | */ |
||
222 | public function setRefreshAttributes($refreshAttributes) |
||
226 | |||
227 | /** |
||
228 | * {@inheritdoc} |
||
229 | */ |
||
230 | public function loadUserByUsername($username) |
||
239 | |||
240 | /** |
||
241 | * {@inheritdoc} |
||
242 | */ |
||
243 | public function refreshUser(UserInterface $user) |
||
261 | |||
262 | /** |
||
263 | * {@inheritdoc} |
||
264 | */ |
||
265 | public function supportsClass($class) |
||
269 | |||
270 | /** |
||
271 | * Search for, and return, the LDAP user by a specific attribute. |
||
272 | * |
||
273 | * @param string $attribute |
||
274 | * @param string $value |
||
275 | * @return LdapObject |
||
276 | */ |
||
277 | protected function getLdapUser($attribute, $value) |
||
294 | |||
295 | /** |
||
296 | * Get all the attributes that should be selected for when querying LDAP. |
||
297 | * |
||
298 | * @return array |
||
299 | */ |
||
300 | protected function getAttributesToSelect() |
||
307 | |||
308 | /** |
||
309 | * Set the roles for the user based on group membership. |
||
310 | * |
||
311 | * @param LdapUserInterface $user |
||
312 | * @return LdapUserInterface |
||
313 | */ |
||
314 | protected function setRolesForUser(LdapUserInterface $user) |
||
332 | |||
333 | /** |
||
334 | * Check all of the groups that are valid for a specific role against all of the LDAP groups that the user belongs |
||
335 | * to. |
||
336 | * |
||
337 | * @param array $roleGroups |
||
338 | * @param LdapObjectCollection $ldapGroups |
||
339 | * @return bool |
||
340 | */ |
||
341 | protected function hasGroupForRoles(array $roleGroups, LdapObjectCollection $ldapGroups) |
||
361 | |||
362 | /** |
||
363 | * Check each LDAP group to see if any of them have an attribute with a specific value. |
||
364 | * |
||
365 | * @param LdapObjectCollection $groups |
||
366 | * @param string $attribute |
||
367 | * @param string $value |
||
368 | * @return bool |
||
369 | */ |
||
370 | protected function hasGroupWithAttributeValue(LdapObjectCollection $groups, $attribute, $value) |
||
383 | |||
384 | /** |
||
385 | * @param LdapUserInterface $user |
||
386 | * @return LdapObjectCollection |
||
387 | */ |
||
388 | protected function getGroupsForUser(LdapUserInterface $user) |
||
405 | |||
406 | /** |
||
407 | * @param LdapObject $ldapObject |
||
408 | * @return LdapUserInterface |
||
409 | */ |
||
410 | protected function constructUserClass(LdapObject $ldapObject) |
||
438 | } |
||
439 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: