| Conditions | 8 |
| Paths | 9 |
| Total Lines | 95 |
| Code Lines | 58 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| 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 |
||
| 41 | public function getItems(string $sqlExtra, int $limit, string $userPrivateKey, int $userId): array |
||
| 42 | { |
||
| 43 | $rows = $this->select( |
||
| 44 | "SELECT id, label, description, pw, url, id_tree, login, email, viewed_no, fa_icon, inactif, perso |
||
| 45 | FROM ".prefixTable('items') |
||
| 46 | |||
| 47 | . $sqlExtra . " ORDER BY id ASC" . |
||
| 48 | ($limit > 0 ? " LIMIT ?". ["i", $limit] : '') |
||
|
|
|||
| 49 | ); |
||
| 50 | |||
| 51 | $ret = []; |
||
| 52 | |||
| 53 | foreach ($rows as $row) { |
||
| 54 | $userKey = $this->select( |
||
| 55 | 'SELECT share_key |
||
| 56 | FROM ' . prefixTable('sharekeys_items') . ' |
||
| 57 | WHERE user_id = '.$userId.' AND object_id = '.$row['id'] |
||
| 58 | ); |
||
| 59 | if (count($userKey) === 0 || empty($row['pw']) === true) { |
||
| 60 | // No share key found |
||
| 61 | $pwd = ''; |
||
| 62 | } else { |
||
| 63 | $pwd = base64_decode(doDataDecryption( |
||
| 64 | $row['pw'], |
||
| 65 | decryptUserObjectKey( |
||
| 66 | $userKey[0]['share_key'], |
||
| 67 | $userPrivateKey |
||
| 68 | ) |
||
| 69 | )); |
||
| 70 | } |
||
| 71 | |||
| 72 | $champs = $this->select( |
||
| 73 | 'SELECT c.title, ci.data |
||
| 74 | FROM ' . prefixTable('categories_items') . ' AS ci |
||
| 75 | INNER JOIN ' . prefixTable('categories') . ' AS c |
||
| 76 | ON ci.field_id = c.id |
||
| 77 | WHERE item_id = '. $row['id'] |
||
| 78 | |||
| 79 | |||
| 80 | |||
| 81 | |||
| 82 | |||
| 83 | |||
| 84 | |||
| 85 | |||
| 86 | |||
| 87 | ); |
||
| 88 | |||
| 89 | if ((int) $row['inactif'] === 0) { |
||
| 90 | if (count($champs) === 0) { |
||
| 91 | array_push( |
||
| 92 | $ret, |
||
| 93 | [ |
||
| 94 | 'id' => (int) $row['id'], |
||
| 95 | 'label' => $row['label'], |
||
| 96 | 'description' => $row['description'], |
||
| 97 | 'pwd' => $pwd, |
||
| 98 | 'url' => $row['url'], |
||
| 99 | 'login' => $row['login'], |
||
| 100 | 'email' => $row['email'], |
||
| 101 | 'viewed_no' => (int) $row['viewed_no'], |
||
| 102 | 'fa_icon' => $row['fa_icon'], |
||
| 103 | 'inactif' => (int) $row['inactif'], |
||
| 104 | 'perso' => (int) $row['perso'] |
||
| 105 | ] |
||
| 106 | ); |
||
| 107 | } else { |
||
| 108 | $retChamps = array(); |
||
| 109 | |||
| 110 | foreach ($champs as $champ) { |
||
| 111 | $retChamps[$champ['title']] = $champ['data']; |
||
| 112 | } |
||
| 113 | |||
| 114 | array_push( |
||
| 115 | $ret, |
||
| 116 | [ |
||
| 117 | 'id' => (int) $row['id'], |
||
| 118 | 'label' => $row['label'], |
||
| 119 | 'description' => $row['description'], |
||
| 120 | 'pwd' => $pwd, |
||
| 121 | 'url' => $row['url'], |
||
| 122 | 'login' => $row['login'], |
||
| 123 | 'email' => $row['email'], |
||
| 124 | 'viewed_no' => (int) $row['viewed_no'], |
||
| 125 | 'fa_icon' => $row['fa_icon'], |
||
| 126 | 'inactif' => (int) $row['inactif'], |
||
| 127 | 'perso' => (int) $row['perso'], |
||
| 128 | 'champs' => $retChamps |
||
| 129 | ] |
||
| 130 | ); |
||
| 131 | } |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | return $ret; |
||
| 136 | |||
| 216 |