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 |
||
9 | class Renderer extends AbstractRenderer |
||
10 | { |
||
11 | /** |
||
12 | * @return string |
||
13 | */ |
||
14 | public function getName() |
||
15 | { |
||
16 | return 'bootstrapTable'; |
||
17 | } |
||
18 | |||
19 | /** |
||
20 | * @return bool |
||
21 | */ |
||
22 | public function isExport() |
||
23 | { |
||
24 | return false; |
||
25 | } |
||
26 | |||
27 | /** |
||
28 | * @return bool |
||
29 | */ |
||
30 | public function isHtml() |
||
31 | { |
||
32 | return true; |
||
33 | } |
||
34 | |||
35 | /** |
||
36 | * @return HttpRequest |
||
37 | * |
||
38 | * @throws \Exception |
||
39 | */ |
||
40 | public function getRequest() |
||
41 | { |
||
42 | $request = parent::getRequest(); |
||
43 | if (!$request instanceof HttpRequest) { |
||
44 | throw new \Exception('Request must be an instance of Zend\Http\PhpEnvironment\Request for HTML rendering'); |
||
45 | } |
||
46 | |||
47 | return $request; |
||
48 | } |
||
49 | |||
50 | /** |
||
51 | * @see \ZfcDatagrid\Renderer\AbstractRenderer::getSortConditions() |
||
52 | * |
||
53 | * @return array |
||
54 | * |
||
55 | * @throws \Exception |
||
56 | */ |
||
57 | View Code Duplication | public function getSortConditions() |
|
|
|||
58 | { |
||
59 | if (is_array($this->sortConditions)) { |
||
60 | // set from cache! (for export) |
||
61 | return $this->sortConditions; |
||
62 | } |
||
63 | |||
64 | $request = $this->getRequest(); |
||
65 | |||
66 | $optionsRenderer = $this->getOptionsRenderer(); |
||
67 | $parameterNames = $optionsRenderer['parameterNames']; |
||
68 | |||
69 | $sortConditions = []; |
||
70 | $sortColumns = $request->getPost($parameterNames['sortColumns'], $request->getQuery($parameterNames['sortColumns'])); |
||
71 | $sortDirections = $request->getPost($parameterNames['sortDirections'], $request->getQuery($parameterNames['sortDirections'])); |
||
72 | if ($sortColumns != '') { |
||
73 | $sortColumns = explode(',', $sortColumns); |
||
74 | $sortDirections = explode(',', $sortDirections); |
||
75 | |||
76 | if (count($sortColumns) != count($sortDirections)) { |
||
77 | throw new \Exception('Count missmatch order columns/direction'); |
||
78 | } |
||
79 | |||
80 | foreach ($sortColumns as $key => $sortColumn) { |
||
81 | $sortDirection = strtoupper($sortDirections[$key]); |
||
82 | |||
83 | if ($sortDirection != 'ASC' && $sortDirection != 'DESC') { |
||
84 | $sortDirection = 'ASC'; |
||
85 | } |
||
86 | |||
87 | foreach ($this->getColumns() as $column) { |
||
88 | /* @var $column \ZfcDatagrid\Column\AbstractColumn */ |
||
89 | if ($column->getUniqueId() == $sortColumn) { |
||
90 | $sortConditions[] = [ |
||
91 | 'sortDirection' => $sortDirection, |
||
92 | 'column' => $column, |
||
93 | ]; |
||
94 | |||
95 | $column->setSortActive($sortDirection); |
||
96 | } |
||
97 | } |
||
98 | } |
||
99 | } |
||
100 | |||
101 | if (!empty($sortConditions)) { |
||
102 | $this->sortConditions = $sortConditions; |
||
103 | } else { |
||
104 | // No user sorting -> get default sorting |
||
105 | $this->sortConditions = $this->getSortConditionsDefault(); |
||
106 | } |
||
107 | |||
108 | return $this->sortConditions; |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @todo Make parameter config |
||
113 | * |
||
114 | * @see \ZfcDatagrid\Renderer\AbstractRenderer::getFilters() |
||
115 | */ |
||
116 | public function getFilters() |
||
152 | |||
153 | /** |
||
154 | * @return int |
||
155 | * |
||
156 | * @throws \Exception |
||
157 | */ |
||
158 | public function getCurrentPageNumber() |
||
170 | |||
171 | /** |
||
172 | * @param Datagrid $grid |
||
173 | */ |
||
174 | public function prepareViewModel(Datagrid $grid) |
||
192 | |||
193 | /** |
||
194 | * @return \Zend\View\Model\ViewModel |
||
195 | */ |
||
196 | public function execute() |
||
203 | } |
||
204 |
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.