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 |
||
| 29 | class LcovReporter |
||
| 30 | implements Reporter, InitializeEventListener, FinalizeEventListener, AnalyzeStopEventListener |
||
| 31 | { |
||
| 32 | |||
| 33 | const SOURCE_FILE_PREFIX = 'SF:'; |
||
| 34 | const COVERAGE_PREFIX = 'DA:'; |
||
| 35 | const END_OF_RECORD = 'end_of_record'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var string Report file name |
||
| 39 | */ |
||
| 40 | private $fileName; |
||
| 41 | |||
| 42 | |||
| 43 | /** |
||
| 44 | * @var \cloak\writer\FileWriter |
||
| 45 | */ |
||
| 46 | private $reportWriter; |
||
| 47 | |||
| 48 | |||
| 49 | /** |
||
| 50 | * @param string|null $fileName |
||
| 51 | * @throws \cloak\writer\DirectoryNotFoundException |
||
| 52 | * @throws \cloak\writer\DirectoryNotWritableException |
||
| 53 | */ |
||
| 54 | public function __construct($fileName) |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param InitializeEvent $event |
||
| 61 | */ |
||
| 62 | View Code Duplication | public function onInitialize(InitializeEvent $event) |
|
|
|
|||
| 63 | { |
||
| 64 | $reportDirectory = $event->getReportDirectory(); |
||
| 65 | $reportFile = $reportDirectory->join($this->fileName); |
||
| 66 | |||
| 67 | $this->reportWriter = new FileWriter( $reportFile->stringify() ); |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @param \cloak\event\AnalyzeStopEvent $event |
||
| 72 | */ |
||
| 73 | public function onAnalyzeStop(AnalyzeStopEvent $event) |
||
| 74 | { |
||
| 75 | $result = $event->getResult(); |
||
| 76 | $this->writeResult($result); |
||
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param FinalizeEvent $event |
||
| 81 | */ |
||
| 82 | public function onFinalize(FinalizeEvent $event) |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param AnalyzedCoverageResult $result |
||
| 89 | */ |
||
| 90 | private function writeResult(AnalyzedCoverageResult $result) |
||
| 91 | { |
||
| 92 | $files = $result->getFiles(); |
||
| 93 | |||
| 94 | foreach($files as $file) { |
||
| 95 | $this->writeFileResult($file); |
||
| 96 | } |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @param FileResult $file |
||
| 101 | */ |
||
| 102 | private function writeFileResult(FileResult $file) |
||
| 103 | { |
||
| 104 | $this->writeFileHeader($file); |
||
| 105 | |||
| 106 | $targetLines = $this->getTargetLinesFromFile($file); |
||
| 107 | |||
| 108 | foreach ($targetLines as $targetLine) { |
||
| 109 | $this->writeLineResult($targetLine); |
||
| 110 | } |
||
| 111 | |||
| 112 | $this->writeFileFooter(); |
||
| 113 | } |
||
| 114 | |||
| 115 | /** |
||
| 116 | * @param FileResult $file |
||
| 117 | */ |
||
| 118 | private function writeFileHeader(FileResult $file) |
||
| 119 | { |
||
| 120 | $parts = [ |
||
| 121 | self::SOURCE_FILE_PREFIX, |
||
| 122 | $file->getPath() |
||
| 123 | ]; |
||
| 124 | |||
| 125 | $record = implode('', $parts); |
||
| 126 | $this->reportWriter->writeLine($record); |
||
| 127 | } |
||
| 128 | |||
| 129 | private function writeFileFooter() |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param \cloak\analyzer\result\LineResult $line |
||
| 136 | */ |
||
| 137 | private function writeLineResult(LineResult $line) |
||
| 138 | { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param FileResult $file |
||
| 152 | * @return array |
||
| 153 | */ |
||
| 154 | private function getTargetLinesFromFile(FileResult $file) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @param EventManager $eventManager |
||
| 167 | */ |
||
| 168 | public function registerTo(EventManager $eventManager) |
||
| 172 | |||
| 173 | } |
||
| 174 |
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.