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 |
||
18 | class PrePushTool |
||
19 | { |
||
20 | const PRE_PUSH_HOOK = '<comment>Pre-push hook</comment>'; |
||
21 | /** |
||
22 | * @var QueryBus |
||
23 | */ |
||
24 | private $queryBus; |
||
25 | /** |
||
26 | * @var PrePushOriginalExecutorInterface |
||
27 | */ |
||
28 | private $prePushOriginalExecutor; |
||
29 | /** |
||
30 | * @var OutputInterface |
||
31 | */ |
||
32 | private $output; |
||
33 | /** |
||
34 | * @var CommandBus |
||
35 | */ |
||
36 | private $commandBus; |
||
37 | |||
38 | /** |
||
39 | * PrePushTool constructor. |
||
40 | * |
||
41 | * @param QueryBus $queryBus |
||
42 | * @param PrePushOriginalExecutorInterface $prePushOriginalExecutor |
||
43 | * @param OutputInterface $output |
||
44 | * @param CommandBus $commandBus |
||
45 | */ |
||
46 | 3 | public function __construct( |
|
57 | |||
58 | /** |
||
59 | * @param string $remote |
||
60 | * @param string $url |
||
61 | * |
||
62 | * @throws InvalidPushException |
||
63 | */ |
||
64 | 3 | public function execute($remote, $url) |
|
65 | { |
||
66 | /** @var ConfigurationDataResponse $configurationData */ |
||
67 | 3 | $configurationData = $this->queryBus->handle(new ConfigurationDataFinderQuery()); |
|
68 | 3 | $prePushResponse = $configurationData->getPrePush(); |
|
69 | |||
70 | 3 | if (true === $prePushResponse->isPrePush()) { |
|
71 | 2 | $this->output->writeln(self::PRE_PUSH_HOOK); |
|
72 | 2 | $this->executeOriginalHook($remote, $url, $prePushResponse->getErrorMessage()); |
|
73 | |||
74 | 1 | $phpunitResponse = $prePushResponse->getPhpUnit(); |
|
75 | |||
76 | 1 | View Code Duplication | if (true === $phpunitResponse->isPhpunit()) { |
|
|||
77 | 1 | $this->commandBus->handle( |
|
78 | 1 | new PhpUnitToolCommand( |
|
79 | 1 | $phpunitResponse->isPhpunitRandomMode(), |
|
80 | 1 | $phpunitResponse->getPhpunitOptions(), |
|
81 | 1 | $prePushResponse->getErrorMessage() |
|
82 | 1 | ) |
|
83 | 1 | ); |
|
84 | |||
85 | 1 | $phpunitStrictCoverageResponse = $prePushResponse->getPhpUnitStrictCoverage(); |
|
86 | |||
87 | 1 | if (true === $phpunitStrictCoverageResponse->isPhpunitStrictCoverage()) { |
|
88 | 1 | $this->commandBus->handle( |
|
89 | 1 | new StrictCoverageCommand( |
|
90 | 1 | $phpunitStrictCoverageResponse->getMinimum(), |
|
91 | 1 | $prePushResponse->getErrorMessage() |
|
92 | 1 | ) |
|
93 | 1 | ); |
|
94 | 1 | } |
|
95 | |||
96 | 1 | $phpunitGuardCoverageResponse = $prePushResponse->getPhpUnitGuardCoverage(); |
|
97 | |||
98 | 1 | if (true === $phpunitGuardCoverageResponse->isEnabled()) { |
|
99 | 1 | $this->commandBus->handle( |
|
100 | 1 | new GuardCoverageCommand($phpunitGuardCoverageResponse->getWarningMessage()) |
|
101 | 1 | ); |
|
102 | 1 | } |
|
103 | 1 | } |
|
104 | |||
105 | 1 | $this->output->writeln(GoodJobLogoResponse::paint($prePushResponse->getRightMessage())); |
|
106 | 1 | } |
|
107 | 2 | } |
|
108 | |||
109 | /** |
||
110 | * @param string $remote |
||
111 | * @param string $url |
||
112 | * @param string $errorMessage |
||
113 | * |
||
114 | * @throws InvalidPushException |
||
115 | */ |
||
116 | 2 | private function executeOriginalHook($remote, $url, $errorMessage) |
|
126 | } |
||
127 |
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.