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 EMTTret 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 EMTTret, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 18 | class EMTTret {
|
||
| 19 | |||
| 20 | /** |
||
| 21 | * Набор правил в данной группе, который задан изначально |
||
| 22 | * Его можно менять динамически добавляя туда правила с помощью put_rule |
||
| 23 | * |
||
| 24 | * @var unknown_type |
||
| 25 | */ |
||
| 26 | public $rules; |
||
| 27 | public $title; |
||
| 28 | |||
| 29 | |||
| 30 | private $disabled = array(); |
||
| 31 | private $enabled = array(); |
||
| 32 | protected $_text= ''; |
||
| 33 | public $logging = false; |
||
| 34 | public $logs = false; |
||
| 35 | public $errors = false; |
||
| 36 | public $debug_enabled = false; |
||
| 37 | public $debug_info = array(); |
||
| 38 | |||
| 39 | |||
| 40 | private $use_layout = false; |
||
| 41 | private $use_layout_set = false; |
||
| 42 | private $class_layout_prefix = false; |
||
| 43 | |||
| 44 | public $class_names = array(); |
||
| 45 | public $classes = array(); |
||
| 46 | public $settings = array(); |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Защищенные теги |
||
| 50 | * |
||
| 51 | * @todo привязать к методам из Jare_Typograph_Tool |
||
| 52 | */ |
||
| 53 | const BASE64_PARAGRAPH_TAG = 'cA==='; // p |
||
| 54 | const BASE64_BREAKLINE_TAG = 'YnIgLw==='; // br / (с пробелом и слэшем) |
||
| 55 | const BASE64_NOBR_OTAG = 'bm9icg==='; // nobr |
||
| 56 | const BASE64_NOBR_CTAG = 'L25vYnI=='; // /nobr |
||
| 57 | |||
| 58 | /** |
||
| 59 | * Типы кавычек |
||
| 60 | */ |
||
| 61 | const QUOTE_FIRS_OPEN = '«'; |
||
| 62 | const QUOTE_FIRS_CLOSE = '»'; |
||
| 63 | const QUOTE_CRAWSE_OPEN = '„'; |
||
| 64 | const QUOTE_CRAWSE_CLOSE = '“'; |
||
| 65 | |||
| 66 | |||
| 67 | private function log($str, $data = null) |
||
| 72 | |||
| 73 | private function error($info, $data = null) |
||
| 78 | |||
| 79 | public function debug($place, &$after_text) |
||
| 87 | |||
| 88 | |||
| 89 | /** |
||
| 90 | * Установить режим разметки для данного Трэта если не было раньше установлено, |
||
| 91 | * EMTLib::LAYOUT_STYLE - с помощью стилей |
||
| 92 | * EMTLib::LAYOUT_CLASS - с помощью классов |
||
| 93 | * |
||
| 94 | * @param int $kind |
||
|
|
|||
| 95 | */ |
||
| 96 | public function set_tag_layout_ifnotset($layout) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * Установить режим разметки для данного Трэта, |
||
| 104 | * EMTLib::LAYOUT_STYLE - с помощью стилей |
||
| 105 | * EMTLib::LAYOUT_CLASS - с помощью классов |
||
| 106 | * EMTLib::LAYOUT_STYLE|EMTLib::LAYOUT_CLASS - оба метода |
||
| 107 | * |
||
| 108 | * @param int $kind |
||
| 109 | */ |
||
| 110 | public function set_tag_layout($layout = EMTLib::LAYOUT_STYLE) |
||
| 115 | |||
| 116 | public function set_class_layout_prefix($prefix) |
||
| 120 | |||
| 121 | |||
| 122 | public function debug_on() |
||
| 126 | |||
| 127 | public function log_on() |
||
| 131 | |||
| 132 | |||
| 133 | private function getmethod($name) |
||
| 139 | |||
| 140 | View Code Duplication | private function _pre_parse() |
|
| 162 | |||
| 163 | private function rule_order_sort($a, $b) |
||
| 169 | |||
| 170 | private function apply_rule($rule) |
||
| 293 | |||
| 294 | |||
| 295 | protected function preg_replace_e($pattern, $replacement, $text) |
||
| 305 | |||
| 306 | private $thereplacement = ""; |
||
| 307 | private function thereplcallback($m) |
||
| 313 | |||
| 314 | private function _apply($list) |
||
| 339 | |||
| 340 | |||
| 341 | /** |
||
| 342 | * Создание защищенного тега с содержимым |
||
| 343 | * |
||
| 344 | * @see EMTLib::build_safe_tag |
||
| 345 | * @param string $content |
||
| 346 | * @param string $tag |
||
| 347 | * @param array $attribute |
||
| 348 | * @return string |
||
| 349 | */ |
||
| 350 | protected function tag($content, $tag = 'span', $attribute = array()) |
||
| 377 | |||
| 378 | |||
| 379 | /** |
||
| 380 | * Добавить правило в группу |
||
| 381 | * |
||
| 382 | * @param string $name |
||
| 383 | * @param array $params |
||
| 384 | */ |
||
| 385 | public function put_rule($name, $params) |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Отключить правило, в обработке |
||
| 393 | * |
||
| 394 | * @param string $name |
||
| 395 | */ |
||
| 396 | public function disable_rule($name) |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Включить правило |
||
| 404 | * |
||
| 405 | * @param string $name |
||
| 406 | */ |
||
| 407 | public function enable_rule($name) |
||
| 412 | |||
| 413 | /** |
||
| 414 | * Добавить настройку в трет |
||
| 415 | * |
||
| 416 | * @param string $key ключ |
||
| 417 | * @param mixed $value значение |
||
| 418 | */ |
||
| 419 | public function set($key, $value) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Установлена ли настройка |
||
| 426 | * |
||
| 427 | * @param string $key |
||
| 428 | */ |
||
| 429 | public function is_on($key) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * Получить строковое значение настройки |
||
| 438 | * |
||
| 439 | * @param unknown_type $key |
||
| 440 | * @return unknown |
||
| 441 | */ |
||
| 442 | public function ss($key) |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Добавить настройку в правило |
||
| 450 | * |
||
| 451 | * @param string $rulename идентификатор правила |
||
| 452 | * @param string $key ключ |
||
| 453 | * @param mixed $value значение |
||
| 454 | */ |
||
| 455 | public function set_rule($rulename, $key, $value) |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Включить правила, согласно списку |
||
| 462 | * |
||
| 463 | * @param array $list список правил |
||
| 464 | * @param boolean $disable выкллючить их или включить |
||
| 465 | * @param boolean $strict строго, т.е. те которые не в списку будут тоже обработаны |
||
| 466 | */ |
||
| 467 | public function activate($list,$disable =false, $strict = true) |
||
| 485 | |||
| 486 | public function set_text(&$text) |
||
| 492 | |||
| 493 | |||
| 494 | /** |
||
| 495 | * Применить к тексту |
||
| 496 | * |
||
| 497 | * @param string $text - текст к которому применить |
||
| 498 | * @param mixed $list - список правил, null - все правила |
||
| 499 | * @return string |
||
| 500 | */ |
||
| 501 | public function apply($list = null) |
||
| 509 | |||
| 510 | |||
| 511 | |||
| 512 | |||
| 513 | /** |
||
| 514 | * Код, выполняем до того, как применить правила |
||
| 515 | * |
||
| 516 | */ |
||
| 517 | public function pre_parse() |
||
| 520 | |||
| 521 | /** |
||
| 522 | * После выполнения всех правил, выполняется этот метод |
||
| 523 | * |
||
| 524 | */ |
||
| 525 | public function post_parse() |
||
| 528 | |||
| 529 | |||
| 530 | } |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.