| Conditions | 17 |
| Paths | 217 |
| Total Lines | 63 |
| Code Lines | 39 |
| 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 declare(strict_types=1); |
||
| 74 | public function updateByForum(int $forum_id, array $types): bool |
||
| 75 | { |
||
| 76 | $forum_id = (int)$forum_id; |
||
| 77 | if (empty($forum_id)) { |
||
| 78 | return false; |
||
| 79 | } |
||
| 80 | |||
| 81 | $types_existing = $this->getByForum($forum_id); |
||
| 82 | $types_valid = []; |
||
| 83 | $types_add = []; |
||
| 84 | $types_update = []; |
||
| 85 | foreach (\array_keys($types_existing) as $key) { |
||
| 86 | if (empty($types[$key])) { |
||
| 87 | continue; |
||
| 88 | } |
||
| 89 | $types_valid[] = $key; |
||
| 90 | if ($types[$key] !== $types_existing[$key]['type_order']) { |
||
| 91 | $types_update[] = $key; |
||
| 92 | } |
||
| 93 | } |
||
| 94 | foreach (\array_keys($types) as $key) { |
||
| 95 | if (!empty($types[$key]) && !isset($types_existing[$key])) { |
||
| 96 | $types_add[] = $key; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | $types_valid = \array_filter($types_valid); |
||
| 100 | $types_add = \array_filter($types_add); |
||
| 101 | $types_update = \array_filter($types_update); |
||
| 102 | |||
| 103 | if (!empty($types_valid)) { |
||
| 104 | $sql = 'DELETE FROM ' . $this->db->prefix('newbb_type_forum') . ' WHERE ' . ' forum_id = ' . $forum_id . ' AND ' . // irmtfan bug fix: delete other forums types when update the type for a specific forum |
||
| 105 | " {$this->keyName} NOT IN (" . \implode(', ', $types_valid) . ')'; |
||
| 106 | if (false === ($result = $this->db->queryF($sql))) { |
||
| 107 | } |
||
| 108 | } |
||
| 109 | |||
| 110 | if (!empty($types_update)) { |
||
| 111 | $type_query = []; |
||
| 112 | foreach ($types_update as $key) { |
||
| 113 | $order = $types[$key]; |
||
| 114 | if ($types_existing[$key]['type_order'] == $order) { |
||
| 115 | continue; |
||
| 116 | } |
||
| 117 | $sql = 'UPDATE ' . $this->db->prefix('newbb_type_forum') . " SET type_order = {$order}" . " WHERE {$this->keyName} = {$key} AND forum_id = {$forum_id}"; |
||
| 118 | if (false === ($result = $this->db->queryF($sql))) { |
||
| 119 | } |
||
| 120 | } |
||
| 121 | } |
||
| 122 | |||
| 123 | if (!empty($types_add)) { |
||
| 124 | $type_query = []; |
||
| 125 | foreach ($types_add as $key) { |
||
| 126 | $order = $types[$key]; |
||
| 127 | //if (!in_array($key, $types_add)) continue; |
||
| 128 | $type_query[] = "({$key}, {$forum_id}, {$order})"; |
||
| 129 | } |
||
| 130 | $sql = 'INSERT INTO ' . $this->db->prefix('newbb_type_forum') . ' (type_id, forum_id, type_order) ' . ' VALUES ' . \implode(', ', $type_query); |
||
| 131 | if (false === ($result = $this->db->queryF($sql))) { |
||
| 132 | //xoops_error($this->db->error()); |
||
| 133 | } |
||
| 134 | } |
||
| 135 | |||
| 136 | return true; |
||
| 137 | } |
||
| 193 |