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 |
||
28 | class IndexPage extends Widget |
||
29 | { |
||
30 | /** |
||
31 | * @var Model the search model |
||
32 | */ |
||
33 | public $model; |
||
34 | |||
35 | /** |
||
36 | * @var object original view context. |
||
37 | * It is used to render sub-views with the same context, as IndexPage |
||
38 | */ |
||
39 | public $originalContext; |
||
40 | |||
41 | /** |
||
42 | * @var DataProviderInterface |
||
43 | */ |
||
44 | public $dataProvider; |
||
45 | |||
46 | /** |
||
47 | * @var array Hash of document blocks, that can be rendered later in the widget's views |
||
48 | * Blocks can be set explicitly on widget initialisation, or by calling [[beginContent]] and |
||
49 | * [[endContent]] |
||
50 | * |
||
51 | * @see beginContent |
||
52 | * @see endContent |
||
53 | */ |
||
54 | public $contents = []; |
||
55 | |||
56 | /** |
||
57 | * @var string the name of current content block, that is under the render |
||
58 | * @see beginContent |
||
59 | * @see endContent |
||
60 | */ |
||
61 | protected $_current = null; |
||
62 | |||
63 | /** |
||
64 | * @var array |
||
65 | */ |
||
66 | public $searchFormData = []; |
||
67 | |||
68 | /** |
||
69 | * @var string the name of view file that contains search fields for the index page. Defaults to `_search` |
||
70 | * @see renderSearchForm() |
||
71 | */ |
||
72 | public $searchView = '_search'; |
||
73 | |||
74 | /** {@inheritdoc} */ |
||
75 | public function init() |
||
104 | |||
105 | /** |
||
106 | * Begins output buffer capture to save data in [[contents]] with the $name key. |
||
107 | * Must not be called nested. See [[endContent]] for capture terminating. |
||
108 | * @param string $name |
||
109 | */ |
||
110 | public function beginContent($name) |
||
119 | |||
120 | /** |
||
121 | * Terminates output buffer capture started by [[beginContent()]]. |
||
122 | * @see beginContent |
||
123 | */ |
||
124 | public function endContent() |
||
133 | |||
134 | /** |
||
135 | * Returns content saved in [[content]] by $name. |
||
136 | * @param string $name |
||
137 | * @return string |
||
138 | */ |
||
139 | public function renderContent($name) |
||
143 | |||
144 | public function run() |
||
148 | |||
149 | public function getOrientationStorage() |
||
155 | |||
156 | public function setSearchFormData($data = []) |
||
160 | |||
161 | public function renderSearchForm($advancedSearchOptions = []) |
||
179 | |||
180 | public function beginSearchForm($options = []) |
||
184 | |||
185 | public function renderSearchButton() |
||
189 | |||
190 | View Code Duplication | public function renderPerPage() |
|
206 | |||
207 | /** |
||
208 | * Renders button to choose representation. |
||
209 | * Returns empty string when nothing to choose (less then 2 representations available). |
||
210 | * @param array $grid class |
||
211 | * @param mixed $current selected representation |
||
212 | * @return string rendered HTML |
||
213 | */ |
||
214 | public static function renderRepresentations($grid, $current) |
||
239 | |||
240 | View Code Duplication | public function renderSorter(array $options) |
|
248 | |||
249 | public function getViewPath() |
||
253 | |||
254 | public function getBulkFormId() |
||
258 | |||
259 | public function beginBulkForm($action = '') |
||
263 | |||
264 | public function endBulkForm() |
||
268 | |||
269 | View Code Duplication | public function renderBulkButton($text, $action, $color = 'default') |
|
278 | } |
||
279 |
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.