Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like AbstractTret 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 AbstractTret, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
13 | abstract class AbstractTret |
||
14 | { |
||
15 | /** |
||
16 | * Набор правил в данной группе, который задан изначально |
||
17 | * Его можно менять динамически добавляя туда правила с помощью put_rule |
||
18 | * |
||
19 | * @var array |
||
20 | */ |
||
21 | public $rules; |
||
22 | public $title; |
||
23 | public $logging = false; |
||
24 | public $logs = false; |
||
25 | public $errors = false; |
||
26 | public $debug_enabled = false; |
||
27 | public $debug_info = array(); |
||
28 | public $class_names = array(); |
||
29 | public $classes = array(); |
||
30 | public $settings = array(); |
||
31 | |||
32 | private $disabled = array(); |
||
33 | private $enabled = array(); |
||
34 | private $use_layout = false; |
||
35 | private $use_layout_set = false; |
||
36 | private $class_layout_prefix = false; |
||
37 | |||
38 | protected $_text = ''; |
||
39 | |||
40 | /** |
||
41 | * Защищенные теги |
||
42 | * |
||
43 | * @todo привязать к методам из Jare_Typograph_Tool |
||
44 | */ |
||
45 | const BASE64_PARAGRAPH_TAG = 'cA==='; // p |
||
46 | const BASE64_BREAKLINE_TAG = 'YnIgLw==='; // br / (с пробелом и слэшем) |
||
47 | const BASE64_NOBR_OTAG = 'bm9icg==='; // nobr |
||
48 | const BASE64_NOBR_CTAG = 'L25vYnI=='; // /nobr |
||
49 | |||
50 | /** |
||
51 | * Типы кавычек |
||
52 | */ |
||
53 | const QUOTE_FIRS_OPEN = '«'; |
||
54 | const QUOTE_FIRS_CLOSE = '»'; |
||
55 | const QUOTE_CRAWSE_OPEN = '„'; |
||
56 | const QUOTE_CRAWSE_CLOSE = '“'; |
||
57 | |||
58 | private function log($str, $data = null) |
||
63 | |||
64 | /** |
||
65 | * @param string $info |
||
66 | */ |
||
67 | private function error($info, $data = null) |
||
72 | |||
73 | public function debug($place, &$after_text) |
||
81 | |||
82 | /** |
||
83 | * Установить режим разметки для данного Трэта если не было раньше установлено, |
||
84 | * Util::LAYOUT_STYLE - с помощью стилей |
||
85 | * Util::LAYOUT_CLASS - с помощью классов |
||
86 | * |
||
87 | */ |
||
88 | public function set_tag_layout_ifnotset($layout) |
||
93 | |||
94 | /** |
||
95 | * Установить режим разметки для данного Трэта, |
||
96 | * Util::LAYOUT_STYLE - с помощью стилей |
||
97 | * Util::LAYOUT_CLASS - с помощью классов |
||
98 | * Util::LAYOUT_STYLE|Util::LAYOUT_CLASS - оба метода |
||
99 | * |
||
100 | */ |
||
101 | public function set_tag_layout($layout = Util::LAYOUT_STYLE) |
||
106 | |||
107 | public function set_class_layout_prefix($prefix) |
||
111 | |||
112 | public function debug_on() |
||
116 | |||
117 | public function log_on() |
||
121 | |||
122 | private function getmethod($name) |
||
129 | |||
130 | View Code Duplication | private function _pre_parse() |
|
140 | |||
141 | View Code Duplication | private function _post_parse() |
|
151 | |||
152 | private function rule_order_sort($a, $b) |
||
159 | |||
160 | private function apply_rule($rule) |
||
278 | |||
279 | /** |
||
280 | * @param string $pattern |
||
281 | * @param string $replacement |
||
282 | */ |
||
283 | protected function preg_replace_e($pattern, $replacement, $text) |
||
294 | |||
295 | private $thereplacement = ""; |
||
296 | |||
297 | private function thereplcallback($m) |
||
304 | |||
305 | private function _apply($list) |
||
328 | |||
329 | /** |
||
330 | * Создание защищенного тега с содержимым |
||
331 | * |
||
332 | * @see EMT_lib::build_safe_tag |
||
333 | * @param string $content |
||
334 | * @param string $tag |
||
335 | * @param array $attribute |
||
336 | * @return string |
||
337 | */ |
||
338 | protected function tag($content, $tag = 'span', $attribute = array()) |
||
361 | |||
362 | /** |
||
363 | * Добавить правило в группу |
||
364 | * |
||
365 | * @param string $name |
||
366 | * @param array $params |
||
367 | */ |
||
368 | public function put_rule($name, $params) |
||
374 | |||
375 | /** |
||
376 | * Отключить правило, в обработке |
||
377 | * |
||
378 | * @param string $name |
||
379 | */ |
||
380 | public function disable_rule($name) |
||
385 | |||
386 | /** |
||
387 | * Включить правило |
||
388 | * |
||
389 | * @param string $name |
||
390 | */ |
||
391 | public function enable_rule($name) |
||
396 | |||
397 | /** |
||
398 | * Добавить настройку в трет |
||
399 | * |
||
400 | * @param string $key ключ |
||
401 | * @param mixed $value значение |
||
402 | */ |
||
403 | public function set($key, $value) |
||
407 | |||
408 | /** |
||
409 | * Установлена ли настройка |
||
410 | * |
||
411 | * @param string $key |
||
412 | */ |
||
413 | View Code Duplication | public function is_on($key) |
|
420 | |||
421 | /** |
||
422 | * Получить строковое значение настройки |
||
423 | * |
||
424 | * @param unknown_type $key |
||
425 | * @return string |
||
426 | */ |
||
427 | public function ss($key) |
||
433 | |||
434 | /** |
||
435 | * Добавить настройку в правило |
||
436 | * |
||
437 | * @param string $rulename идентификатор правила |
||
438 | * @param string $key ключ |
||
439 | * @param mixed $value значение |
||
440 | */ |
||
441 | public function set_rule($rulename, $key, $value) |
||
445 | |||
446 | /** |
||
447 | * Включить правила, согласно списку |
||
448 | * |
||
449 | * @param array $list список правил |
||
450 | * @param boolean $disable выкллючить их или включить |
||
451 | * @param boolean $strict строго, т.е. те которые не в списку будут тоже обработаны |
||
452 | */ |
||
453 | public function activate($list, $disable = false, $strict = true) |
||
468 | |||
469 | public function set_text(&$text) |
||
475 | |||
476 | /** |
||
477 | * Применить к тексту |
||
478 | * |
||
479 | * @param mixed $list - список правил, null - все правила |
||
480 | * @return string |
||
481 | */ |
||
482 | public function apply($list = null) |
||
491 | |||
492 | /** |
||
493 | * Код, выполняем до того, как применить правила |
||
494 | * |
||
495 | */ |
||
496 | public function pre_parse() |
||
499 | |||
500 | /** |
||
501 | * После выполнения всех правил, выполняется этот метод |
||
502 | * |
||
503 | */ |
||
504 | public function post_parse() |
||
507 | |||
508 | } |
||
509 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.