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 |
||
10 | class MySQLAttributeRepository implements AttributeRepository { |
||
11 | /** @var PDO */ |
||
12 | private $pdo = null; |
||
13 | /** @var PDOStatement */ |
||
14 | private $selectServices = null; |
||
15 | /** @var PDOStatement */ |
||
16 | private $hasService = null; |
||
17 | /** @var PDOStatement */ |
||
18 | private $insertService = null; |
||
19 | /** @var PDOStatement */ |
||
20 | private $updateService = null; |
||
21 | /** @var PDOStatement */ |
||
22 | private $updateTryDate = null; |
||
23 | /** @var PDOStatement */ |
||
24 | private $updateRunDate = null; |
||
25 | /** @var array */ |
||
26 | private $services = array(); |
||
27 | |||
28 | /** |
||
29 | * @param PDO $pdo |
||
30 | * @param string $tableName |
||
31 | */ |
||
32 | public function __construct(PDO $pdo, $tableName = 'services') { |
||
44 | |||
45 | /** |
||
46 | * @param string $key |
||
47 | * @return bool |
||
48 | */ |
||
49 | View Code Duplication | public function has($key) { |
|
55 | |||
56 | /** |
||
57 | * @param string $key |
||
58 | * @param int $timeout |
||
59 | * @param array $data |
||
60 | * @throws Exception |
||
61 | * @return $this |
||
62 | */ |
||
63 | View Code Duplication | public function store($key, $timeout, array $data = array()) { |
|
85 | |||
86 | /** |
||
87 | * @param string $key |
||
88 | * @return $this |
||
89 | */ |
||
90 | public function markTry($key) { |
||
95 | |||
96 | /** |
||
97 | * @param string $key |
||
98 | * @return $this |
||
99 | */ |
||
100 | public function markRun($key) { |
||
105 | |||
106 | /** |
||
107 | * @return Service[] |
||
108 | */ |
||
109 | View Code Duplication | public function fetchServices() { |
|
119 | } |
||
120 |
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.