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 |
||
16 | class DibiFluentMssqlDataSource extends DibiFluentDataSource |
||
17 | { |
||
18 | |||
19 | /** |
||
20 | * @var Fluent |
||
21 | */ |
||
22 | protected $data_source; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $data = []; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | protected $primary_key; |
||
33 | |||
34 | |||
35 | /** |
||
36 | * @param Fluent $data_source |
||
37 | * @param string $primary_key |
||
38 | */ |
||
39 | public function __construct(Fluent $data_source, $primary_key) |
||
40 | { |
||
41 | $this->data_source = $data_source; |
||
42 | $this->primary_key = $primary_key; |
||
43 | } |
||
44 | |||
45 | |||
46 | /******************************************************************************** |
||
47 | * IDataSource implementation * |
||
48 | ********************************************************************************/ |
||
49 | |||
50 | |||
51 | /** |
||
52 | * Get count of data |
||
53 | * @return int |
||
54 | */ |
||
55 | public function getCount() |
||
56 | { |
||
57 | $clone = clone $this->data_source; |
||
58 | $clone->removeClause('ORDER BY'); |
||
59 | |||
60 | return $clone->count(); |
||
61 | } |
||
62 | |||
63 | |||
64 | /** |
||
65 | * Get the data |
||
66 | * @param array $condition |
||
67 | * @return static |
||
68 | */ |
||
69 | public function filterOne(array $condition) |
||
70 | { |
||
71 | $this->data_source->where($condition); |
||
72 | |||
73 | return $this; |
||
74 | } |
||
75 | |||
76 | |||
77 | /** |
||
78 | * Filter by date |
||
79 | * @param Filter\FilterDate $filter |
||
80 | * @return void |
||
81 | */ |
||
82 | View Code Duplication | public function applyFilterDate(Filter\FilterDate $filter) |
|
|
|||
83 | { |
||
84 | $conditions = $filter->getCondition(); |
||
85 | |||
86 | $date = DateTimeHelper::tryConvertToDateTime($conditions[$filter->getColumn()], [$filter->getPhpFormat()]); |
||
87 | |||
88 | $this->data_source->where('CONVERT(varchar(10), %n, 112) = ?', $filter->getColumn(), $date->format('Ymd')); |
||
89 | } |
||
90 | |||
91 | |||
92 | /** |
||
93 | * Filter by date range |
||
94 | * @param Filter\FilterDateRange $filter |
||
95 | * @return void |
||
96 | */ |
||
97 | View Code Duplication | public function applyFilterDateRange(Filter\FilterDateRange $filter) |
|
112 | |||
113 | |||
114 | /** |
||
115 | * Filter by date |
||
116 | * @param Filter\FilterText $filter |
||
117 | * @return void |
||
118 | */ |
||
119 | public function applyFilterText(Filter\FilterText $filter) |
||
153 | |||
154 | |||
155 | /** |
||
156 | * Apply limit and offset on data |
||
157 | * @param int $offset |
||
158 | * @param int $limit |
||
159 | * @return static |
||
160 | */ |
||
161 | public function limit($offset, $limit) |
||
172 | } |
||
173 |
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.