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 |
||
7 | class BreadCrumbsTrail { |
||
8 | |||
9 | /** @var BreadCrumb[] */ |
||
10 | private $crumbs; |
||
11 | |||
12 | /** @var ParameterReader */ |
||
13 | private $reader; |
||
14 | |||
15 | /** |
||
16 | * @param ParameterReader $reader |
||
17 | * @param BreadCrumb[] $crumbs |
||
18 | */ |
||
19 | public function __construct(ParameterReader $reader, array $crumbs) { |
||
23 | |||
24 | /** |
||
25 | * @return bool |
||
26 | */ |
||
27 | public function hasCrumbs() { |
||
30 | |||
31 | /** |
||
32 | * @return array|BreadCrumb[] |
||
33 | */ |
||
34 | public function getCrumbs() { |
||
37 | |||
38 | /** |
||
39 | * @return BreadCrumb |
||
40 | * @throws \Exception If there are no crumbs |
||
41 | */ |
||
42 | public function getLastCrumb() { |
||
48 | |||
49 | /** |
||
50 | * @param Action $action |
||
51 | * @param string $actionId |
||
52 | * @return array|BreadCrumb[] |
||
53 | */ |
||
54 | public function updateCrumbs(Action $action, $actionId) { |
||
69 | |||
70 | public function reset() { |
||
73 | |||
74 | View Code Duplication | private function makeTarget($actionId, Action $action) { |
|
88 | |||
89 | private function readRawParameters(Action $action) { |
||
99 | } |
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.