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 |
||
8 | class Updater |
||
9 | { |
||
10 | const STABILITY_STABLE = 'stable'; |
||
11 | const STABILITY_UNSTABLE = 'unstable'; |
||
12 | const STABILITY_ANY = 'any'; |
||
13 | |||
14 | /** |
||
15 | * @var AdapterInterface |
||
16 | */ |
||
17 | private $adapter; |
||
18 | |||
19 | /** |
||
20 | * Updater constructor. |
||
21 | * |
||
22 | * @param AdapterInterface $adapter |
||
23 | */ |
||
24 | public function __construct(AdapterInterface $adapter) |
||
28 | |||
29 | /** |
||
30 | * @return mixed |
||
31 | * |
||
32 | * @throws UpdaterException |
||
33 | */ |
||
34 | public function hasUpdate() |
||
42 | |||
43 | /** |
||
44 | * @return mixed |
||
45 | */ |
||
46 | public function getOldVersion() |
||
50 | |||
51 | /** |
||
52 | * @return mixed |
||
53 | */ |
||
54 | public function getNewVersion() |
||
58 | |||
59 | /** |
||
60 | * @param string $stability |
||
61 | * |
||
62 | * @return bool |
||
63 | * |
||
64 | * @throws UpdaterException |
||
65 | */ |
||
66 | public function update($stability = self::STABILITY_ANY) |
||
76 | |||
77 | /** |
||
78 | * @return mixed |
||
79 | * |
||
80 | * @throws UpdaterException |
||
81 | */ |
||
82 | public function rollback() |
||
90 | |||
91 | /** |
||
92 | * @param $stability |
||
93 | */ |
||
94 | private function validateStability($stability) |
||
100 | } |
||
101 |
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.