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 |
||
17 | class SportProxy extends Proxy implements SportInterface { |
||
18 | |||
19 | /** |
||
20 | * {@inheritdoc} |
||
21 | */ |
||
22 | 1 | public function load() { |
|
25 | |||
26 | /** |
||
27 | * Lazy loads the leagues for this sport. |
||
28 | * |
||
29 | * @throws \Exception |
||
30 | * When the leagues cannot be loaded. |
||
31 | * |
||
32 | * @return void |
||
33 | */ |
||
34 | 1 | View Code Duplication | protected function loadLeagues() { |
42 | |||
43 | /** |
||
44 | * {@inheritdoc} |
||
45 | */ |
||
46 | 1 | public function getId() { |
|
49 | |||
50 | /** |
||
51 | * {@inheritdoc} |
||
52 | */ |
||
53 | 1 | public function getLeagues() { |
|
56 | |||
57 | /** |
||
58 | * {@inheritdoc} |
||
59 | */ |
||
60 | 1 | public function getName() { |
|
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | 1 | public function addLeague(LeagueInterface $league) { |
|
79 | |||
80 | } |
||
81 |
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.