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:
1 | <?php |
||
6 | class Quote extends AbstractTret |
||
7 | { |
||
8 | /** |
||
9 | * Базовые параметры тофа |
||
10 | * |
||
11 | * @var array |
||
12 | */ |
||
13 | public $title = "Кавычки"; |
||
14 | |||
15 | public $rules = array( |
||
16 | 'quotes_outside_a' => array( |
||
17 | 'description' => 'Кавычки вне тэга <a>', |
||
18 | 'pattern' => '/(\<%%\_\_[^\>]+\>)\"(.+?)\"(\<\/%%\_\_[^\>]+\>)/s', |
||
19 | 'replacement' => '"\1\2\3"' |
||
20 | ), |
||
21 | |||
22 | 'open_quote' => array( |
||
23 | 'description' => 'Открывающая кавычка', |
||
24 | 'pattern' => '/(^|\(|\s|\>|-)(\"|\\\")(\S+)/iue', |
||
25 | 'replacement' => '$m[1] . \EMT\Tret\AbstractTret::QUOTE_FIRS_OPEN . $m[3]' |
||
26 | ), |
||
27 | 'close_quote' => array( |
||
28 | 'description' => 'Закрывающая кавычка', |
||
29 | 'pattern' => '/([a-zа-яё0-9]|\.|\&hellip\;|\!|\?|\>|\)|\:)((\"|\\\")+)(\.|\&hellip\;|\;|\:|\?|\!|\,|\s|\)|\<\/|$)/uie', |
||
30 | 'replacement' => '$m[1] . str_repeat(\EMT\Tret\AbstractTret::QUOTE_FIRS_CLOSE, substr_count($m[2],"\"") ) . $m[4]' |
||
31 | ), |
||
32 | 'close_quote_adv' => array( |
||
33 | 'description' => 'Закрывающая кавычка особые случаи', |
||
34 | 'pattern' => |
||
35 | array( |
||
36 | '/([a-zа-яё0-9]|\.|\&hellip\;|\!|\?|\>|\)|\:)((\"|\\\"|\«\;)+)(\<[^\>]+\>)(\.|\&hellip\;|\;|\:|\?|\!|\,|\)|\<\/|$| )/uie', |
||
37 | '/([a-zа-яё0-9]|\.|\&hellip\;|\!|\?|\>|\)|\:)(\s+)((\"|\\\")+)(\s+)(\.|\&hellip\;|\;|\:|\?|\!|\,|\)|\<\/|$| )/uie', |
||
38 | '/\>(\«\;)\.($|\s|\<)/ui', |
||
39 | '/\>(\«\;),($|\s|\<|\S)/ui', |
||
40 | ), |
||
41 | 'replacement' => |
||
42 | array( |
||
43 | '$m[1] . str_repeat(\EMT\Tret\AbstractTret::QUOTE_FIRS_CLOSE, substr_count($m[2],"\"")+substr_count($m[2],"«") ) . $m[4]. $m[5]', |
||
44 | '$m[1] .$m[2]. str_repeat(\EMT\Tret\AbstractTret::QUOTE_FIRS_CLOSE, substr_count($m[3],"\"")+substr_count($m[3],"«") ) . $m[5]. $m[6]', |
||
45 | '>».\2', |
||
46 | '>»,\2', |
||
47 | ), |
||
48 | ), |
||
49 | 'open_quote_adv' => array( |
||
50 | 'description' => 'Открывающая кавычка особые случаи', |
||
51 | 'pattern' => '/(^|\(|\s|\>)(\"|\\\")(\s)(\S+)/iue', |
||
52 | 'replacement' => '$m[1] . \EMT\Tret\AbstractTret::QUOTE_FIRS_OPEN .$m[4]' |
||
53 | ), |
||
54 | 'quotation' => array( |
||
55 | 'description' => 'Внутренние кавычки-лапки и дюймы', |
||
56 | 'function' => 'build_sub_quotations' |
||
57 | ), |
||
58 | ); |
||
59 | |||
60 | /** |
||
61 | * @param string $text |
||
62 | */ |
||
63 | protected function inject_in($pos, $text) |
||
67 | |||
68 | protected function build_sub_quotations() |
||
153 | } |
||
154 |
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.