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 |
||
30 | class MarkdownReporter |
||
31 | implements Reporter, InitializeEventListener, FinalizeEventListener, AnalyzeStartEventListener, AnalyzeStopEventListener |
||
32 | { |
||
33 | |||
34 | |||
35 | const TABLE_SEPARATOR_CHAR = '|'; |
||
36 | |||
37 | const ALIGN_LEFT = ':--'; |
||
38 | const ALIGN_RIGTH = '--:'; |
||
39 | |||
40 | private $columnHeaders = [ |
||
41 | ' No. ', |
||
42 | ' File ', |
||
43 | ' Line ', |
||
44 | ' Coverage ' |
||
45 | ]; |
||
46 | |||
47 | private $columnHeaderAlignments = [ |
||
48 | self::ALIGN_RIGTH, |
||
49 | self::ALIGN_LEFT, |
||
50 | self::ALIGN_RIGTH, |
||
51 | self::ALIGN_RIGTH |
||
52 | ]; |
||
53 | |||
54 | /** |
||
55 | * @var string Report file name |
||
56 | */ |
||
57 | private $fileName; |
||
58 | |||
59 | /** |
||
60 | * @var CoverageBounds |
||
61 | */ |
||
62 | private $bounds; |
||
63 | |||
64 | /** |
||
65 | * @var \cloak\writer\FileWriter |
||
66 | */ |
||
67 | private $reportWriter; |
||
68 | |||
69 | /** |
||
70 | * @var \DateTimeImmutable |
||
71 | */ |
||
72 | private $generatedAt; |
||
73 | |||
74 | |||
75 | /** |
||
76 | * @param string $fileName |
||
77 | */ |
||
78 | public function __construct ($fileName) |
||
79 | { |
||
80 | $this->fileName = $fileName; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param \cloak\event\InitializeEvent $event |
||
85 | */ |
||
86 | View Code Duplication | public function onInitialize(InitializeEvent $event) |
|
|
|||
87 | { |
||
88 | $this->bounds = $event->getCoverageBounds(); |
||
89 | |||
90 | $reportDirectory = $event->getReportDirectory(); |
||
91 | $reportFile = $reportDirectory->join($this->fileName); |
||
92 | |||
93 | $this->reportWriter = new FileWriter( $reportFile->stringify() ); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param AnalyzeStartEvent $event |
||
98 | */ |
||
99 | public function onAnalyzeStart(AnalyzeStartEvent $event) |
||
103 | |||
104 | /** |
||
105 | * @param AnalyzeStopEvent $event |
||
106 | */ |
||
107 | public function onAnalyzeStop(AnalyzeStopEvent $event) |
||
111 | |||
112 | /** |
||
113 | * @param FinalizeEvent $event |
||
114 | */ |
||
115 | public function onFinalize(FinalizeEvent $event) |
||
119 | |||
120 | /** |
||
121 | * @param AnalyzedCoverageResult $result |
||
122 | */ |
||
123 | private function writeMarkdownReport(AnalyzedCoverageResult $result) |
||
129 | |||
130 | private function writeTitle() |
||
135 | |||
136 | private function writeDescription() |
||
144 | |||
145 | /** |
||
146 | * @param AnalyzedCoverageResult $result |
||
147 | */ |
||
148 | private function writeResult(AnalyzedCoverageResult $result) |
||
165 | |||
166 | /** |
||
167 | * @param string $title |
||
168 | * @param CoverageResultNodeCollection $files |
||
169 | */ |
||
170 | private function writeGroup($title, CoverageResultNodeCollection $files) |
||
176 | |||
177 | /** |
||
178 | * @param string $title |
||
179 | */ |
||
180 | private function writeResultHeader($title) |
||
185 | |||
186 | private function writeFilesResultHeader() |
||
194 | |||
195 | /** |
||
196 | * @param CoverageResultNodeCollection $files |
||
197 | */ |
||
198 | private function writeFileResults(CoverageResultNodeCollection $files) |
||
209 | |||
210 | /** |
||
211 | * @param int $orderNumber |
||
212 | * @param FileResult $file |
||
213 | */ |
||
214 | private function writeFileResult($orderNumber, FileResult $file) |
||
234 | |||
235 | /** |
||
236 | * @param array $values |
||
237 | * @return string |
||
238 | */ |
||
239 | private function toTableRow(array $values) |
||
246 | |||
247 | /** |
||
248 | * @param EventManager $eventManager |
||
249 | */ |
||
250 | public function registerTo(EventManager $eventManager) |
||
254 | |||
255 | } |
||
256 |
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.