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 |
||
14 | class Manager extends Database implements ManagerInterface |
||
15 | { |
||
16 | /** @var string Entity identifier */ |
||
17 | protected $entityName; |
||
18 | |||
19 | /** @var string Entity primary field name */ |
||
20 | protected $primaryFieldName; |
||
21 | |||
22 | /** @var array Collection of entity field names and their types */ |
||
23 | protected $attributes = array(); |
||
24 | |||
25 | /** @var \PDO Database driver */ |
||
26 | protected $driver; |
||
27 | |||
28 | /** |
||
29 | * Manager constructor. |
||
30 | * |
||
31 | * @param string $entityName Entity name |
||
32 | * @param array $attributes Key-value collection with field name => type |
||
33 | * @param \PDO $driver database low-level driver |
||
34 | */ |
||
35 | public function __construct($entityName, $attributes, $driver) |
||
41 | |||
42 | View Code Duplication | public function execute($sql) |
|
66 | |||
67 | /** |
||
68 | * Get query for database entity to work with. |
||
69 | * |
||
70 | * @return QueryInterface Query for building database request |
||
71 | */ |
||
72 | public function query() |
||
76 | |||
77 | /** |
||
78 | * Get new entity instance. |
||
79 | * |
||
80 | * @return RecordInterface New database manager entity instance |
||
81 | */ |
||
82 | public function instance() |
||
86 | |||
87 | /** |
||
88 | * Create new database entity record. |
||
89 | * @param RecordInterface $entity Entity record for creation |
||
90 | * @return RecordInterface Created database entity record with new primary identifier |
||
91 | */ |
||
92 | public function create(RecordInterface $entity) |
||
100 | |||
101 | /** |
||
102 | * Read database entity records from QueryInterface. |
||
103 | * |
||
104 | * @param QueryInterface $query For retrieving records |
||
105 | * @return RecordInterface[] Collection of read database entity records |
||
106 | */ |
||
107 | public function read(QueryInterface $query) |
||
111 | |||
112 | /** |
||
113 | * Update database entity record. |
||
114 | * |
||
115 | * @param RecordInterface $entity Entity record for updating |
||
116 | */ |
||
117 | public function update(RecordInterface $entity) |
||
130 | |||
131 | /** |
||
132 | * Delete database record from database. |
||
133 | * |
||
134 | * @param RecordInterface $entity Entity record for removing |
||
135 | */ |
||
136 | public function delete(RecordInterface $entity) |
||
142 | |||
143 | /** |
||
144 | * Convert RecordInterface instance to collection of its field name => value. |
||
145 | * |
||
146 | * @param RecordInterface $object Database record instance to convert |
||
147 | * @return array Collection of key => value with SQL fields statements |
||
148 | */ |
||
149 | protected function &getFields(RecordInterface &$object = null) |
||
164 | |||
165 | /** |
||
166 | * Quote variable for security reasons. |
||
167 | * |
||
168 | * @param string $value |
||
169 | * @return string Quoted value |
||
170 | */ |
||
171 | protected function quote($value) |
||
175 | } |
||
176 |
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.