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 |
||
28 | class TreeReporter |
||
29 | implements Reporter, InitializeEventListener, AnalyzeStopEventListener, CoverageResultVisitor |
||
30 | { |
||
31 | |||
32 | const IDENT_SIZE = 2; |
||
33 | |||
34 | /** |
||
35 | * @var \cloak\writer\ResultConsoleWriter |
||
36 | */ |
||
37 | private $console; |
||
38 | |||
39 | |||
40 | /** |
||
41 | * @var int |
||
42 | */ |
||
43 | private $indent; |
||
44 | |||
45 | |||
46 | public function __construct() |
||
50 | |||
51 | /** |
||
52 | * @param \cloak\event\InitializeEvent $event |
||
53 | */ |
||
54 | public function onInitialize(InitializeEvent $event) |
||
55 | { |
||
56 | $coverageBounds = $event->getCoverageBounds(); |
||
57 | $this->console = new ResultConsoleWriter($coverageBounds); |
||
58 | } |
||
59 | |||
60 | /** |
||
61 | * @param \cloak\event\AnalyzeStopEvent $event |
||
62 | */ |
||
63 | public function onAnalyzeStop(AnalyzeStopEvent $event) |
||
64 | { |
||
65 | $result = $event->getResult(); |
||
66 | |||
67 | $this->writeHeader($result); |
||
68 | $this->writeChildResults($result); |
||
69 | $this->writeTotalCoverage($event->getResult()); |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @param CoverageResultNode $result |
||
74 | */ |
||
75 | public function visit(CoverageResultNode $result) |
||
79 | |||
80 | /** |
||
81 | * @param CoverageResultNode $result |
||
82 | */ |
||
83 | protected function writeHeader(CoverageResultNode $result) |
||
88 | |||
89 | /** |
||
90 | * @param CoverageResultNode $result |
||
91 | */ |
||
92 | protected function writeResult(CoverageResultNode $result) |
||
99 | |||
100 | /** |
||
101 | * @param CoverageResultNode $result |
||
102 | */ |
||
103 | protected function writeChildResults(CoverageResultNode $result) |
||
111 | |||
112 | protected function writeCoverageResult(CoverageResultNode $result) |
||
122 | |||
123 | /** |
||
124 | * @param AnalyzedCoverageResult $result |
||
125 | */ |
||
126 | View Code Duplication | protected function writeTotalCoverage(AnalyzedCoverageResult $result) |
|
133 | |||
134 | /** |
||
135 | * @param EventManager $eventManager |
||
136 | */ |
||
137 | public function registerTo(EventManager $eventManager) |
||
141 | |||
142 | } |
||
143 |
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.