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 |
||
12 | View Code Duplication | class GithubBranchSource implements GithubBranch |
|
|
|||
13 | { |
||
14 | /** @var GithubBranchId */ |
||
15 | private $id; |
||
16 | |||
17 | /** @var GithubRepo */ |
||
18 | private $repo; |
||
19 | |||
20 | /** @var string */ |
||
21 | private $name; |
||
22 | |||
23 | /** @var GithubCommit */ |
||
24 | private $lastCommit; |
||
25 | |||
26 | /** |
||
27 | * GithubBranchSource constructor. |
||
28 | * |
||
29 | * @param GithubBranchId $id |
||
30 | * @param GithubRepo $repo |
||
31 | * @param string $name |
||
32 | * @param GithubCommit $lastCommit |
||
33 | */ |
||
34 | public function __construct(GithubBranchId $id, GithubRepo $repo, $name, GithubCommit $lastCommit) |
||
41 | |||
42 | /** @return GithubBranchId */ |
||
43 | public function getId() |
||
47 | |||
48 | /** @return GithubRepoId */ |
||
49 | public function getRepoId() |
||
53 | |||
54 | /** @return GithubRepo */ |
||
55 | public function getRepo() |
||
59 | |||
60 | /** @return string */ |
||
61 | public function getName() |
||
65 | |||
66 | /** @return GithubCommitId */ |
||
67 | public function getLastCommitId() |
||
71 | |||
72 | /** @return GithubCommit */ |
||
73 | public function getLastCommit() |
||
77 | } |
||
78 |
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.