| Total Complexity | 48 |
| Total Lines | 368 |
| 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 | /** @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) |
||
| 97 | { |
||
| 98 | $sql = $this->db->sql_build_query('SELECT', [ |
||
| 99 | 'SELECT' => 'COUNT(DISTINCT smiley_id) AS smilies_count', |
||
| 100 | 'FROM' => [SMILIES_TABLE => ''], |
||
| 101 | 'WHERE' => ($cat > -1) ? 'category = ' . (int) $cat : "code <> ''", |
||
| 102 | ]); |
||
| 103 | $result = $this->db->sql_query($sql); |
||
| 104 | $nb = (int) $this->db->sql_fetchfield('smilies_count'); |
||
| 105 | $this->db->sql_freeresult($result); |
||
| 106 | |||
| 107 | return $nb; |
||
| 108 | } |
||
| 109 | |||
| 110 | public function capitalize($var) |
||
| 113 | } |
||
| 114 | |||
| 115 | public function get_langs() |
||
| 116 | { |
||
| 117 | $data = []; |
||
| 118 | $sql = 'SELECT * |
||
| 119 | FROM ' . LANG_TABLE; |
||
| 120 | $result = $this->db->sql_query($sql); |
||
| 121 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 122 | { |
||
| 123 | $data[$row['lang_id']] = $row['lang_iso']; |
||
| 124 | } |
||
| 125 | $this->db->sql_freeresult($result); |
||
| 126 | |||
| 127 | return $data; |
||
| 128 | } |
||
| 129 | |||
| 130 | public function verify_cat_langs($langs, $cat, $i, $lang_cat, $ajax) |
||
| 148 | } |
||
| 149 | } |
||
| 150 | |||
| 151 | private function cat_to_template($i, $data) |
||
| 158 | ]); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | |||
| 162 | private function cat_to_ajax($i, $data) |
||
| 178 | } |
||
| 179 | |||
| 180 | public function category_exist() |
||
| 181 | { |
||
| 182 | $sql = 'SELECT COUNT(cat_id) AS total |
||
| 183 | FROM ' . $this->smilies_category_table; |
||
| 184 | $result = $this->db->sql_query($sql); |
||
| 185 | $total = (int) $this->db->sql_fetchfield('total'); |
||
| 186 | $this->db->sql_freeresult($result); |
||
| 187 | |||
| 188 | return $total; |
||
| 189 | } |
||
| 190 | |||
| 191 | public function cat_name($cat) |
||
| 192 | { |
||
| 193 | $cat_name = $this->language->lang('SC_CATEGORY_DEFAUT'); |
||
| 194 | if ($cat > 0) |
||
| 195 | { |
||
| 196 | $lang = (string) $this->user->lang_name; |
||
| 197 | $sql = 'SELECT cat_name |
||
| 198 | FROM ' . $this->smilies_category_table . " |
||
| 199 | WHERE cat_id = $cat |
||
| 200 | AND cat_lang = '$lang'"; |
||
| 201 | $result = $this->db->sql_query_limit($sql, 1); |
||
| 202 | $row = $this->db->sql_fetchrow($result); |
||
| 203 | $this->db->sql_freeresult($result); |
||
| 204 | if (isset($row['cat_name'])) |
||
| 205 | { |
||
| 206 | $cat_name = $row['cat_name']; |
||
| 207 | } |
||
| 208 | else |
||
| 209 | { |
||
| 210 | $sql = 'SELECT cat_name |
||
| 211 | FROM ' . $this->smilies_category_table . " |
||
| 212 | WHERE cat_lang = 'en' |
||
| 213 | AND cat_id = $cat"; |
||
| 214 | $result = $this->db->sql_query_limit($sql, 1); |
||
| 215 | $row = $this->db->sql_fetchrow($result); |
||
| 216 | $cat_name = $row['cat_name']; |
||
| 217 | $this->db->sql_freeresult($result); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | |||
| 221 | return $cat_name; |
||
| 222 | } |
||
| 223 | |||
| 224 | public function get_max_order() |
||
| 225 | { |
||
| 226 | // Get max order id... |
||
| 227 | $sql = 'SELECT MAX(cat_order) AS maxi |
||
| 228 | FROM ' . $this->smilies_category_table; |
||
| 229 | $result = $this->db->sql_query($sql); |
||
| 230 | $max = (int) $this->db->sql_fetchfield('maxi'); |
||
| 231 | $this->db->sql_freeresult($result); |
||
| 232 | |||
| 233 | return $max; |
||
| 234 | } |
||
| 235 | |||
| 236 | public function get_max_id() |
||
| 246 | } |
||
| 247 | |||
| 248 | public function get_first_order() |
||
| 249 | { |
||
| 250 | // Get first order id... |
||
| 251 | $sql = 'SELECT cat_id, cat_order, cat_nb |
||
| 252 | FROM ' . $this->smilies_category_table . ' |
||
| 253 | WHERE cat_nb > 0 |
||
| 254 | ORDER BY cat_order ASC'; |
||
| 255 | $result = $this->db->sql_query_limit($sql, 1); |
||
| 256 | $row = $this->db->sql_fetchrow($result); |
||
| 257 | $data = [ |
||
| 258 | 'first' => $row['cat_id'], |
||
| 259 | 'cat_nb' => $row['cat_nb'], |
||
| 260 | ]; |
||
| 261 | $this->db->sql_freeresult($result); |
||
| 262 | |||
| 263 | return $data; |
||
| 264 | } |
||
| 265 | |||
| 266 | public function get_cat_id($id) |
||
| 267 | { |
||
| 268 | $sql = 'SELECT category |
||
| 269 | FROM ' . SMILIES_TABLE . ' |
||
| 270 | WHERE smiley_id = ' . $id; |
||
| 271 | $result = $this->db->sql_query($sql); |
||
| 272 | $category = (int) $this->db->sql_fetchfield('category'); |
||
| 273 | $this->db->sql_freeresult($result); |
||
| 274 | |||
| 275 | return $category; |
||
| 276 | } |
||
| 277 | |||
| 278 | public function get_cat_nb($id) |
||
| 279 | { |
||
| 280 | $sql = 'SELECT cat_nb |
||
| 281 | FROM ' . $this->smilies_category_table . ' |
||
| 282 | WHERE cat_id = ' . (int) $id; |
||
| 283 | $result = $this->db->sql_query($sql); |
||
| 284 | $cat_nb = (int) $this->db->sql_fetchfield('cat_nb'); |
||
| 285 | $this->db->sql_freeresult($result); |
||
| 286 | |||
| 287 | return $cat_nb; |
||
| 288 | } |
||
| 289 | |||
| 290 | public function extract_list_categories($cat) |
||
| 291 | { |
||
| 292 | $title = ''; |
||
| 293 | $cat_order = $i = 0; |
||
| 294 | $lang = (string) $this->user->lang_name; |
||
| 295 | $sql = $this->db->sql_build_query('SELECT', [ |
||
| 296 | 'SELECT' => '*', |
||
| 297 | 'FROM' => [$this->smilies_category_table => ''], |
||
| 298 | 'WHERE' => "cat_nb <> 0 AND cat_lang = '$lang'", |
||
| 299 | 'ORDER_BY' => 'cat_order ASC', |
||
| 300 | ]); |
||
| 301 | $result = $this->db->sql_query($sql); |
||
| 302 | while ($row = $this->db->sql_fetchrow($result)) |
||
| 303 | { |
||
| 304 | $actual_cat = (int) $row['cat_id'] === $cat; |
||
| 305 | $this->template->assign_block_vars('categories', [ |
||
| 306 | 'CLASS' => $actual_cat ? 'cat-active' : 'cat-inactive', |
||
| 307 | 'SEPARATE' => ($i > 0) ? ' - ' : '', |
||
| 308 | 'CAT_NAME' => $row['cat_name'] ?: $row['cat_title'], |
||
| 309 | 'CAT_ORDER' => $row['cat_order'], |
||
| 310 | 'CAT_ID' => $row['cat_id'], |
||
| 311 | 'CAT_NB' => $row['cat_nb'], |
||
| 312 | 'U_CAT' => $this->helper->route('sylver35_smiliescat_smilies_pop', ['select' => $row['cat_id']]), |
||
| 313 | ]); |
||
| 314 | $i++; |
||
| 315 | |||
| 316 | // Keep these values in memory |
||
| 317 | $title = $actual_cat ? $row['cat_name'] : $title; |
||
| 318 | $cat_order = $row['cat_order']; |
||
| 319 | } |
||
| 320 | $this->db->sql_freeresult($result); |
||
| 321 | |||
| 322 | // Add the Unclassified category if not empty |
||
| 323 | if ($nb = $this->smilies_count(0)) |
||
| 324 | { |
||
| 325 | $this->template->assign_block_vars('categories', [ |
||
| 326 | 'CLASS' => ($cat === 0) ? 'cat-active' : 'cat-inactive', |
||
| 327 | 'SEPARATE' => ($i > 0) ? ' - ' : '', |
||
| 328 | 'CAT_NAME' => $this->language->lang('SC_CATEGORY_DEFAUT'), |
||
| 329 | 'CAT_ORDER' => $cat_order + 1, |
||
| 330 | 'CAT_ID' => 0, |
||
| 331 | 'CAT_NB' => $nb, |
||
| 332 | 'U_CAT' => $this->helper->route('sylver35_smiliescat_smilies_pop', ['select' => 0]), |
||
| 333 | ]); |
||
| 334 | } |
||
| 335 | |||
| 336 | return (!$cat) ? $this->language->lang('SC_CATEGORY_DEFAUT') : $title; |
||
| 337 | } |
||
| 338 | |||
| 339 | public function set_order($action, $current_order) |
||
| 360 | } |
||
| 361 | |||
| 362 | public function move_cat($id, $action) |
||
| 390 | } |
||
| 391 | } |
||
| 392 |