Total Complexity | 46 |
Total Lines | 268 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like LDAPIterator 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.
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 LDAPIterator, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | final class LDAPIterator implements Iterator |
||
26 | { |
||
27 | private $ldap; |
||
28 | private $filter; |
||
29 | private $baseDn; |
||
30 | private $returnAttributes; |
||
31 | private $pageSize; |
||
32 | private $resolveRangedAttributes; |
||
33 | private $entries; |
||
34 | private $current; |
||
35 | /** |
||
36 | * Required for paging |
||
37 | * |
||
38 | * @var unknown |
||
|
|||
39 | */ |
||
40 | private $currentResult; |
||
41 | /** |
||
42 | * Required for paging |
||
43 | * |
||
44 | * @var unknown |
||
45 | */ |
||
46 | private $cookie = true; |
||
47 | |||
48 | /** |
||
49 | * @param Ldap $ldap The Zend\Ldap\Ldap object that this iterator will use to retrieve results from |
||
50 | * @param string $filter An LDAP search filter (e.g. "(&(objectClass=user)(!(objectClass=computer)))") |
||
51 | * @param string|null $baseDn The Base DN to search from (or null to search from the connection root) |
||
52 | * @param array|null $returnAttributes The attributes to request from the LDAP server, or null to request all |
||
53 | * @param int $pageSize Number of results per page. This *must* be less than the LDAP server MaxPageSize setting |
||
54 | * @param bool $resolveRangedAttributes Whether or not to return ranged attributes |
||
55 | */ |
||
56 | public function __construct(Ldap $ldap, $filter = "", $baseDn = null, array $returnAttributes = null, $pageSize = 250, $resolveRangedAttributes = false) |
||
57 | { |
||
58 | $this->ldap = $ldap; |
||
59 | $this->filter = $filter; |
||
60 | $this->baseDn = $baseDn; |
||
61 | $this->returnAttributes = $returnAttributes; |
||
62 | $this->pageSize = $pageSize; |
||
63 | $this->resolveRangedAttributes = $resolveRangedAttributes; |
||
64 | } |
||
65 | |||
66 | private function getLdap() |
||
67 | { |
||
68 | return $this->ldap; |
||
69 | } |
||
70 | |||
71 | private function getFilter() |
||
72 | { |
||
73 | return $this->filter; |
||
74 | } |
||
75 | |||
76 | private function getBaseDn() |
||
79 | } |
||
80 | |||
81 | private function getReturnAttributes() |
||
82 | { |
||
83 | return $this->returnAttributes; |
||
84 | } |
||
85 | |||
86 | private function getPageSize() |
||
87 | { |
||
88 | return $this->pageSize; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * @return bool |
||
93 | */ |
||
94 | private function getResolveRangedAttributes() |
||
95 | { |
||
96 | return $this->resolveRangedAttributes; |
||
97 | } |
||
98 | |||
99 | private function fetchPagedResult() |
||
100 | { |
||
101 | if ($this->cookie === null || $this->cookie === '') { |
||
102 | return false; |
||
103 | } |
||
104 | |||
105 | if ($this->cookie === true) { |
||
106 | // First fetch! |
||
107 | $this->cookie = ''; |
||
108 | } |
||
109 | |||
110 | $ldap = $this->getLdap(); |
||
111 | $resource = $ldap->getResource(); |
||
112 | |||
113 | $baseDn = $this->getBaseDn(); |
||
114 | if (!$baseDn) { |
||
115 | $baseDn = $ldap->getBaseDn(); |
||
116 | } |
||
117 | |||
118 | ldap_control_paged_result($resource, $this->getPageSize(), true, $this->cookie); |
||
119 | if ($this->getReturnAttributes() !== null) { |
||
120 | $resultResource = ldap_search($resource, $baseDn, $this->getFilter(), $this->getReturnAttributes()); |
||
121 | } else { |
||
122 | $resultResource = ldap_search($resource, $baseDn, $this->getFilter()); |
||
123 | } |
||
124 | if (! is_resource($resultResource)) { |
||
125 | /* |
||
126 | * @TODO better exception msg |
||
127 | */ |
||
128 | throw new \Exception('ldap_search returned something wrong...' . ldap_error($resource)); |
||
129 | } |
||
130 | |||
131 | $entries = ldap_get_entries($resource, $resultResource); |
||
132 | if ($entries === false) { |
||
133 | throw new LdapException($ldap, 'Entries could not get fetched'); |
||
134 | } |
||
135 | $entries = $this->getConvertedEntries($entries); |
||
136 | |||
137 | ErrorHandler::start(); |
||
138 | $response = ldap_control_paged_result_response($resource, $resultResource, $this->cookie); |
||
139 | ErrorHandler::stop(); |
||
140 | |||
141 | if ($response !== true) { |
||
142 | throw new LdapException($ldap, 'Paged result was empty'); |
||
143 | } |
||
144 | |||
145 | if ($this->entries === null) { |
||
146 | $this->entries = []; |
||
147 | } |
||
148 | |||
149 | $this->entries = array_merge($this->entries, $entries); |
||
150 | |||
151 | return true; |
||
152 | } |
||
153 | private function getConvertedEntries(array $entries) |
||
154 | { |
||
155 | $result = []; |
||
156 | |||
157 | foreach ($entries as $key => $entry) { |
||
158 | if ($key === 'count') { |
||
159 | continue; |
||
160 | } |
||
161 | |||
162 | $result[$key] = $this->getConvertedEntry($entry); |
||
163 | } |
||
164 | |||
165 | return $result; |
||
166 | } |
||
167 | private function getConvertedEntry(array $entry) |
||
168 | { |
||
169 | $result = []; |
||
170 | |||
171 | foreach ($entry as $key => $value) { |
||
172 | if (is_int($key)) { |
||
173 | continue; |
||
174 | } |
||
175 | if ($key === 'count') { |
||
176 | continue; |
||
177 | } |
||
178 | |||
179 | if (isset($value['count'])) { |
||
180 | unset($value['count']); |
||
181 | } |
||
182 | |||
183 | $result[$key] = $value; |
||
184 | } |
||
185 | |||
186 | if ($this->getResolveRangedAttributes() === true) { |
||
187 | $result = $this->resolveRangedAttributes($result); |
||
188 | } |
||
189 | |||
190 | return $result; |
||
191 | } |
||
192 | private function resolveRangedAttributes(array $row) |
||
193 | { |
||
194 | $result = []; |
||
195 | foreach ($row as $key => $value) { |
||
196 | $keyExploded = explode(';range=', $key); |
||
197 | |||
198 | if (count($keyExploded) === 2) { |
||
199 | $range = explode('-', $keyExploded[1]); |
||
200 | $offsetAndLimit = (int) $range[1] + 1; |
||
201 | |||
202 | $result[$keyExploded[0]] = array_merge($value, $this->getAttributeRecursive($row['dn'], $keyExploded[0], $offsetAndLimit, $offsetAndLimit)); |
||
203 | } else { |
||
204 | $result[$key] = $value; |
||
205 | } |
||
206 | } |
||
207 | |||
208 | return $result; |
||
209 | } |
||
210 | private function getAttributeRecursive(string $dn, string $attrName, int $offset, int $maxPerRequest) |
||
242 | } |
||
243 | public function current() |
||
244 | { |
||
245 | if (! is_array($this->current)) { |
||
246 | $this->rewind(); |
||
247 | } |
||
248 | if (! is_array($this->current)) { |
||
249 | return; |
||
250 | } |
||
251 | |||
252 | return $this->current; |
||
253 | } |
||
254 | public function key() |
||
264 | } |
||
265 | public function next() |
||
266 | { |
||
267 | // initial |
||
268 | if ($this->entries === null) { |
||
269 | $this->fetchPagedResult(); |
||
270 | } |
||
271 | |||
272 | next($this->entries); |
||
273 | |||
274 | $this->current = current($this->entries); |
||
275 | } |
||
276 | public function rewind() |
||
277 | { |
||
278 | // initial |
||
279 | if ($this->entries === null) { |
||
280 | $this->fetchPagedResult(); |
||
281 | } |
||
282 | |||
283 | reset($this->entries); |
||
284 | $this->current = current($this->entries); |
||
285 | } |
||
286 | public function valid() |
||
293 | } |
||
294 | } |
||
295 |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths