LcovReporter   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 145
Duplicated Lines 4.83 %

Coupling/Cohesion

Components 1
Dependencies 10

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 15
c 6
b 0
f 1
lcom 1
cbo 10
dl 7
loc 145
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A onFinalize() 0 4 1
A writeFileFooter() 0 4 1
A onInitialize() 7 7 1
A onAnalyzeStop() 0 5 1
A writeResult() 0 8 2
A writeFileResult() 0 12 2
A writeFileHeader() 0 10 1
A writeLineResult() 0 12 2
A getTargetLinesFromFile() 0 10 2
A registerTo() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

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
2
3
/**
4
 * This file is part of cloak.
5
 *
6
 * (c) Noritaka Horio <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace cloak\reporter;
13
14
15
use cloak\AnalyzedCoverageResult;
16
use cloak\result\FileResult;
17
use cloak\analyzer\result\LineResult;
18
use cloak\event\InitializeEvent;
19
use cloak\event\AnalyzeStopEvent;
20
use cloak\event\FinalizeEvent;
21
use cloak\writer\FileWriter;
22
use PHPExtra\EventManager\EventManager;
23
24
25
/**
26
 * Class LcovReporter
27
 * @package cloak\reporter
28
 */
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)
55
    {
56
        $this->fileName = $fileName;
57
    }
58
59
    /**
60
     * @param InitializeEvent $event
61
     */
62 View Code Duplication
    public function onInitialize(InitializeEvent $event)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
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)
83
    {
84
        $this->reportWriter = null;
85
    }
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()
130
    {
131
        $this->reportWriter->writeLine(self::END_OF_RECORD);
132
    }
133
134
    /**
135
     * @param \cloak\analyzer\result\LineResult $line
136
     */
137
    private function writeLineResult(LineResult $line)
138
    {
139
140
        $executedCount = $line->isExecuted() ? 1 : 0;
141
        $recordParts = [
142
            $line->getLineNumber(),
143
            $executedCount
144
        ];
145
146
        $record = self::COVERAGE_PREFIX . implode(',', $recordParts);
147
        $this->reportWriter->writeLine($record);
148
    }
149
150
    /**
151
     * @param FileResult $file
152
     * @return array
153
     */
154
    private function getTargetLinesFromFile(FileResult $file)
155
    {
156
        $lineResults = $file->getLineResults();
157
158
        $results = $lineResults->selectLines(function(LineResult $line) {
159
            return $line->isExecuted() || $line->isUnused();
160
        })->all();
161
162
        return $results;
163
    }
164
165
    /**
166
     * @param EventManager $eventManager
167
     */
168
    public function registerTo(EventManager $eventManager)
169
    {
170
        $eventManager->add($this);
171
    }
172
173
}
174