Conditions | 18 |
Paths | 129 |
Total Lines | 89 |
Code Lines | 53 |
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 |
||
87 | public function process(array &$state): void |
||
88 | { |
||
89 | Assert::keyExists($state, 'Attributes'); |
||
90 | $attributes = &$state['Attributes']; |
||
91 | |||
92 | // perform a merge on the ldap_search_filter |
||
93 | // loop over the attributes and build the search and replace arrays |
||
94 | $arrSearch = $arrReplace = []; |
||
95 | foreach ($attributes as $attr => $val) { |
||
96 | $arrSearch[] = '%' . $attr . '%'; |
||
97 | |||
98 | if (is_array($val) && count($val) > 0 && strlen($val[0]) > 0) { |
||
99 | $arrReplace[] = $this->connector->escapeFilterValue($val[0], true); |
||
|
|||
100 | } else { |
||
101 | $arrReplace[] = ''; |
||
102 | } |
||
103 | } |
||
104 | |||
105 | // merge the attributes into the ldap_search_filter |
||
106 | /** @psalm-var string[] $arrReplace */ |
||
107 | $filter = str_replace($arrSearch, $arrReplace, $this->searchFilter); |
||
108 | if (strpos($filter, '%') !== false) { |
||
109 | Logger::info(sprintf( |
||
110 | '%s: There are non-existing attributes in the search filter. (%s)', |
||
111 | $this->title, |
||
112 | $filter |
||
113 | )); |
||
114 | return; |
||
115 | } |
||
116 | |||
117 | $this->connector->bind($this->searchUsername, $this->searchPassword); |
||
118 | |||
119 | $options = [ |
||
120 | 'scope' => $this->config->getOptionalString('search.scope', Query::SCOPE_SUB), |
||
121 | 'timeout' => $this->config->getOptionalInteger('timeout', 3), |
||
122 | ]; |
||
123 | |||
124 | $entries = $this->connector->searchForMultiple( |
||
125 | $this->searchBase, |
||
126 | $filter, |
||
127 | $options, |
||
128 | true |
||
129 | ); |
||
130 | |||
131 | $results = []; |
||
132 | foreach ($entries as $entry) { |
||
133 | $tmp = array_intersect_key( |
||
134 | $entry->getAttributes(), |
||
135 | array_fill_keys(array_values($this->searchAttributes), null) |
||
136 | ); |
||
137 | |||
138 | $binaries = array_intersect( |
||
139 | array_keys($tmp), |
||
140 | $this->binaryAttributes, |
||
141 | ); |
||
142 | foreach ($binaries as $binary) { |
||
143 | /** @psalm-var array $attr */ |
||
144 | $attr = $entry->getAttribute($binary); |
||
145 | $tmp[$binary] = array_map('base64_encode', $attr); |
||
146 | } |
||
147 | |||
148 | $results[] = $tmp; |
||
149 | } |
||
150 | |||
151 | // handle [multiple] values |
||
152 | foreach ($results as $result) { |
||
153 | foreach ($this->searchAttributes as $target => $name) { |
||
154 | // If there is no mapping defined, just use the name of the LDAP-attribute as a target |
||
155 | if (is_int($target)) { |
||
156 | $target = $name; |
||
157 | } |
||
158 | |||
159 | if (isset($attributes[$target]) && $this->attrPolicy === 'replace') { |
||
160 | unset($attributes[$target]); |
||
161 | } |
||
162 | |||
163 | if (isset($result[$name])) { |
||
164 | if (isset($attributes[$target])) { |
||
165 | foreach (array_values($result[$name]) as $value) { |
||
166 | if ($this->attrPolicy === 'merge') { |
||
167 | if (!in_array($value, $attributes[$target], true)) { |
||
168 | $attributes[$target][] = $value; |
||
169 | } |
||
170 | } else { |
||
171 | $attributes[$target][] = $value; |
||
172 | } |
||
173 | } |
||
174 | } else { |
||
175 | $attributes[$target] = array_values($result[$name]); |
||
176 | } |
||
182 |