| Conditions | 15 |
| Paths | 46 |
| Total Lines | 66 |
| Code Lines | 45 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 1 | 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 |
||
| 30 | function get_data ($item, $user = false) { |
||
| 31 | $user = (int)$user ?: $this->id; |
||
| 32 | if (!$item || $user == User::GUEST_ID) { |
||
| 33 | return false; |
||
| 34 | } |
||
| 35 | $data = $this->cache->{"data/$user"} ?: []; |
||
| 36 | if (is_array($item)) { |
||
| 37 | $result = []; |
||
| 38 | $absent = []; |
||
| 39 | foreach ($item as $i) { |
||
| 40 | if (isset($data[$i])) { |
||
| 41 | $result[$i] = $data[$i]; |
||
| 42 | } else { |
||
| 43 | $absent[] = $i; |
||
| 44 | } |
||
| 45 | } |
||
| 46 | if ($absent) { |
||
| 47 | $absent = implode( |
||
| 48 | ',', |
||
| 49 | $this->db()->s($absent) |
||
| 50 | ); |
||
| 51 | $absent = array_column( |
||
| 52 | $this->db()->qfa( |
||
| 53 | "SELECT `item`, `value` |
||
| 54 | FROM `[prefix]users_data` |
||
| 55 | WHERE |
||
| 56 | `id` = '$user' AND |
||
| 57 | `item` IN($absent)" |
||
| 58 | ), |
||
| 59 | 'value', |
||
| 60 | 'item' |
||
| 61 | ); |
||
| 62 | foreach ($absent as &$a) { |
||
| 63 | $a = _json_decode($a); |
||
| 64 | if ($a === null) { |
||
| 65 | $a = false; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | unset($a); |
||
| 69 | $result += $absent; |
||
| 70 | $data += $absent; |
||
| 71 | $this->cache->{"data/$user"} = $data; |
||
| 72 | } |
||
| 73 | return $result; |
||
| 74 | } |
||
| 75 | if ($data === false || !isset($data[$item])) { |
||
| 76 | if (!is_array($data)) { |
||
| 77 | $data = []; |
||
| 78 | } |
||
| 79 | $data[$item] = _json_decode( |
||
| 80 | $this->db()->qfs( |
||
| 81 | "SELECT `value` |
||
| 82 | FROM `[prefix]users_data` |
||
| 83 | WHERE |
||
| 84 | `id` = '$user' AND |
||
| 85 | `item` = '%s'", |
||
| 86 | $item |
||
| 87 | ) |
||
| 88 | ); |
||
| 89 | if ($data[$item] === null) { |
||
| 90 | $data[$item] = false; |
||
| 91 | } |
||
| 92 | $this->cache->{"data/$user"} = $data; |
||
| 93 | } |
||
| 94 | return $data[$item]; |
||
| 95 | } |
||
| 96 | /** |
||
| 163 |