| Total Complexity | 53 |
| Total Lines | 375 |
| Duplicated Lines | 0 % |
| Changes | 26 | ||
| Bugs | 0 | Features | 0 |
Complex classes like category often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use category, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class category |
||
| 23 | { |
||
| 24 | /** @var \phpbb\cache\driver\driver_interface */ |
||
| 25 | protected $cache; |
||
| 26 | |||
| 27 | /** @var \phpbb\db\driver\driver_interface */ |
||
| 28 | protected $db; |
||
| 29 | |||
| 30 | /** @var \phpbb\config\config */ |
||
| 31 | protected $config; |
||
| 32 | |||
| 33 | /* @var \phpbb\controller\helper */ |
||
| 34 | protected $helper; |
||
| 35 | |||
| 36 | /** @var \phpbb\user */ |
||
| 37 | protected $user; |
||
| 38 | |||
| 39 | /** @var \phpbb\language\language */ |
||
| 40 | protected $language; |
||
| 41 | |||
| 42 | /** @var \phpbb\template\template */ |
||
| 43 | protected $template; |
||
| 44 | |||
| 45 | /** @var \phpbb\extension\manager "Extension Manager" */ |
||
| 46 | protected $ext_manager; |
||
| 47 | |||
| 48 | /** @var \phpbb\log\log */ |
||
| 49 | protected $log; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * The database tables |
||
| 53 | * |
||
| 54 | * @var string */ |
||
| 55 | protected $smilies_category_table; |
||
| 56 | |||
| 57 | /** @var string phpBB root path */ |
||
| 58 | protected $root_path; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Constructor |
||
| 62 | */ |
||
| 63 | public function __construct(cache $cache, db $db, config $config, helper $helper, user $user, language $language, template $template, manager $ext_manager, log $log, $smilies_category_table, $root_path) |
||
| 64 | { |
||
| 65 | $this->cache = $cache; |
||
| 66 | $this->db = $db; |
||
| 67 | $this->config = $config; |
||
| 68 | $this->helper = $helper; |
||
| 69 | $this->user = $user; |
||
| 70 | $this->language = $language; |
||
| 71 | $this->template = $template; |
||
| 72 | $this->ext_manager = $ext_manager; |
||
| 73 | $this->log = $log; |
||
| 74 | $this->smilies_category_table = $smilies_category_table; |
||
| 75 | $this->root_path = $root_path; |
||
| 76 | } |
||
| 77 | |||
| 78 | public function get_version() |
||
| 94 | } |
||
| 95 | |||
| 96 | public function smilies_count($cat, $compact = false) |
||
| 108 | } |
||
| 109 | |||
| 110 | public function capitalize($var) |
||
| 113 | } |
||
| 114 | |||
| 115 | public function get_langs() |
||
| 116 | { |
||
| 117 | $return = []; |
||
| 118 | $sql = 'SELECT * |
||
| 119 | FROM ' . LANG_TABLE; |
||
| 120 | $result = $this->db->sql_query($sql); |
||
| 121 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 122 | { |
||
| 123 | $return[$row['lang_id']] = $row['lang_iso']; |
||
| 124 | } |
||
| 125 | $this->db->sql_freeresult($result); |
||
| 126 | |||
| 127 | return $return; |
||
| 128 | } |
||
| 129 | |||
| 130 | public function verify_cat_langs($langs, $cat, $i, $lang_cat, $ajax) |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | private function cat_to_template($i, $return) |
||
| 152 | { |
||
| 153 | if (($i !== 0) && !empty($return)) |
||
| 154 | { |
||
| 155 | $this->template->assign_block_vars('categories', [ |
||
| 156 | 'ERROR' => true, |
||
| 157 | 'LANG_EMPTY' => $this->language->lang('SC_LANGUAGE_EMPTY', (count($return) > 1) ? 2 : 1) . implode(', ', $return), |
||
| 158 | ]); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | private function cat_to_ajax($i, $return) |
||
| 163 | { |
||
| 164 | $values = [ |
||
| 165 | 'error' => false, |
||
| 166 | 'lang_empty' => '', |
||
| 167 | ]; |
||
| 168 | |||
| 169 | if (($i !== 0) && !empty($return)) |
||
| 170 | { |
||
| 171 | $values = [ |
||
| 172 | 'error' => true, |
||
| 173 | 'lang_empty' => $this->language->lang('SC_LANGUAGE_EMPTY', (count($return) > 1) ? 2 : 1) . implode(', ', $return), |
||
| 174 | ]; |
||
| 175 | } |
||
| 176 | |||
| 177 | return $values; |
||
| 178 | } |
||
| 179 | |||
| 180 | public function category_exist() |
||
| 181 | { |
||
| 182 | $sql = 'SELECT COUNT(cat_id) AS total |
||
| 183 | FROM ' . $this->smilies_category_table . ' |
||
| 184 | ORDER BY cat_order ASC'; |
||
| 185 | $result = $this->db->sql_query_limit($sql, 1); |
||
| 186 | $total = (int) $this->db->sql_fetchfield('total'); |
||
| 187 | $this->db->sql_freeresult($result); |
||
| 188 | |||
| 189 | return $total; |
||
| 190 | } |
||
| 191 | |||
| 192 | public function cat_name($cat) |
||
| 193 | { |
||
| 194 | $cat_name = $this->language->lang('SC_CATEGORY_DEFAUT'); |
||
| 195 | if ($cat > 0) |
||
| 196 | { |
||
| 197 | $lang = (string) $this->user->lang_name; |
||
| 198 | $sql = 'SELECT cat_name |
||
| 199 | FROM ' . $this->smilies_category_table . " |
||
| 200 | WHERE cat_lang = '$lang' |
||
| 201 | AND cat_id = $cat"; |
||
| 202 | $result = $this->db->sql_query_limit($sql, 1); |
||
| 203 | $row = $this->db->sql_fetchrow($result); |
||
| 204 | if (isset($row['cat_name'])) |
||
| 205 | { |
||
| 206 | $cat_name = $row['cat_name']; |
||
| 207 | $this->db->sql_freeresult($result); |
||
| 208 | } |
||
| 209 | else |
||
| 210 | { |
||
| 211 | $sql = 'SELECT cat_name |
||
| 212 | FROM ' . $this->smilies_category_table . " |
||
| 213 | WHERE cat_lang = 'en' |
||
| 214 | AND cat_id = $cat"; |
||
| 215 | $result = $this->db->sql_query_limit($sql, 1); |
||
| 216 | $row = $this->db->sql_fetchrow($result); |
||
| 217 | $cat_name = $row['cat_name']; |
||
| 218 | $this->db->sql_freeresult($result); |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | return $cat_name; |
||
| 223 | } |
||
| 224 | |||
| 225 | public function get_max_order() |
||
| 226 | { |
||
| 227 | // Get max order id... |
||
| 228 | $sql = 'SELECT MAX(cat_order) AS maxi |
||
| 229 | FROM ' . $this->smilies_category_table; |
||
| 230 | $result = $this->db->sql_query($sql); |
||
| 231 | $max = (int) $this->db->sql_fetchfield('maxi'); |
||
| 232 | $this->db->sql_freeresult($result); |
||
| 233 | |||
| 234 | return $max; |
||
| 235 | } |
||
| 236 | |||
| 237 | public function get_max_id() |
||
| 238 | { |
||
| 239 | // Get max id... |
||
| 240 | $sql = 'SELECT MAX(cat_id) AS id_max |
||
| 241 | FROM ' . $this->smilies_category_table; |
||
| 242 | $result = $this->db->sql_query($sql); |
||
| 243 | $id_max = (int) $this->db->sql_fetchfield('id_max'); |
||
| 244 | $this->db->sql_freeresult($result); |
||
| 245 | |||
| 246 | return $id_max; |
||
| 247 | } |
||
| 248 | |||
| 249 | public function get_first_order() |
||
| 250 | { |
||
| 251 | // Get first order id... |
||
| 252 | $sql = 'SELECT cat_order, cat_id |
||
| 253 | FROM ' . $this->smilies_category_table . ' |
||
| 254 | ORDER BY cat_order ASC'; |
||
| 255 | $result = $this->db->sql_query_limit($sql, 1); |
||
| 256 | $first = (int) $this->db->sql_fetchfield('cat_id'); |
||
| 257 | $this->db->sql_freeresult($result); |
||
| 258 | |||
| 259 | return $first; |
||
| 260 | } |
||
| 261 | |||
| 262 | public function get_cat_id($id) |
||
| 263 | { |
||
| 264 | $sql = 'SELECT category |
||
| 265 | FROM ' . SMILIES_TABLE . ' |
||
| 266 | WHERE smiley_id = ' . $id; |
||
| 267 | $result = $this->db->sql_query($sql); |
||
| 268 | $cat = (int) $this->db->sql_fetchfield('category'); |
||
| 269 | $this->db->sql_freeresult($result); |
||
| 270 | |||
| 271 | return $cat; |
||
| 272 | } |
||
| 273 | |||
| 274 | public function get_cat_nb($id) |
||
| 275 | { |
||
| 276 | $sql = 'SELECT cat_nb |
||
| 277 | FROM ' . $this->smilies_category_table . ' |
||
| 278 | WHERE cat_id = ' . (int) $id; |
||
| 279 | $result = $this->db->sql_query($sql); |
||
| 280 | $cat_nb = (int) $this->db->sql_fetchfield('cat_nb'); |
||
| 281 | $this->db->sql_freeresult($result); |
||
| 282 | |||
| 283 | return $cat_nb; |
||
| 284 | } |
||
| 285 | |||
| 286 | public function extract_list_categories($cat) |
||
| 287 | { |
||
| 288 | $title = ''; |
||
| 289 | $cat_order = $i = 0; |
||
| 290 | $lang = (string) $this->user->lang_name; |
||
| 291 | $sql = $this->db->sql_build_query('SELECT', [ |
||
| 292 | 'SELECT' => '*', |
||
| 293 | 'FROM' => [$this->smilies_category_table => ''], |
||
| 294 | 'WHERE' => "cat_nb <> 0 AND cat_lang = '$lang'", |
||
| 295 | 'ORDER_BY' => 'cat_order ASC', |
||
| 296 | ]); |
||
| 297 | $result = $this->db->sql_query($sql); |
||
| 298 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 299 | { |
||
| 300 | $actual_cat = (int) $row['cat_id'] === $cat; |
||
| 301 | $this->template->assign_block_vars('categories', [ |
||
| 302 | 'CLASS' => $actual_cat ? 'cat-active' : 'cat-inactive', |
||
| 303 | 'SEPARATE' => ($i > 0) ? ' - ' : '', |
||
| 304 | 'CAT_NAME' => $row['cat_name'] ?: $row['cat_title'], |
||
| 305 | 'CAT_ORDER' => $row['cat_order'], |
||
| 306 | 'CAT_ID' => $row['cat_id'], |
||
| 307 | 'CAT_NB' => $row['cat_nb'], |
||
| 308 | 'U_CAT' => $this->helper->route('sylver35_smiliescat_smilies_pop', ['select' => $row['cat_id']]), |
||
| 309 | ]); |
||
| 310 | $i++; |
||
| 311 | |||
| 312 | // Keep these values in memory |
||
| 313 | $title = $actual_cat ? $row['cat_name'] : $title; |
||
| 314 | $cat_order = $row['cat_order']; |
||
| 315 | } |
||
| 316 | $this->db->sql_freeresult($result); |
||
| 317 | |||
| 318 | // Add the Unclassified category if not empty |
||
| 319 | if ($nb = $this->smilies_count(0)) |
||
| 320 | { |
||
| 321 | $this->template->assign_block_vars('categories', [ |
||
| 322 | 'CLASS' => ($cat === 0) ? 'cat-active' : 'cat-inactive', |
||
| 323 | 'SEPARATE' => ($i > 0) ? ' - ' : '', |
||
| 324 | 'CAT_NAME' => $this->language->lang('SC_CATEGORY_DEFAUT'), |
||
| 325 | 'CAT_ORDER' => $cat_order + 1, |
||
| 326 | 'CAT_ID' => 0, |
||
| 327 | 'CAT_NB' => $nb, |
||
| 328 | 'U_CAT' => $this->helper->route('sylver35_smiliescat_smilies_pop', ['select' => 0]), |
||
| 329 | ]); |
||
| 330 | } |
||
| 331 | |||
| 332 | return (!$cat) ? $this->language->lang('SC_CATEGORY_DEFAUT') : $title; |
||
| 333 | } |
||
| 334 | |||
| 335 | public function set_order($action, $current_order) |
||
| 354 | } |
||
| 355 | |||
| 356 | public function reset_first_cat($current_order, $switch_order_id) |
||
| 357 | { |
||
| 358 | $first = $this->get_first_order(); |
||
| 359 | if ($current_order === 1 || $switch_order_id === 1) |
||
| 360 | { |
||
| 361 | if ($this->get_cat_nb($first) > 0) |
||
| 362 | { |
||
| 363 | $this->config->set('smilies_category_nb', $first); |
||
| 364 | } |
||
| 365 | } |
||
| 366 | } |
||
| 367 | |||
| 368 | public function move_cat($id, $action) |
||
| 397 | } |
||
| 398 | } |
||
| 399 |