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 |
||
26 | class TextReporter |
||
27 | implements Reporter, InitializeEventListener, AnalyzeStopEventListener |
||
28 | { |
||
29 | |||
30 | |||
31 | /** |
||
32 | * @var \cloak\writer\ResultConsoleWriter |
||
33 | */ |
||
34 | private $console; |
||
35 | |||
36 | |||
37 | /** |
||
38 | * @param \cloak\event\InitializeEvent $event |
||
39 | */ |
||
40 | public function onInitialize(InitializeEvent $event) |
||
41 | { |
||
42 | $coverageBounds = $event->getCoverageBounds(); |
||
43 | $this->console = new ResultConsoleWriter($coverageBounds); |
||
44 | } |
||
45 | |||
46 | /** |
||
47 | * @param \cloak\event\AnalyzeStopEvent $event |
||
48 | */ |
||
49 | public function onAnalyzeStop(AnalyzeStopEvent $event) |
||
53 | |||
54 | /** |
||
55 | * @param AnalyzedCoverageResult $result |
||
56 | */ |
||
57 | public function reportResult(AnalyzedCoverageResult $result) |
||
67 | |||
68 | /** |
||
69 | * @param \cloak\result\FileResult $file |
||
70 | */ |
||
71 | protected function reportFile(FileResult $file) |
||
88 | |||
89 | /** |
||
90 | * @param AnalyzedCoverageResult $result |
||
91 | */ |
||
92 | View Code Duplication | protected function writeTotalCoverage(AnalyzedCoverageResult $result) |
|
99 | |||
100 | /** |
||
101 | * @param EventManager $eventManager |
||
102 | */ |
||
103 | public function registerTo(EventManager $eventManager) |
||
107 | |||
108 | } |
||
109 |
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.