| 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 |
||
| 203 | private function buildUserFoldersList(array $userInfo): array |
||
| 204 | { |
||
| 205 | //Build tree |
||
| 206 | $tree = new NestedTree(prefixTable('nested_tree'), 'id', 'parent_id', 'title'); |
||
| 207 | |||
| 208 | // Start by adding the manually added folders |
||
| 209 | $allowedFolders = array_map('intval', explode(";", $userInfo['groupes_visibles'])); |
||
| 210 | $readOnlyFolders = []; |
||
| 211 | $allowedFoldersByRoles = []; |
||
| 212 | $restrictedFoldersForItems = []; |
||
| 213 | $foldersLimited = []; |
||
| 214 | $foldersLimitedFull = []; |
||
| 215 | $restrictedItems = []; |
||
| 216 | $personalFolders = []; |
||
| 217 | |||
| 218 | $userFunctionId = str_replace(";", ",", $userInfo['fonction_id']); |
||
| 219 | |||
| 220 | // Get folders from the roles |
||
| 221 | if (empty($userFunctionId) === false) { |
||
| 222 | $rows = $this->select("SELECT * FROM " . prefixTable('roles_values') . " WHERE role_id IN (".$userFunctionId.") AND type IN ('W', 'ND', 'NE', 'NDNE', 'R')"); |
||
| 223 | foreach ($rows as $record) { |
||
| 224 | if ($record['type'] === 'R') { |
||
| 225 | array_push($readOnlyFolders, $record['folder_id']); |
||
| 226 | } elseif (in_array($record['folder_id'], $allowedFolders) === false) { |
||
| 227 | array_push($allowedFoldersByRoles, $record['folder_id']); |
||
| 228 | } |
||
| 229 | } |
||
| 230 | $allowedFoldersByRoles = array_unique($allowedFoldersByRoles); |
||
| 231 | $readOnlyFolders = array_unique($readOnlyFolders); |
||
| 232 | // Clean arrays |
||
| 233 | foreach ($allowedFoldersByRoles as $value) { |
||
| 234 | $key = array_search($value, $readOnlyFolders); |
||
| 235 | if ($key !== false) { |
||
| 236 | unset($readOnlyFolders[$key]); |
||
| 237 | } |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | // Does this user is allowed to see other items |
||
| 242 | $inc = 0; |
||
| 243 | $rows = $this->select("SELECT id, id_tree FROM " . prefixTable('items') . " WHERE restricted_to LIKE '".$userInfo['id']."'". |
||
| 244 | (empty($userFunctionId) === false ? ' AND id_tree NOT IN ('.$userFunctionId.')' : '')); |
||
| 245 | foreach ($rows as $record) { |
||
| 246 | // Exclude restriction on item if folder is fully accessible |
||
| 247 | $restrictedFoldersForItems[$inc] = $record['id_tree']; |
||
| 248 | ++$inc; |
||
| 249 | } |
||
| 250 | |||
| 251 | // Check for the users roles if some specific rights exist on items |
||
| 252 | $rows = $this->select("SELECT i.id_tree, r.item_id |
||
| 253 | FROM " . prefixTable('items') . " as i |
||
| 254 | INNER JOIN " . prefixTable('restriction_to_roles') . " as r ON (r.item_id=i.id) |
||
| 255 | WHERE ".(empty($userFunctionId) === false ? ' id_tree NOT IN ('.$userFunctionId.') AND ' : '')." i.id_tree != '' |
||
| 256 | ORDER BY i.id_tree ASC"); |
||
| 257 | foreach ($rows as $record) { |
||
| 258 | $foldersLimited[$record['id_tree']][$inc] = $record['item_id']; |
||
| 259 | //array_push($foldersLimitedFull, $record['item_id']); |
||
| 260 | array_push($restrictedItems, $record['item_id']); |
||
| 261 | array_push($foldersLimitedFull, $record['id_tree']); |
||
| 262 | ++$inc; |
||
| 263 | } |
||
| 264 | |||
| 265 | // Add all personal folders |
||
| 266 | $rows = $this->select( |
||
| 267 | 'SELECT id |
||
| 268 | FROM ' . prefixTable('nested_tree') . ' |
||
| 269 | WHERE title = '.$userInfo['id'].' AND personal_folder = 1'. |
||
| 270 | (empty($userFunctionId) === false ? ' AND id NOT IN ('.$userFunctionId.')' : ''). |
||
| 271 | ' LIMIT 0,1' |
||
| 272 | ); |
||
| 273 | if (empty($rows['id']) === false) { |
||
| 274 | array_push($personalFolders, $rows['id']); |
||
| 275 | // get all descendants |
||
| 276 | $ids = $tree->getDescendants($rows['id'], false, false, true); |
||
| 277 | foreach ($ids as $id) { |
||
| 278 | array_push($personalFolders, $id); |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | // All folders visibles |
||
| 283 | return [ |
||
| 284 | 'folders' => array_unique( |
||
| 285 | array_filter( |
||
| 286 | array_merge( |
||
| 287 | $allowedFolders, |
||
| 288 | $foldersLimitedFull, |
||
| 289 | $allowedFoldersByRoles, |
||
| 290 | $restrictedFoldersForItems, |
||
| 291 | $readOnlyFolders, |
||
| 292 | $personalFolders |
||
| 293 | ) |
||
| 294 | ) |
||
| 295 | ), |
||
| 296 | 'items' => array_unique($restrictedItems), |
||
| 297 | ]; |
||
| 300 | } |
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.