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 |
||
79 | public function updateByForum($forum_id, $types) |
||
80 | { |
||
81 | $forum_id = (int)$forum_id; |
||
82 | if (empty($forum_id)) { |
||
83 | return false; |
||
84 | } |
||
85 | |||
86 | $types_existing = $this->getByForum($forum_id); |
||
87 | $types_valid = []; |
||
88 | $types_add = []; |
||
89 | $types_update = []; |
||
90 | foreach (\array_keys($types_existing) as $key) { |
||
91 | if (empty($types[$key])) { |
||
92 | continue; |
||
93 | } |
||
94 | $types_valid[] = $key; |
||
95 | if ($types[$key] !== $types_existing[$key]['type_order']) { |
||
96 | $types_update[] = $key; |
||
97 | } |
||
98 | } |
||
99 | foreach (\array_keys($types) as $key) { |
||
100 | if (!empty($types[$key]) && !isset($types_existing[$key])) { |
||
101 | $types_add[] = $key; |
||
102 | } |
||
103 | } |
||
104 | $types_valid = \array_filter($types_valid); |
||
105 | $types_add = \array_filter($types_add); |
||
106 | $types_update = \array_filter($types_update); |
||
107 | |||
108 | if (!empty($types_valid)) { |
||
109 | $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 |
||
110 | " {$this->keyName} NOT IN (" . \implode(', ', $types_valid) . ')'; |
||
111 | if (false === ($result = $this->db->queryF($sql))) { |
||
112 | } |
||
113 | } |
||
114 | |||
115 | if (!empty($types_update)) { |
||
116 | $type_query = []; |
||
117 | foreach ($types_update as $key) { |
||
118 | $order = $types[$key]; |
||
119 | if ($types_existing[$key]['type_order'] == $order) { |
||
120 | continue; |
||
121 | } |
||
122 | $sql = 'UPDATE ' . $this->db->prefix('newbb_type_forum') . " SET type_order = {$order}" . " WHERE {$this->keyName} = {$key} AND forum_id = {$forum_id}"; |
||
123 | if (false === ($result = $this->db->queryF($sql))) { |
||
124 | } |
||
125 | } |
||
126 | } |
||
127 | |||
128 | if (!empty($types_add)) { |
||
129 | $type_query = []; |
||
130 | foreach ($types_add as $key) { |
||
131 | $order = $types[$key]; |
||
132 | //if (!in_array($key, $types_add)) continue; |
||
133 | $type_query[] = "({$key}, {$forum_id}, {$order})"; |
||
134 | } |
||
135 | $sql = 'INSERT INTO ' . $this->db->prefix('newbb_type_forum') . ' (type_id, forum_id, type_order) ' . ' VALUES ' . \implode(', ', $type_query); |
||
136 | if (false === ($result = $this->db->queryF($sql))) { |
||
137 | //xoops_error($this->db->error()); |
||
138 | } |
||
139 | } |
||
140 | |||
141 | return true; |
||
142 | } |
||
198 |