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 Delete extends AbstractCommand |
||
39 | { |
||
40 | |||
41 | private $table; |
||
42 | |||
43 | use WhereTrait; |
||
44 | |||
45 | /** |
||
46 | * @param string $table table with which you wish to remove records from |
||
47 | * @param null|WhereStatementInterface $where an object that is designed to |
||
48 | * return a where statement to limit the data that is affected by the delete |
||
49 | * @since v1.0.0 |
||
50 | */ |
||
51 | 11 | public function __construct($table, WhereStatementInterface $where = null) |
|
55 | |||
56 | /** |
||
57 | * takes the SQL and the data provided and executes the query with the data |
||
58 | * @param PDO $pdo a connection object that defines where the connection is to be executed |
||
59 | * @return bool TRUE on success or FALSE on failure. |
||
60 | * @throws ExecutionErrorException thrown when any exception or SQL failure occurs |
||
61 | * @since v1.0.0 |
||
62 | */ |
||
63 | 4 | View Code Duplication | public function execute(PDO $pdo) |
80 | |||
81 | /** |
||
82 | * retrieves the data that is uses to fulfill the requirements of a prepared |
||
83 | * statement |
||
84 | * @return array a single level associative array containing keys that |
||
85 | * represent the fields and values that represent items to fulfill the |
||
86 | * requirements of a prepared statement |
||
87 | * @since v1.0.0 |
||
88 | */ |
||
89 | 6 | public function getData() |
|
93 | |||
94 | /** |
||
95 | * Generates a SQL statement ready to be prepared for execution with the intent of removing data |
||
96 | * @return string a string that represents a delete statement ready to be prepared by PDO |
||
97 | * @since v1.0.0 |
||
98 | */ |
||
99 | 6 | public function getSql() |
|
104 | |||
105 | /** |
||
106 | * retrieves the table with which you wish to remove from |
||
107 | * @return string table with which you wish to remove from |
||
108 | * @since v1.0.0 |
||
109 | */ |
||
110 | 8 | public function getTable() |
|
114 | |||
115 | /** |
||
116 | * defines a table with which you wish to remove from |
||
117 | * @param string $table table with which you wish to remove from |
||
118 | * @return Delete for method chaining |
||
119 | * @since v1.0.0 |
||
120 | */ |
||
121 | 11 | public function setTable($table) |
|
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.