Complex classes like Ldap 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 Ldap, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | class Ldap extends AbstractEndpoint |
||
26 | { |
||
27 | use LoggerTrait; |
||
28 | |||
29 | /** |
||
30 | * Kind. |
||
31 | */ |
||
32 | public const KIND = 'LdapEndpoint'; |
||
33 | |||
34 | /** |
||
35 | * Ldap. |
||
36 | * |
||
37 | * @var Ldap |
||
38 | */ |
||
39 | protected $ldap; |
||
40 | |||
41 | /** |
||
42 | * Uri. |
||
43 | * |
||
44 | * @var string |
||
45 | */ |
||
46 | protected $uri = 'ldap://127.0.0.1:389'; |
||
47 | |||
48 | /** |
||
49 | * Binddn. |
||
50 | * |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $binddn; |
||
54 | |||
55 | /** |
||
56 | * Bindpw. |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | protected $bindpw; |
||
61 | |||
62 | /** |
||
63 | * Basedn. |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | protected $basedn = ''; |
||
68 | |||
69 | /** |
||
70 | * tls. |
||
71 | * |
||
72 | * @var bool |
||
73 | */ |
||
74 | protected $tls = false; |
||
75 | |||
76 | /** |
||
77 | * Options. |
||
78 | * |
||
79 | * @var array |
||
80 | */ |
||
81 | protected $options = []; |
||
82 | |||
83 | /** |
||
84 | * Init endpoint. |
||
85 | */ |
||
86 | 32 | public function __construct(string $name, string $type, LdapServer $ldap, CollectionInterface $collection, WorkflowFactory $workflow, LoggerInterface $logger, array $resource = []) |
|
97 | |||
98 | /** |
||
99 | * {@inheritdoc} |
||
100 | */ |
||
101 | 4 | public function setup(bool $simulate = false): EndpointInterface |
|
137 | |||
138 | /** |
||
139 | * {@inheritdoc} |
||
140 | */ |
||
141 | 3 | public function setLdapOptions(?array $config = null): EndpointInterface |
|
142 | { |
||
143 | 3 | if ($config === null) { |
|
144 | return $this; |
||
145 | } |
||
146 | |||
147 | 3 | foreach ($config as $option => $value) { |
|
148 | switch ($option) { |
||
149 | 3 | case 'options': |
|
150 | 1 | $this->options = $value; |
|
151 | |||
152 | 1 | break; |
|
153 | 2 | case 'uri': |
|
154 | 2 | case 'binddn': |
|
155 | 1 | case 'bindpw': |
|
156 | 1 | case 'basedn': |
|
157 | 1 | $this->{$option} = (string) $value; |
|
158 | |||
159 | 1 | break; |
|
160 | 1 | case 'tls': |
|
161 | 1 | $this->tls = (bool) $value; |
|
162 | |||
163 | 1 | break; |
|
164 | default: |
||
165 | throw new InvalidArgumentException('unknown ldap option '.$option.' given'); |
||
166 | } |
||
167 | } |
||
168 | |||
169 | 3 | return $this; |
|
170 | } |
||
171 | |||
172 | /** |
||
173 | * {@inheritdoc} |
||
174 | */ |
||
175 | 1 | public function shutdown(bool $simulate = false): EndpointInterface |
|
181 | |||
182 | /** |
||
183 | * {@inheritdoc} |
||
184 | */ |
||
185 | 4 | public function change(AttributeMapInterface $map, array $diff, array $object, EndpointObjectInterface $endpoint_object, bool $simulate = false): ?string |
|
211 | |||
212 | /** |
||
213 | * {@inheritdoc} |
||
214 | */ |
||
215 | 2 | public function delete(AttributeMapInterface $map, array $object, EndpointObjectInterface $endpoint_object, bool $simulate = false): bool |
|
227 | |||
228 | /** |
||
229 | * {@inheritdoc} |
||
230 | */ |
||
231 | 3 | public function create(AttributeMapInterface $map, array $object, bool $simulate = false): ?string |
|
249 | |||
250 | /** |
||
251 | * {@inheritdoc} |
||
252 | */ |
||
253 | 11 | public function transformQuery(?array $query = null) |
|
273 | |||
274 | /** |
||
275 | * {@inheritdoc} |
||
276 | */ |
||
277 | public function count(?array $query = null): int |
||
283 | |||
284 | /** |
||
285 | * {@inheritdoc} |
||
286 | */ |
||
287 | public function getAll(?array $query = null): Generator |
||
304 | |||
305 | /** |
||
306 | * {@inheritdoc} |
||
307 | */ |
||
308 | 6 | public function getDiff(AttributeMapInterface $map, array $diff): array |
|
347 | |||
348 | /** |
||
349 | * {@inheritdoc} |
||
350 | */ |
||
351 | 4 | public function getOne(array $object, ?array $attributes = []): EndpointObjectInterface |
|
368 | |||
369 | /** |
||
370 | * Normalize dn. |
||
371 | */ |
||
372 | 8 | protected function normalizeDn(string $dn): string |
|
384 | |||
385 | /** |
||
386 | * Prepare object. |
||
387 | */ |
||
388 | 2 | protected function prepareRawObject(array $result): array |
|
422 | |||
423 | /** |
||
424 | * Move ldap object. |
||
425 | */ |
||
426 | 1 | protected function moveLdapObject(string $new_dn, string $current_dn, bool $simulate = false): bool |
|
442 | |||
443 | /** |
||
444 | * Get dn. |
||
445 | */ |
||
446 | 9 | protected function getDn(array $object, array $endpoint_object = []): string |
|
457 | } |
||
458 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..