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 |
||
17 | trait TransactionalTrait |
||
18 | { |
||
19 | /** |
||
20 | * @var bool |
||
21 | */ |
||
22 | private $transactional = false; |
||
23 | |||
24 | /** |
||
25 | * @var int |
||
26 | */ |
||
27 | private $transactionalLevel = 0; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | private $watched = array(); |
||
33 | |||
34 | /** |
||
35 | * @return boolean |
||
36 | */ |
||
37 | public function isTransactional() |
||
41 | |||
42 | /** |
||
43 | * @return int |
||
44 | */ |
||
45 | public function getTransactionalLevel() |
||
49 | |||
50 | /** |
||
51 | * Begin transaction |
||
52 | */ |
||
53 | public function beginTransaction() |
||
61 | |||
62 | /** |
||
63 | * Rollback transaction |
||
64 | */ |
||
65 | View Code Duplication | public function rollback() |
|
77 | |||
78 | /** |
||
79 | * Commit transaction. |
||
80 | */ |
||
81 | View Code Duplication | public function commit() |
|
95 | |||
96 | /** |
||
97 | * @param string $table |
||
98 | * @param array $record |
||
99 | */ |
||
100 | protected function watch($table, array &$record) |
||
110 | |||
111 | /** |
||
112 | * Commit action. |
||
113 | * |
||
114 | * @param string $table |
||
115 | * @param array $records |
||
116 | */ |
||
117 | abstract protected function performCommit($table, array $records); |
||
118 | |||
119 | /** |
||
120 | * Rollback action. |
||
121 | * |
||
122 | * @param string $table |
||
123 | * @param array $records |
||
124 | */ |
||
125 | abstract protected function performRollback($table, array $records); |
||
126 | } |
||
127 |
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.