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 PreCommitProcessor |
||
11 | { |
||
12 | /** |
||
13 | * @var IOInterface |
||
14 | */ |
||
15 | private $io; |
||
16 | /** |
||
17 | * @var PhpUnitGuardCoverageConfigurator |
||
18 | */ |
||
19 | private $phpUnitGuardCoverageConfigurator; |
||
20 | |||
21 | /** |
||
22 | * PreCommitProcessor constructor. |
||
23 | * |
||
24 | * @param PhpUnitGuardCoverageConfigurator $phpUnitGuardCoverageConfigurator |
||
25 | */ |
||
26 | 3 | public function __construct(PhpUnitGuardCoverageConfigurator $phpUnitGuardCoverageConfigurator) |
|
30 | |||
31 | /** |
||
32 | * @param PreCommit $preCommitData |
||
33 | * @param IOInterface $input |
||
34 | * |
||
35 | * @return PreCommit |
||
36 | */ |
||
37 | 3 | View Code Duplication | public function process(PreCommit $preCommitData, IOInterface $input) |
|
|||
38 | { |
||
39 | 3 | $this->io = $input; |
|
40 | 3 | if (true === $preCommitData->isUndefined()) { |
|
41 | 2 | $preCommitData = PreCommitConfigurator::configure($this->io, $preCommitData); |
|
42 | 2 | } |
|
43 | |||
44 | 3 | if (true === $preCommitData->isEnabled()) { |
|
45 | 2 | $preCommitData = $preCommitData->setExecute($this->configTools($preCommitData->getExecute())); |
|
46 | 2 | } |
|
47 | |||
48 | 3 | return $preCommitData; |
|
49 | } |
||
50 | |||
51 | /** |
||
52 | * @param ExecuteInterface $execute |
||
53 | * |
||
54 | * @return ExecuteInterface |
||
55 | */ |
||
56 | 2 | private function configTools(ExecuteInterface $execute) |
|
71 | } |
||
72 |
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.