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 |
||
38 | class Create extends AbstractCommand |
||
39 | { |
||
40 | |||
41 | |||
42 | private $table; |
||
43 | private $data; |
||
44 | |||
45 | /** |
||
46 | * @param string $table table with which you wish to append to |
||
47 | * @param array $data a single level associative array containing keys that |
||
48 | * represent the fields and values that represent new values to be added into |
||
49 | * the table |
||
50 | * @since v1.0.0 |
||
51 | */ |
||
52 | 9 | public function __construct($table, $data) |
|
56 | |||
57 | /** |
||
58 | * Generates a SQL statement ready to be prepared for execution with the |
||
59 | * intent of updating data |
||
60 | * @return string a string that represents an update statement ready to be |
||
61 | * prepared by PDO |
||
62 | * @throws EmptyDatasetException if no data has been set no fields can be |
||
63 | * discerned and no query can be made |
||
64 | * @since v1.0.0 |
||
65 | */ |
||
66 | 6 | public function getSql() |
|
80 | |||
81 | /** |
||
82 | * takes the SQL and the data provided and executes the query with the data |
||
83 | * @param PDO $pdo a connection object that defines where the connection is |
||
84 | * to be executed |
||
85 | * @return string will return the lastInsertId from the PDO connection object |
||
86 | * @throws ExecutionErrorException thrown when any exception or SQL failure |
||
87 | * occurs |
||
88 | * @since v1.0.0 |
||
89 | */ |
||
90 | 3 | View Code Duplication | public function execute(PDO $pdo) |
106 | |||
107 | /** |
||
108 | * retrieves the table with which you wish to append to |
||
109 | * @return string table with which you wish to append to |
||
110 | * @since v1.0.0 |
||
111 | */ |
||
112 | 6 | public function getTable() |
|
116 | |||
117 | /** |
||
118 | * retrieves the data that is used to generate the create statement. The |
||
119 | * fields of the array are used to generate the field list. |
||
120 | * @return array a single level associative array containing keys that |
||
121 | * represent the fields and values that represent new values to be added |
||
122 | * into the table |
||
123 | * @since v1.0.0 |
||
124 | */ |
||
125 | 7 | public function getData() |
|
129 | |||
130 | /** |
||
131 | * defines a table with which you wish to append to |
||
132 | * @param string $table a table with which you wish to append to |
||
133 | * @return Create for method chaining |
||
134 | * @since v1.0.0 |
||
135 | */ |
||
136 | 9 | public function setTable($table) |
|
141 | |||
142 | /** |
||
143 | * sets the data that is used to generate the create statement. The fields |
||
144 | * of the array are used to generate the field list. |
||
145 | * @param array $data a single level associative array containing keys that |
||
146 | * represent the fields and values that represent new values to be added |
||
147 | * into the table |
||
148 | * @return Create for method chaining |
||
149 | * @since v1.0.0 |
||
150 | */ |
||
151 | 9 | public function setData(array $data) |
|
156 | } |
||
157 |
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.