| Total Complexity | 46 |
| Total Lines | 260 |
| 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 | public function __construct(Ldap $ldap, string $filter, string $baseDn = null, array $returnAttributes = null, $pageSize = 250, bool $resolveRangedAttributes = false) |
||
| 49 | { |
||
| 50 | $this->ldap = $ldap; |
||
| 51 | $this->filter = $filter; |
||
| 52 | $this->baseDn = $baseDn; |
||
| 53 | $this->returnAttributes = $returnAttributes; |
||
| 54 | $this->pageSize = $pageSize; |
||
| 55 | $this->resolveRangedAttributes = $resolveRangedAttributes; |
||
| 56 | } |
||
| 57 | |||
| 58 | private function getLdap() |
||
| 59 | { |
||
| 60 | return $this->ldap; |
||
| 61 | } |
||
| 62 | |||
| 63 | private function getFilter() |
||
| 64 | { |
||
| 65 | return $this->filter; |
||
| 66 | } |
||
| 67 | |||
| 68 | private function getBaseDn() |
||
| 71 | } |
||
| 72 | |||
| 73 | private function getReturnAttributes() |
||
| 74 | { |
||
| 75 | return $this->returnAttributes; |
||
| 76 | } |
||
| 77 | |||
| 78 | private function getPageSize() |
||
| 79 | { |
||
| 80 | return $this->pageSize; |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @return bool |
||
| 85 | */ |
||
| 86 | private function getResolveRangedAttributes() |
||
| 87 | { |
||
| 88 | return $this->resolveRangedAttributes; |
||
| 89 | } |
||
| 90 | |||
| 91 | private function fetchPagedResult() |
||
| 92 | { |
||
| 93 | if ($this->cookie === null || $this->cookie === '') { |
||
| 94 | return false; |
||
| 95 | } |
||
| 96 | |||
| 97 | if ($this->cookie === true) { |
||
| 98 | // First fetch! |
||
| 99 | $this->cookie = ''; |
||
| 100 | } |
||
| 101 | |||
| 102 | $ldap = $this->getLdap(); |
||
| 103 | $resource = $ldap->getResource(); |
||
| 104 | |||
| 105 | $baseDn = $this->getBaseDn(); |
||
| 106 | if (!$baseDn) { |
||
| 107 | $baseDn = $ldap->getBaseDn(); |
||
| 108 | } |
||
| 109 | |||
| 110 | ldap_control_paged_result($resource, $this->getPageSize(), true, $this->cookie); |
||
| 111 | if ($this->getReturnAttributes() !== null) { |
||
| 112 | $resultResource = ldap_search($resource, $baseDn, $this->getFilter(), $this->getReturnAttributes()); |
||
| 113 | } else { |
||
| 114 | $resultResource = ldap_search($resource, $baseDn, $this->getFilter()); |
||
| 115 | } |
||
| 116 | if (! is_resource($resultResource)) { |
||
| 117 | /* |
||
| 118 | * @TODO better exception msg |
||
| 119 | */ |
||
| 120 | throw new \Exception('ldap_search returned something wrong...' . ldap_error($resource)); |
||
| 121 | } |
||
| 122 | |||
| 123 | $entries = ldap_get_entries($resource, $resultResource); |
||
| 124 | if ($entries === false) { |
||
| 125 | throw new LdapException($ldap, 'Entries could not get fetched'); |
||
| 126 | } |
||
| 127 | $entries = $this->getConvertedEntries($entries); |
||
| 128 | |||
| 129 | ErrorHandler::start(); |
||
| 130 | $response = ldap_control_paged_result_response($resource, $resultResource, $this->cookie); |
||
| 131 | ErrorHandler::stop(); |
||
| 132 | |||
| 133 | if ($response !== true) { |
||
| 134 | throw new LdapException($ldap, 'Paged result was empty'); |
||
| 135 | } |
||
| 136 | |||
| 137 | if ($this->entries === null) { |
||
| 138 | $this->entries = []; |
||
| 139 | } |
||
| 140 | |||
| 141 | $this->entries = array_merge($this->entries, $entries); |
||
| 142 | |||
| 143 | return true; |
||
| 144 | } |
||
| 145 | private function getConvertedEntries(array $entries) |
||
| 146 | { |
||
| 147 | $result = []; |
||
| 148 | |||
| 149 | foreach ($entries as $key => $entry) { |
||
| 150 | if ($key === 'count') { |
||
| 151 | continue; |
||
| 152 | } |
||
| 153 | |||
| 154 | $result[$key] = $this->getConvertedEntry($entry); |
||
| 155 | } |
||
| 156 | |||
| 157 | return $result; |
||
| 158 | } |
||
| 159 | private function getConvertedEntry(array $entry) |
||
| 160 | { |
||
| 161 | $result = []; |
||
| 162 | |||
| 163 | foreach ($entry as $key => $value) { |
||
| 164 | if (is_int($key)) { |
||
| 165 | continue; |
||
| 166 | } |
||
| 167 | if ($key === 'count') { |
||
| 168 | continue; |
||
| 169 | } |
||
| 170 | |||
| 171 | if (isset($value['count'])) { |
||
| 172 | unset($value['count']); |
||
| 173 | } |
||
| 174 | |||
| 175 | $result[$key] = $value; |
||
| 176 | } |
||
| 177 | |||
| 178 | if ($this->getResolveRangedAttributes() === true) { |
||
| 179 | $result = $this->resolveRangedAttributes($result); |
||
| 180 | } |
||
| 181 | |||
| 182 | return $result; |
||
| 183 | } |
||
| 184 | private function resolveRangedAttributes(array $row) |
||
| 185 | { |
||
| 186 | $result = []; |
||
| 187 | foreach ($row as $key => $value) { |
||
| 188 | $keyExploded = explode(';range=', $key); |
||
| 189 | |||
| 190 | if (count($keyExploded) === 2) { |
||
| 191 | $range = explode('-', $keyExploded[1]); |
||
| 192 | $offsetAndLimit = (int) $range[1] + 1; |
||
| 193 | |||
| 194 | $result[$keyExploded[0]] = array_merge($value, $this->getAttributeRecursive($row['dn'], $keyExploded[0], $offsetAndLimit, $offsetAndLimit)); |
||
| 195 | } else { |
||
| 196 | $result[$key] = $value; |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | return $result; |
||
| 201 | } |
||
| 202 | private function getAttributeRecursive(string $dn, string $attrName, int $offset, int $maxPerRequest) |
||
| 234 | } |
||
| 235 | public function current() |
||
| 236 | { |
||
| 237 | if (! is_array($this->current)) { |
||
| 238 | $this->rewind(); |
||
| 239 | } |
||
| 240 | if (! is_array($this->current)) { |
||
| 241 | return; |
||
| 242 | } |
||
| 243 | |||
| 244 | return $this->current; |
||
| 245 | } |
||
| 246 | public function key() |
||
| 256 | } |
||
| 257 | public function next() |
||
| 258 | { |
||
| 259 | // initial |
||
| 260 | if ($this->entries === null) { |
||
| 261 | $this->fetchPagedResult(); |
||
| 262 | } |
||
| 263 | |||
| 264 | next($this->entries); |
||
| 265 | |||
| 266 | $this->current = current($this->entries); |
||
| 267 | } |
||
| 268 | public function rewind() |
||
| 269 | { |
||
| 270 | // initial |
||
| 271 | if ($this->entries === null) { |
||
| 272 | $this->fetchPagedResult(); |
||
| 273 | } |
||
| 274 | |||
| 275 | reset($this->entries); |
||
| 276 | $this->current = current($this->entries); |
||
| 277 | } |
||
| 278 | public function valid() |
||
| 285 | } |
||
| 286 | } |
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