Passed
Pull Request — master (#26)
by Scott
02:03
created

CoverageCheck::handleFileNotFound()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 12
rs 9.4285
1
<?php
2
namespace exussum12\CoverageChecker;
3
4
use stdClass;
5
6
/**
7
 * Class CoverageCheck
8
 * @package exussum12\CoverageChecker
9
 */
10
class CoverageCheck
11
{
12
    /**
13
     * @var DiffFileLoader
14
     */
15
    protected $diff;
16
17
    /**
18
     * @var FileChecker
19
     */
20
    protected $fileChecker;
21
22
    /**
23
     * @var FileMatcher
24
     */
25
    protected $matcher;
26
27
    /**
28
     * @var stdClass
29
     */
30
    protected $cache;
31
32
    /**
33
     * @var array
34
     */
35
    protected $uncoveredLines = [];
36
37
    /**
38
     * @var array
39
     */
40
    protected $coveredLines = [];
41
42
    /**
43
     * CoverageCheck constructor.
44
     * This class is used for filtering the "checker" by the diff
45
     * For example if the checker is phpunit, this class filters the phpunit
46
     * output by the diff of the pull request. giving only the common lines in
47
     * each
48
     *
49
     * @param DiffFileLoader $diff
50
     * @param FileChecker $fileChecker
51
     * @param FileMatcher $matcher
52
     */
53
    public function __construct(
54
        DiffFileLoader $diff,
55
        FileChecker $fileChecker,
56
        FileMatcher $matcher
57
    ) {
58
        $this->diff = $diff;
59
        $this->fileChecker = $fileChecker;
60
        $this->matcher = $matcher;
61
        $this->cache = new stdClass;
62
    }
63
64
    /**
65
     * array of uncoveredLines and coveredLines
66
     * @return array
67
     */
68
    public function getCoveredLines()
69
    {
70
        $this->getDiff();
71
72
        $coveredFiles = $this->fileChecker->parseLines();
73
        $this->uncoveredLines = [];
74
        $this->coveredLines = [];
75
76
        $diffFiles = array_keys($this->cache->diff);
77
        foreach ($diffFiles as $file) {
78
            $matchedFile = $this->findFile($file, $coveredFiles);
79
            if ($matchedFile !== false) {
80
                $this->matchLines($file, $matchedFile);
81
            }
82
        }
83
84
        return [
85
            'uncoveredLines' => $this->uncoveredLines,
86
            'coveredLines' => $this->coveredLines,
87
        ];
88
    }
89
90
    /**
91
     * @param string $file the filename containing the uncovered line
92
     * @param int $line the number of the uncovered line
93
     * @param string $message the message showing why its uncovered
94
     */
95
    protected function addUnCoveredLine($file, $line, $message)
96
    {
97
        if (!isset($this->uncoveredLines[$file])) {
98
            $this->uncoveredLines[$file] = [];
99
        }
100
101
        $this->uncoveredLines[$file][$line] = $message;
102
    }
103
104
    /**
105
     * @param string $file the filename containing the covered line
106
     * @param int $line the number of the covered line
107
     */
108
    protected function addCoveredLine($file, $line)
109
    {
110
        if (!isset($this->coveredLines[$file])) {
111
            $this->coveredLines[$file] = [];
112
        }
113
114
        $this->coveredLines[$file][] = $line;
115
    }
116
117
    /**
118
     * @param string $fileName the file name in the diff
119
     * @param string $matchedFile the file name of the matched file
120
     */
121
    protected function matchLines($fileName, $matchedFile)
122
    {
123
        foreach ($this->cache->diff[$fileName] as $line) {
124
            $validLine = $this->fileChecker->getErrorsOnLine($matchedFile, $line);
125
126
            if (is_null($validLine)) {
127
                continue;
128
            }
129
130
            if (count($validLine) == 0) {
131
                $this->addCoveredLine($fileName, $line);
132
                continue;
133
            }
134
135
            $message = $validLine ?: "No cover";
136
137
            $this->addUnCoveredLine(
138
                $fileName,
139
                $line,
140
                $message
0 ignored issues
show
Bug introduced by
It seems like $message defined by $validLine ?: 'No cover' on line 135 can also be of type array; however, exussum12\CoverageChecke...eck::addUnCoveredLine() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
141
            );
142
        }
143
    }
144
145
    protected function addCoveredFile($file)
146
    {
147
        foreach ($this->cache->diff[$file] as $line) {
148
            $this->addCoveredLine($file, $line);
149
        }
150
    }
151
152
    protected function addUnCoveredFile($file)
153
    {
154
        foreach ($this->cache->diff[$file] as $line) {
155
            $this->addUnCoveredLine($file, $line, 0);
156
        }
157
    }
158
159
    protected function getDiff()
160
    {
161
        if (empty($this->cache->diff)) {
162
            $this->cache->diff = $this->diff->getChangedLines();
163
        }
164
165
        return $this->cache->diff;
166
    }
167
168
    protected function handleFileNotFound($file)
169
    {
170
        $unMatchedFile = $this->fileChecker->handleNotFoundFile();
171
172
        if ($unMatchedFile === true) {
173
            $this->addCoveredFile($file);
174
        }
175
176
        if ($unMatchedFile === false) {
177
            $this->addUnCoveredFile($file);
178
        }
179
    }
180
181
    protected function findFile($file, $coveredFiles)
182
    {
183
        try {
184
            return $this->matcher->match($file, $coveredFiles);
185
        } catch (Exceptions\FileNotFound $e) {
186
            $this->handleFileNotFound($file);
187
            return false;
188
        }
189
    }
190
}
191