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 |
||
8 | abstract class BaseXml extends CComponent |
||
9 | { |
||
10 | public $templatesAlias = 'frontend.views.xml'; |
||
11 | |||
12 | public $charset = 'windows-1251'; |
||
13 | |||
14 | public $template; |
||
15 | |||
16 | public $filePath; |
||
17 | |||
18 | public $dataProviderClass; |
||
19 | |||
20 | /** |
||
21 | * @var CDbCriteria |
||
22 | */ |
||
23 | public $criteria; |
||
24 | |||
25 | public $itemsBufferSize = 1000; |
||
26 | |||
27 | /** |
||
28 | * @var XMLWriter |
||
29 | */ |
||
30 | protected $xmlWriter; |
||
31 | |||
32 | protected $dataProvider; |
||
33 | |||
34 | /** |
||
35 | * @var bool $outCashedXml отдавать кэшированый файл |
||
36 | */ |
||
37 | protected $outCashedXml = false; |
||
38 | |||
39 | private $itemsBufferCounter = 0; |
||
40 | |||
41 | abstract public function buildXml(); |
||
42 | |||
43 | public function init() |
||
66 | |||
67 | public function render() |
||
75 | |||
76 | View Code Duplication | protected function saveXml() |
|
88 | |||
89 | /** |
||
90 | * @param string $template |
||
91 | * |
||
92 | * @return string $path |
||
93 | * @throws InvalidArgumentException |
||
94 | */ |
||
95 | View Code Duplication | protected function getTemplatePath($template) |
|
111 | |||
112 | /** |
||
113 | * @return string $filePath |
||
114 | */ |
||
115 | View Code Duplication | protected function getXmlPath() |
|
125 | |||
126 | /** |
||
127 | * Следить за буфером, при необходимост и обнулить |
||
128 | */ |
||
129 | protected function followBuffer() |
||
134 | |||
135 | private function flushItemsBuffer() |
||
140 | |||
141 | private function increaseBufferCounter() |
||
145 | |||
146 | private function isInvalidateCache() |
||
161 | } |
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.