Conditions | 14 |
Paths | 16 |
Total Lines | 94 |
Code Lines | 60 |
Lines | 0 |
Ratio | 0 % |
Changes | 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 |
||
159 | private function buildUserFoldersList(array $userInfo): array |
||
160 | { |
||
161 | //Build tree |
||
162 | $tree = new NestedTree(prefixTable('nested_tree'), 'id', 'parent_id', 'title'); |
||
163 | |||
164 | // Start by adding the manually added folders |
||
165 | $allowedFolders = explode(";", $userInfo['groupes_visibles']); |
||
166 | $readOnlyFolders = []; |
||
167 | $allowedFoldersByRoles = []; |
||
168 | $restrictedFoldersForItems = []; |
||
169 | $foldersLimited = []; |
||
170 | $foldersLimitedFull = []; |
||
171 | $restrictedItems = []; |
||
172 | $personalFolders = []; |
||
173 | |||
174 | $userFunctionId = str_replace(";", ",", $userInfo['fonction_id']); |
||
175 | |||
176 | // Get folders from the roles |
||
177 | if (empty($userFunctionId) === false) { |
||
178 | $rows = $this->select("SELECT * FROM " . prefixTable('roles_values') . " WHERE role_id IN (".$userFunctionId.") AND type IN ('W', 'ND', 'NE', 'NDNE', 'R')"); |
||
179 | foreach ($rows as $record) { |
||
180 | if ($record['type'] === 'R') { |
||
181 | array_push($readOnlyFolders, $record['folder_id']); |
||
182 | } elseif (in_array($record['folder_id'], $allowedFolders) === false) { |
||
183 | array_push($allowedFoldersByRoles, $record['folder_id']); |
||
184 | } |
||
185 | } |
||
186 | $allowedFoldersByRoles = array_unique($allowedFoldersByRoles); |
||
187 | $readOnlyFolders = array_unique($readOnlyFolders); |
||
188 | // Clean arrays |
||
189 | foreach ($allowedFoldersByRoles as $value) { |
||
190 | $key = array_search($value, $readOnlyFolders); |
||
191 | if ($key !== false) { |
||
192 | unset($readOnlyFolders[$key]); |
||
193 | } |
||
194 | } |
||
195 | } |
||
196 | |||
197 | // Does this user is allowed to see other items |
||
198 | $inc = 0; |
||
199 | $rows = $this->select("SELECT id, id_tree FROM " . prefixTable('items') . " WHERE restricted_to LIKE '".$userInfo['id']."'". |
||
200 | (empty($userFunctionId) === false ? ' AND id_tree NOT IN ('.$userFunctionId.')' : '')); |
||
201 | foreach ($rows as $record) { |
||
202 | // Exclude restriction on item if folder is fully accessible |
||
203 | $restrictedFoldersForItems[$inc] = $record['id_tree']; |
||
204 | ++$inc; |
||
205 | } |
||
206 | |||
207 | // Check for the users roles if some specific rights exist on items |
||
208 | $rows = $this->select("SELECT i.id_tree, r.item_id |
||
209 | FROM " . prefixTable('items') . " as i |
||
210 | INNER JOIN " . prefixTable('restriction_to_roles') . " as r ON (r.item_id=i.id) |
||
211 | WHERE ".(empty($userFunctionId) === false ? ' id_tree NOT IN ('.$userFunctionId.') AND ' : '')." i.id_tree != '' |
||
212 | ORDER BY i.id_tree ASC"); |
||
213 | foreach ($rows as $record) { |
||
214 | $foldersLimited[$record['id_tree']][$inc] = $record['item_id']; |
||
215 | //array_push($foldersLimitedFull, $record['item_id']); |
||
216 | array_push($restrictedItems, $record['item_id']); |
||
217 | array_push($foldersLimitedFull, $record['id_tree']); |
||
218 | ++$inc; |
||
219 | } |
||
220 | |||
221 | // Add all personal folders |
||
222 | $rows = $this->select( |
||
223 | 'SELECT id |
||
224 | FROM ' . prefixTable('nested_tree') . ' |
||
225 | WHERE title = '.$userInfo['id'].' AND personal_folder = 1'. |
||
226 | (empty($userFunctionId) === false ? ' AND id NOT IN ('.$userFunctionId.')' : ''). |
||
227 | ' LIMIT 0,1' |
||
228 | ); |
||
229 | if (empty($rows['id']) === false) { |
||
230 | array_push($personalFolders, $rows['id']); |
||
231 | // get all descendants |
||
232 | $ids = $tree->getDescendants($rows['id'], false, false, true); |
||
233 | foreach ($ids as $id) { |
||
234 | array_push($personalFolders, $id); |
||
235 | } |
||
236 | } |
||
237 | |||
238 | // All folders visibles |
||
239 | return [ |
||
240 | 'folders' => array_unique( |
||
241 | array_filter( |
||
242 | array_merge( |
||
243 | $allowedFolders, |
||
244 | $foldersLimitedFull, |
||
245 | $allowedFoldersByRoles, |
||
246 | $restrictedFoldersForItems, |
||
247 | $readOnlyFolders, |
||
248 | $personalFolders |
||
249 | ) |
||
250 | ) |
||
251 | ), |
||
252 | 'items' => array_unique($restrictedItems), |
||
253 | ]; |
||
256 | } |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.