CoverageCheck::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 9
rs 10
c 0
b 0
f 0
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
    public function __construct(
50
        DiffFileLoader $diff,
51
        FileChecker $fileChecker,
52
        FileMatcher $matcher
53
    ) {
54
        $this->diff = $diff;
55
        $this->fileChecker = $fileChecker;
56
        $this->matcher = $matcher;
57
        $this->cache = new stdClass;
58
    }
59
60
    /**
61
     * array of uncoveredLines and coveredLines
62
     */
63
    public function getCoveredLines(): array
64
    {
65
        $this->getDiff();
66
67
        $coveredFiles = $this->fileChecker->parseLines();
68
        $this->uncoveredLines = [];
69
        $this->coveredLines = [];
70
71
        $diffFiles = array_keys($this->cache->diff);
72
        foreach ($diffFiles as $file) {
73
            $matchedFile = $this->findFile($file, $coveredFiles);
74
            if ($matchedFile !== '') {
75
                $this->matchLines($file, $matchedFile);
76
            }
77
        }
78
79
        return [
80
            'uncoveredLines' => $this->uncoveredLines,
81
            'coveredLines' => $this->coveredLines,
82
        ];
83
    }
84
85
    protected function addUnCoveredLine(string $file, int $line, array $message)
86
    {
87
        if (!isset($this->uncoveredLines[$file])) {
88
            $this->uncoveredLines[$file] = [];
89
        }
90
91
        $this->uncoveredLines[$file][$line] = $message;
92
    }
93
94
    protected function addCoveredLine(string $file, int $line)
95
    {
96
        if (!isset($this->coveredLines[$file])) {
97
            $this->coveredLines[$file] = [];
98
        }
99
100
        $this->coveredLines[$file][] = $line;
101
    }
102
103
    protected function matchLines(string $fileName, string $matchedFile)
104
    {
105
        foreach ($this->cache->diff[$fileName] as $line) {
106
            $messages = $this->fileChecker->getErrorsOnLine($matchedFile, $line);
107
108
            if (is_null($messages)) {
109
                continue;
110
            }
111
112
            if (count($messages) == 0) {
113
                $this->addCoveredLine($fileName, $line);
114
                continue;
115
            }
116
117
118
            $this->addUnCoveredLine(
119
                $fileName,
120
                $line,
121
                $messages
122
            );
123
        }
124
    }
125
126
    protected function addCoveredFile(string $file)
127
    {
128
        foreach ($this->cache->diff[$file] as $line) {
129
            $this->addCoveredLine($file, $line);
130
        }
131
    }
132
133
    protected function addUnCoveredFile(string $file)
134
    {
135
        foreach ($this->cache->diff[$file] as $line) {
136
            $this->addUnCoveredLine($file, $line, ['No Cover']);
137
        }
138
    }
139
140
    protected function getDiff(): array
141
    {
142
        if (empty($this->cache->diff)) {
143
            $this->cache->diff = $this->diff->getChangedLines();
144
        }
145
146
        return $this->cache->diff;
147
    }
148
149
    protected function handleFileNotFound(string $file)
150
    {
151
        $unMatchedFile = $this->fileChecker->handleNotFoundFile();
152
153
        if ($unMatchedFile === true) {
154
            $this->addCoveredFile($file);
155
        }
156
157
        if ($unMatchedFile === false) {
158
            $this->addUnCoveredFile($file);
159
        }
160
    }
161
162
    protected function findFile(string $file, array $coveredFiles): string
163
    {
164
        try {
165
            return $this->matcher->match($file, $coveredFiles);
166
        } catch (Exceptions\FileNotFound $e) {
167
            $this->handleFileNotFound($file);
168
            return '';
169
        }
170
    }
171
}
172