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 Result extends ResultAbstract |
||
29 | { |
||
30 | /** |
||
31 | * the column names |
||
32 | * |
||
33 | * @var array |
||
34 | * @access protected |
||
35 | */ |
||
36 | protected $cols; |
||
37 | |||
38 | /** |
||
39 | * the column values |
||
40 | * |
||
41 | * @var array |
||
42 | * @access protected |
||
43 | */ |
||
44 | protected $vals; |
||
45 | |||
46 | /** |
||
47 | * @var \mysqli_stmt |
||
48 | * @access protected |
||
49 | */ |
||
50 | protected $statement; |
||
51 | |||
52 | /** |
||
53 | * Invoke to set statement |
||
54 | * |
||
55 | * @param \mysqli_stmt $statement |
||
56 | * @return $this |
||
57 | * @access public |
||
58 | */ |
||
59 | public function __invoke(\mysqli_stmt $statement) |
||
64 | |||
65 | /** |
||
66 | * {@inheritDoc} |
||
67 | */ |
||
68 | public function fieldCount()/*# : int */ |
||
72 | |||
73 | /** |
||
74 | * {@inheritDoc} |
||
75 | */ |
||
76 | public function rowCount()/*# : int */ |
||
80 | |||
81 | /** |
||
82 | * {@inheritDoc} |
||
83 | */ |
||
84 | public function affectedRows()/*# : int */ |
||
88 | |||
89 | /** |
||
90 | * {@inheritDoc} |
||
91 | */ |
||
92 | public function close() |
||
98 | |||
99 | /** |
||
100 | * {@inheritDoc} |
||
101 | */ |
||
102 | protected function realFetchAll()/*# : array */ |
||
115 | |||
116 | /** |
||
117 | * {@inheritDoc} |
||
118 | */ |
||
119 | protected function realFetchRow($rowCount)/*# : array */ |
||
133 | |||
134 | /** |
||
135 | * Get one row of data |
||
136 | * |
||
137 | * @return array|null |
||
138 | * @access protected |
||
139 | */ |
||
140 | protected function getOneRow() |
||
173 | } |
||
174 |
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.