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 | /** |
||
28 | * Kind. |
||
29 | */ |
||
30 | public const KIND = 'LdapEndpoint'; |
||
31 | |||
32 | /** |
||
33 | * Ldap. |
||
34 | * |
||
35 | * @var Ldap |
||
36 | */ |
||
37 | protected $ldap; |
||
38 | |||
39 | /** |
||
40 | * Uri. |
||
41 | * |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $uri = 'ldap://127.0.0.1:389'; |
||
45 | |||
46 | /** |
||
47 | * Binddn. |
||
48 | * |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $binddn; |
||
52 | |||
53 | /** |
||
54 | * Bindpw. |
||
55 | * |
||
56 | * @var string |
||
57 | */ |
||
58 | protected $bindpw; |
||
59 | |||
60 | /** |
||
61 | * Basedn. |
||
62 | * |
||
63 | * @var string |
||
64 | */ |
||
65 | protected $basedn = ''; |
||
66 | |||
67 | /** |
||
68 | * tls. |
||
69 | * |
||
70 | * @var bool |
||
71 | */ |
||
72 | protected $tls = false; |
||
73 | |||
74 | /** |
||
75 | * Options. |
||
76 | * |
||
77 | * @var array |
||
78 | */ |
||
79 | protected $options = []; |
||
80 | |||
81 | /** |
||
82 | * Init endpoint. |
||
83 | */ |
||
84 | 29 | public function __construct(string $name, string $type, LdapServer $ldap, CollectionInterface $collection, WorkflowFactory $workflow, LoggerInterface $logger, array $resource = []) |
|
95 | |||
96 | /** |
||
97 | * {@inheritdoc} |
||
98 | */ |
||
99 | 4 | public function setup(bool $simulate = false): EndpointInterface |
|
135 | |||
136 | /** |
||
137 | * {@inheritdoc} |
||
138 | */ |
||
139 | 3 | public function setLdapOptions(?array $config = null): EndpointInterface |
|
140 | { |
||
141 | 3 | if ($config === null) { |
|
142 | return $this; |
||
143 | } |
||
144 | |||
145 | 3 | foreach ($config as $option => $value) { |
|
146 | 3 | switch ($option) { |
|
147 | 3 | case 'options': |
|
148 | 1 | $this->options = $value; |
|
149 | |||
150 | 1 | break; |
|
151 | 2 | case 'uri': |
|
152 | 2 | case 'binddn': |
|
153 | 1 | case 'bindpw': |
|
154 | 1 | case 'basedn': |
|
155 | 1 | $this->{$option} = (string) $value; |
|
156 | |||
157 | 1 | break; |
|
158 | 1 | case 'tls': |
|
159 | 1 | $this->tls = (bool) $value; |
|
160 | |||
161 | 1 | break; |
|
162 | default: |
||
163 | 3 | throw new InvalidArgumentException('unknown ldap option '.$option.' given'); |
|
164 | } |
||
165 | } |
||
166 | |||
167 | 3 | return $this; |
|
168 | } |
||
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | */ |
||
173 | 1 | public function shutdown(bool $simulate = false): EndpointInterface |
|
179 | |||
180 | /** |
||
181 | * {@inheritdoc} |
||
182 | */ |
||
183 | 3 | public function change(AttributeMapInterface $map, array $diff, array $object, array $endpoint_object, bool $simulate = false): ?string |
|
215 | |||
216 | /** |
||
217 | * {@inheritdoc} |
||
218 | */ |
||
219 | 2 | public function delete(AttributeMapInterface $map, array $object, array $endpoint_object, bool $simulate = false): bool |
|
232 | |||
233 | /** |
||
234 | * {@inheritdoc} |
||
235 | */ |
||
236 | 3 | public function create(AttributeMapInterface $map, array $object, bool $simulate = false): ?string |
|
257 | |||
258 | /** |
||
259 | * {@inheritdoc} |
||
260 | */ |
||
261 | 8 | public function transformQuery(?array $query = null) |
|
262 | { |
||
263 | 8 | $result = null; |
|
264 | |||
265 | 8 | if ($this->filter_all !== null) { |
|
266 | 1 | $result = $this->filter_all; |
|
267 | } |
||
268 | |||
269 | 8 | if (!empty($query)) { |
|
270 | 8 | if ($this->filter_all === null) { |
|
271 | 7 | $result = QueryTransformer::transform($query); |
|
272 | } else { |
||
273 | 1 | $result = '(&'.$this->filter_all.QueryTransformer::transform($query).')'; |
|
274 | } |
||
275 | } |
||
276 | |||
277 | 8 | return $result; |
|
278 | } |
||
279 | |||
280 | /** |
||
281 | * {@inheritdoc} |
||
282 | */ |
||
283 | public function getAll(?array $query = null): Generator |
||
298 | |||
299 | /** |
||
300 | * {@inheritdoc} |
||
301 | */ |
||
302 | 5 | public function getDiff(AttributeMapInterface $map, array $diff): array |
|
337 | |||
338 | /** |
||
339 | * {@inheritdoc} |
||
340 | */ |
||
341 | 3 | public function getOne(array $object, ?array $attributes = []): EndpointObjectInterface |
|
360 | |||
361 | /** |
||
362 | * Prepare object. |
||
363 | */ |
||
364 | 1 | protected function prepareRawObject(array $result): array |
|
383 | |||
384 | /** |
||
385 | * Move ldap object. |
||
386 | */ |
||
387 | 1 | protected function moveLdapObject(string $new_dn, string $current_dn, bool $simulate = false): bool |
|
403 | |||
404 | /** |
||
405 | * Get dn. |
||
406 | */ |
||
407 | 8 | protected function getDn(array $object, array $endpoint_object = []): string |
|
418 | } |
||
419 |
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..