| Total Complexity | 56 |
| Total Lines | 404 |
| Duplicated Lines | 0 % |
| Changes | 27 | ||
| 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 | private const DEFAULT_CAT = 9998; |
||
| 25 | private const NOT_DISPLAY = 9999; |
||
| 26 | |||
| 27 | /** @var \phpbb\cache\driver\driver_interface */ |
||
| 28 | protected $cache; |
||
| 29 | |||
| 30 | /** @var \phpbb\db\driver\driver_interface */ |
||
| 31 | protected $db; |
||
| 32 | |||
| 33 | /** @var \phpbb\config\config */ |
||
| 34 | protected $config; |
||
| 35 | |||
| 36 | /* @var \phpbb\controller\helper */ |
||
| 37 | protected $helper; |
||
| 38 | |||
| 39 | /** @var \phpbb\user */ |
||
| 40 | protected $user; |
||
| 41 | |||
| 42 | /** @var \phpbb\language\language */ |
||
| 43 | protected $language; |
||
| 44 | |||
| 45 | /** @var \phpbb\template\template */ |
||
| 46 | protected $template; |
||
| 47 | |||
| 48 | /** @var \phpbb\extension\manager "Extension Manager" */ |
||
| 49 | protected $ext_manager; |
||
| 50 | |||
| 51 | /** @var \phpbb\log\log */ |
||
| 52 | protected $log; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The database tables |
||
| 56 | * |
||
| 57 | * @var string */ |
||
| 58 | protected $smilies_category_table; |
||
| 59 | |||
| 60 | /** @var string phpBB root path */ |
||
| 61 | protected $root_path; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Constructor |
||
| 65 | */ |
||
| 66 | 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) |
||
| 67 | { |
||
| 68 | $this->cache = $cache; |
||
| 69 | $this->db = $db; |
||
| 70 | $this->config = $config; |
||
| 71 | $this->helper = $helper; |
||
| 72 | $this->user = $user; |
||
| 73 | $this->language = $language; |
||
| 74 | $this->template = $template; |
||
| 75 | $this->ext_manager = $ext_manager; |
||
| 76 | $this->log = $log; |
||
| 77 | $this->smilies_category_table = $smilies_category_table; |
||
| 78 | $this->root_path = $root_path; |
||
| 79 | } |
||
| 80 | |||
| 81 | public function get_version() |
||
| 82 | { |
||
| 83 | if (($data = $this->cache->get('_smiliescat_version')) === false) |
||
| 84 | { |
||
| 85 | $md_manager = $this->ext_manager->create_extension_metadata_manager('sylver35/smiliescat'); |
||
| 86 | $meta = $md_manager->get_metadata(); |
||
| 87 | |||
| 88 | $data = [ |
||
| 89 | 'version' => $meta['version'], |
||
| 90 | 'homepage' => $meta['homepage'], |
||
| 91 | ]; |
||
| 92 | // cache for 7 days |
||
| 93 | $this->cache->put('_smiliescat_version', $data, 604800); |
||
| 94 | } |
||
| 95 | |||
| 96 | return $data; |
||
| 97 | } |
||
| 98 | |||
| 99 | public function smilies_count($cat) |
||
| 100 | { |
||
| 101 | $sql = $this->db->sql_build_query('SELECT', [ |
||
| 102 | 'SELECT' => 'COUNT(DISTINCT smiley_id) AS smilies_count', |
||
| 103 | 'FROM' => [SMILIES_TABLE => ''], |
||
| 104 | 'WHERE' => ($cat > 0) ? 'category = ' . $cat : "code <> ''", |
||
| 105 | ]); |
||
| 106 | $result = $this->db->sql_query($sql); |
||
| 107 | $nb = (int) $this->db->sql_fetchfield('smilies_count'); |
||
| 108 | $this->db->sql_freeresult($result); |
||
| 109 | |||
| 110 | return $nb; |
||
| 111 | } |
||
| 112 | |||
| 113 | public function capitalize($var) |
||
| 114 | { |
||
| 115 | return ucfirst(strtolower(trim($var))); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function get_langs() |
||
| 119 | { |
||
| 120 | $data = []; |
||
| 121 | $sql = 'SELECT * |
||
| 122 | FROM ' . LANG_TABLE; |
||
| 123 | $result = $this->db->sql_query($sql); |
||
| 124 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 125 | { |
||
| 126 | $data[$row['lang_id']] = $row['lang_iso']; |
||
| 127 | } |
||
| 128 | $this->db->sql_freeresult($result); |
||
| 129 | |||
| 130 | return $data; |
||
| 131 | } |
||
| 132 | |||
| 133 | public function verify_cat_langs($langs, $cat, $i, $lang_cat, $ajax) |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | private function cat_to_template($i, $data) |
||
| 155 | { |
||
| 156 | if (($i !== 0) && !empty($data)) |
||
| 157 | { |
||
| 158 | $this->template->assign_block_vars('categories', [ |
||
| 159 | 'ERROR' => true, |
||
| 160 | 'LANG_EMPTY' => $this->language->lang('SC_LANGUAGE_EMPTY', (count($data) > 1) ? 2 : 1) . implode(', ', $data), |
||
| 161 | ]); |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | private function cat_to_ajax($i, $data) |
||
| 166 | { |
||
| 167 | $values = [ |
||
| 168 | 'error' => false, |
||
| 169 | 'lang_empty' => '', |
||
| 170 | ]; |
||
| 171 | |||
| 172 | if (($i !== 0) && !empty($data)) |
||
| 173 | { |
||
| 174 | $values = [ |
||
| 175 | 'error' => true, |
||
| 176 | 'lang_empty' => $this->language->lang('SC_LANGUAGE_EMPTY', (count($data) > 1) ? 2 : 1) . implode(', ', $data), |
||
| 177 | ]; |
||
| 178 | } |
||
| 179 | |||
| 180 | return $values; |
||
| 181 | } |
||
| 182 | |||
| 183 | public function category_sort($category) |
||
| 199 | } |
||
| 200 | |||
| 201 | public function category_exist() |
||
| 202 | { |
||
| 203 | $sql = 'SELECT COUNT(cat_id) AS total |
||
| 204 | FROM ' . $this->smilies_category_table; |
||
| 205 | $result = $this->db->sql_query($sql); |
||
| 206 | $total = (int) $this->db->sql_fetchfield('total'); |
||
| 207 | $this->db->sql_freeresult($result); |
||
| 208 | |||
| 209 | return $total; |
||
| 210 | } |
||
| 211 | |||
| 212 | public function return_name($cat, $name = '', $all = false) |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | private function cat_name($cat) |
||
| 233 | { |
||
| 234 | $lang = (string) $this->user->lang_name; |
||
| 235 | $sql = 'SELECT cat_name |
||
| 236 | FROM ' . $this->smilies_category_table . " |
||
| 237 | WHERE cat_id = $cat |
||
| 238 | AND cat_lang = '$lang'"; |
||
| 239 | $result = $this->db->sql_query_limit($sql, 1); |
||
| 240 | $cat_name = (string) $this->db->sql_fetchfield('cat_name'); |
||
| 241 | $this->db->sql_freeresult($result); |
||
| 242 | if ($cat_name) |
||
| 243 | { |
||
| 244 | return $cat_name; |
||
| 245 | } |
||
| 246 | else |
||
| 247 | { |
||
| 248 | $sql2 = 'SELECT cat_name |
||
| 249 | FROM ' . $this->smilies_category_table . " |
||
| 250 | WHERE cat_id = $cat |
||
| 251 | AND cat_lang = 'en'"; |
||
| 252 | $result2 = $this->db->sql_query_limit($sql2, 1); |
||
| 253 | $cat_name = (string) $this->db->sql_fetchfield('cat_name'); |
||
| 254 | $this->db->sql_freeresult($result2); |
||
| 255 | } |
||
| 256 | |||
| 257 | return $cat_name; |
||
| 258 | } |
||
| 259 | |||
| 260 | public function get_max_order() |
||
| 261 | { |
||
| 262 | // Get max order id... |
||
| 263 | $sql = 'SELECT MAX(cat_order) AS maxi |
||
| 264 | FROM ' . $this->smilies_category_table; |
||
| 265 | $result = $this->db->sql_query($sql); |
||
| 266 | $max = (int) $this->db->sql_fetchfield('maxi'); |
||
| 267 | $this->db->sql_freeresult($result); |
||
| 268 | |||
| 269 | return $max; |
||
| 270 | } |
||
| 271 | |||
| 272 | public function get_max_id() |
||
| 273 | { |
||
| 274 | // Get max id... |
||
| 275 | $sql = 'SELECT MAX(cat_id) AS id_max |
||
| 276 | FROM ' . $this->smilies_category_table; |
||
| 277 | $result = $this->db->sql_query($sql); |
||
| 278 | $id_max = (int) $this->db->sql_fetchfield('id_max'); |
||
| 279 | $this->db->sql_freeresult($result); |
||
| 280 | |||
| 281 | return $id_max; |
||
| 282 | } |
||
| 283 | |||
| 284 | public function get_first_order() |
||
| 285 | { |
||
| 286 | // Get first order id non empty... |
||
| 287 | $sql = 'SELECT cat_id, cat_order, cat_nb |
||
| 288 | FROM ' . $this->smilies_category_table . ' |
||
| 289 | WHERE cat_nb > 0 |
||
| 290 | ORDER BY cat_order ASC'; |
||
| 291 | $result = $this->db->sql_query_limit($sql, 1); |
||
| 292 | $first = (int) $this->db->sql_fetchfield('cat_id'); |
||
| 293 | $this->db->sql_freeresult($result); |
||
| 294 | $first = !$first ? self::DEFAULT_CAT : $first; |
||
| 295 | |||
| 296 | return $first; |
||
| 297 | } |
||
| 298 | |||
| 299 | public function get_cat_id($id) |
||
| 300 | { |
||
| 301 | $sql = 'SELECT category |
||
| 302 | FROM ' . SMILIES_TABLE . ' |
||
| 303 | WHERE smiley_id = ' . $id; |
||
| 304 | $result = $this->db->sql_query($sql); |
||
| 305 | $category = (int) $this->db->sql_fetchfield('category'); |
||
| 306 | $this->db->sql_freeresult($result); |
||
| 307 | |||
| 308 | return $category; |
||
| 309 | } |
||
| 310 | |||
| 311 | public function get_cat_nb($id) |
||
| 312 | { |
||
| 313 | $sql = 'SELECT cat_nb |
||
| 314 | FROM ' . $this->smilies_category_table . ' |
||
| 315 | WHERE cat_id = ' . $id; |
||
| 316 | $result = $this->db->sql_query($sql); |
||
| 317 | $cat_nb = (int) $this->db->sql_fetchfield('cat_nb'); |
||
| 318 | $this->db->sql_freeresult($result); |
||
| 319 | |||
| 320 | return $cat_nb; |
||
| 321 | } |
||
| 322 | |||
| 323 | public function extract_list_categories($cat) |
||
| 324 | { |
||
| 325 | $title = ''; |
||
| 326 | $cat_order = $i = 0; |
||
| 327 | $lang = (string) $this->user->lang_name; |
||
| 328 | $sql = $this->db->sql_build_query('SELECT', [ |
||
| 329 | 'SELECT' => '*', |
||
| 330 | 'FROM' => [$this->smilies_category_table => ''], |
||
| 331 | 'WHERE' => "cat_nb <> 0 AND cat_lang = '$lang'", |
||
| 332 | 'ORDER_BY' => 'cat_order ASC', |
||
| 333 | ]); |
||
| 334 | $result = $this->db->sql_query($sql); |
||
| 335 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 336 | { |
||
| 337 | $actual_cat = (int) $row['cat_id'] === $cat; |
||
| 338 | $this->template->assign_block_vars('categories', [ |
||
| 339 | 'CLASS' => $actual_cat ? 'cat-active' : 'cat-inactive', |
||
| 340 | 'SEPARATE' => ($i > 0) ? ' - ' : '', |
||
| 341 | 'CAT_NAME' => $row['cat_name'] ?: $row['cat_title'], |
||
| 342 | 'CAT_ORDER' => $row['cat_order'], |
||
| 343 | 'CAT_ID' => $row['cat_id'], |
||
| 344 | 'CAT_NB' => $row['cat_nb'], |
||
| 345 | 'U_CAT' => $this->helper->route('sylver35_smiliescat_smilies_pop', ['select' => $row['cat_id']]), |
||
| 346 | ]); |
||
| 347 | $i++; |
||
| 348 | |||
| 349 | // Keep these values in memory |
||
| 350 | $title = $actual_cat ? $row['cat_name'] : $title; |
||
| 351 | $cat_order = $row['cat_order']; |
||
| 352 | } |
||
| 353 | $this->db->sql_freeresult($result); |
||
| 354 | |||
| 355 | // Add the Unclassified category if not empty |
||
| 356 | if ($nb = $this->smilies_count(self::DEFAULT_CAT)) |
||
| 357 | { |
||
| 358 | $title = ($cat == self::DEFAULT_CAT) ? $this->language->lang('SC_CATEGORY_DEFAUT') : $title; |
||
| 359 | $this->template->assign_block_vars('categories', [ |
||
| 360 | 'CLASS' => ($cat === self::DEFAULT_CAT) ? 'cat-active' : 'cat-inactive', |
||
| 361 | 'SEPARATE' => ($i > 0) ? ' - ' : '', |
||
| 362 | 'CAT_NAME' => $this->language->lang('SC_CATEGORY_DEFAUT'), |
||
| 363 | 'CAT_ORDER' => $cat_order + 1, |
||
| 364 | 'CAT_ID' => self::DEFAULT_CAT, |
||
| 365 | 'CAT_NB' => $nb, |
||
| 366 | 'U_CAT' => $this->helper->route('sylver35_smiliescat_smilies_pop', ['select' => self::DEFAULT_CAT]), |
||
| 367 | ]); |
||
| 368 | } |
||
| 369 | |||
| 370 | return $title; |
||
| 371 | } |
||
| 372 | |||
| 373 | public function set_order($action, $current_order) |
||
| 394 | } |
||
| 395 | |||
| 396 | public function move_cat($id, $action) |
||
| 426 | } |
||
| 427 | } |
||
| 428 |