| Conditions | 19 |
| Paths | 109 |
| Total Lines | 94 |
| Code Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 3 | ||
| 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 |
||
| 105 | public function process(array &$request): void |
||
| 106 | { |
||
| 107 | Assert::keyExists($request, 'Attributes'); |
||
| 108 | |||
| 109 | $attributes = &$request['Attributes']; |
||
| 110 | |||
| 111 | // skip if any of if_missing_attributes is set |
||
| 112 | foreach($this->if_missing_attributes as $attribute) { |
||
| 113 | if (isset($attributes[$attribute])) { |
||
| 114 | \SimpleSAML\Logger::debug( |
||
| 115 | 'AttributeAddFromLDAP: ' |
||
| 116 | . 'skipping because ' . $attribute . ' is present' |
||
| 117 | ); |
||
| 118 | return; |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | // perform a merge on the ldap_search_filter |
||
| 123 | // loop over the attributes and build the search and replace arrays |
||
| 124 | $arrSearch = []; |
||
| 125 | $arrReplace = []; |
||
| 126 | foreach ($attributes as $attr => $val) { |
||
| 127 | $arrSearch[] = '%' . $attr . '%'; |
||
| 128 | |||
| 129 | if (strlen($val[0]) > 0) { |
||
| 130 | $arrReplace[] = Ldap::escapeFilterValue($val[0]); |
||
| 131 | } else { |
||
| 132 | $arrReplace[] = ''; |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | // merge the attributes into the ldap_search_filter |
||
| 137 | $filter = str_replace($arrSearch, $arrReplace, $this->search_filter); |
||
|
|
|||
| 138 | |||
| 139 | if (strpos($filter, '%') !== false) { |
||
| 140 | Logger::info( |
||
| 141 | 'AttributeAddFromLDAP: There are non-existing attributes in the search filter. (' . |
||
| 142 | $this->search_filter . ')' |
||
| 143 | ); |
||
| 144 | return; |
||
| 145 | } |
||
| 146 | |||
| 147 | if (!in_array($this->attr_policy, ['merge', 'replace', 'add'], true)) { |
||
| 148 | Logger::warning("AttributeAddFromLDAP: 'attribute.policy' must be one of 'merge'," . |
||
| 149 | "'replace' or 'add'."); |
||
| 150 | return; |
||
| 151 | } |
||
| 152 | |||
| 153 | // getLdap |
||
| 154 | try { |
||
| 155 | $ldap = $this->getLdap(); |
||
| 156 | } catch (\Exception $e) { |
||
| 157 | // Added this warning in case $this->getLdap() fails |
||
| 158 | Logger::warning("AttributeAddFromLDAP: exception = " . $e); |
||
| 159 | return; |
||
| 160 | } |
||
| 161 | // search for matching entries |
||
| 162 | try { |
||
| 163 | $entries = $ldap->searchformultiple( |
||
| 164 | $this->base_dn, |
||
| 165 | $filter, |
||
| 166 | array_values($this->search_attributes), |
||
| 167 | true, |
||
| 168 | false |
||
| 169 | ); |
||
| 170 | } catch (\Exception $e) { |
||
| 171 | return; // silent fail, error is still logged by LDAP search |
||
| 172 | } |
||
| 173 | |||
| 174 | // handle [multiple] values |
||
| 175 | foreach ($entries as $entry) { |
||
| 176 | foreach ($this->search_attributes as $target => $name) { |
||
| 177 | if (is_numeric($target)) { |
||
| 178 | $target = $name; |
||
| 179 | } |
||
| 180 | |||
| 181 | if (isset($attributes[$target]) && $this->attr_policy === 'replace') { |
||
| 182 | unset($attributes[$target]); |
||
| 183 | } |
||
| 184 | $name = strtolower($name); |
||
| 185 | if (isset($entry[$name])) { |
||
| 186 | unset($entry[$name]['count']); |
||
| 187 | if (isset($attributes[$target])) { |
||
| 188 | foreach (array_values($entry[$name]) as $value) { |
||
| 189 | if ($this->attr_policy === 'merge') { |
||
| 190 | if (!in_array($value, $attributes[$target], true)) { |
||
| 191 | $attributes[$target][] = $value; |
||
| 192 | } |
||
| 193 | } else { |
||
| 194 | $attributes[$target][] = $value; |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } else { |
||
| 198 | $attributes[$target] = array_values($entry[$name]); |
||
| 199 | } |
||
| 205 |