| Conditions | 20 |
| Paths | 129 |
| Total Lines | 95 |
| Code Lines | 57 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 88 | public function process(array &$state): void |
||
| 89 | { |
||
| 90 | Assert::keyExists($state, 'Attributes'); |
||
| 91 | $attributes = &$state['Attributes']; |
||
| 92 | |||
| 93 | // perform a merge on the ldap_search_filter |
||
| 94 | // loop over the attributes and build the search and replace arrays |
||
| 95 | $arrSearch = $arrReplace = []; |
||
| 96 | foreach ($attributes as $attr => $val) { |
||
| 97 | $arrSearch[] = '%' . $attr . '%'; |
||
| 98 | |||
| 99 | if (is_array($val) && count($val) > 0 && is_string($val[0]) && strlen($val[0]) > 0) { |
||
| 100 | $arrReplace[] = $this->connector->escapeFilterValue($val[0], true); |
||
|
|
|||
| 101 | } else { |
||
| 102 | $arrReplace[] = ''; |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | // merge the attributes into the ldap_search_filter |
||
| 107 | /** @psalm-var string[] $arrReplace */ |
||
| 108 | $filter = str_replace($arrSearch, $arrReplace, $this->searchFilter); |
||
| 109 | if (strpos($filter, '%') !== false) { |
||
| 110 | Logger::info(sprintf( |
||
| 111 | '%s: There are non-existing attributes in the search filter. (%s)', |
||
| 112 | $this->title, |
||
| 113 | $filter, |
||
| 114 | )); |
||
| 115 | return; |
||
| 116 | } |
||
| 117 | |||
| 118 | $this->connector->bind($this->searchUsername, $this->searchPassword); |
||
| 119 | |||
| 120 | $wantedAttrs = $this->config->getOptionalValue( |
||
| 121 | 'attributes', |
||
| 122 | // If specifically set to NULL return all attributes, if not set at all return nothing (safe default) |
||
| 123 | in_array('attributes', $this->config->getOptions(), true) ? ['*'] : [], |
||
| 124 | ); |
||
| 125 | |||
| 126 | $options = [ |
||
| 127 | 'scope' => $this->config->getOptionalString('search.scope', Query::SCOPE_SUB), |
||
| 128 | 'timeout' => $this->config->getOptionalInteger('timeout', 3), |
||
| 129 | 'filter' => array_values($wantedAttrs), |
||
| 130 | ]; |
||
| 131 | |||
| 132 | $entries = $this->connector->searchForMultiple( |
||
| 133 | $this->searchBase, |
||
| 134 | $filter, |
||
| 135 | $options, |
||
| 136 | true, |
||
| 137 | ); |
||
| 138 | |||
| 139 | $results = []; |
||
| 140 | foreach ($entries as $entry) { |
||
| 141 | $tmp = array_intersect_key( |
||
| 142 | $entry->getAttributes(), |
||
| 143 | array_fill_keys(array_values($this->searchAttributes), null), |
||
| 144 | ); |
||
| 145 | |||
| 146 | $binaries = array_intersect( |
||
| 147 | array_keys($tmp), |
||
| 148 | $this->binaryAttributes, |
||
| 149 | ); |
||
| 150 | foreach ($binaries as $binary) { |
||
| 151 | $attr = $entry->getAttribute($binary); |
||
| 152 | $tmp[$binary] = array_map('base64_encode', $attr); |
||
| 153 | } |
||
| 154 | |||
| 155 | $results[] = $tmp; |
||
| 156 | } |
||
| 157 | |||
| 158 | // handle [multiple] values |
||
| 159 | foreach ($results as $result) { |
||
| 160 | foreach ($this->searchAttributes as $target => $name) { |
||
| 161 | // If there is no mapping defined, just use the name of the LDAP-attribute as a target |
||
| 162 | if (is_int($target)) { |
||
| 163 | $target = $name; |
||
| 164 | } |
||
| 165 | |||
| 166 | if (isset($attributes[$target]) && $this->attrPolicy === 'replace') { |
||
| 167 | unset($attributes[$target]); |
||
| 168 | } |
||
| 169 | |||
| 170 | if (isset($result[$name])) { |
||
| 171 | if (isset($attributes[$target])) { |
||
| 172 | foreach (array_values($result[$name]) as $value) { |
||
| 173 | if ($this->attrPolicy === 'merge') { |
||
| 174 | if (!in_array($value, $attributes[$target], true)) { |
||
| 175 | $attributes[$target][] = $value; |
||
| 176 | } |
||
| 177 | } else { |
||
| 178 | $attributes[$target][] = $value; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | } else { |
||
| 182 | $attributes[$target] = array_values($result[$name]); |
||
| 183 | } |
||
| 189 |