Issues (37)

src/Loaders/Generic.php (1 issue)

Labels
Severity
1
<?php
2
namespace exussum12\CoverageChecker\Loaders;
3
4
use exussum12\CoverageChecker\FileChecker;
5
6
/**
7
 * Class Generic
8
 * Used for parsing output on a single line
9
 * @package exussum12\CoverageChecker
10
 */
11
abstract class Generic implements FileChecker
12
{
13
    protected $lineMatch = '';
14
15
    /**
16
     * @var string
17
     */
18
    protected $file;
19
20
    /**
21
     * @var array
22
     */
23
    protected $errors = [];
24
25
    /**
26
     * PhanJsonLoader constructor.
27
     * @param string $file the path to the file containing phan output
28
     */
29
    public function __construct($file)
30
    {
31
        $this->file = $file;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function parseLines(): array
38
    {
39
        $handle = fopen($this->file, 'r');
40
        while (($line = fgets($handle)) !== false) {
0 ignored issues
show
It seems like $handle can also be of type false; however, parameter $handle of fgets() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        while (($line = fgets(/** @scrutinizer ignore-type */ $handle)) !== false) {
Loading history...
41
            if (!$this->checkForFile($line)) {
42
                continue;
43
            }
44
45
            $this->addError($line);
46
        }
47
48
        return array_keys($this->errors);
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function getErrorsOnLine(string $file, int $lineNumber)
55
    {
56
        $errors = [];
57
        if (isset($this->errors[$file][$lineNumber])) {
58
            $errors = $this->errors[$file][$lineNumber];
59
        }
60
61
        return $errors;
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function handleNotFoundFile()
68
    {
69
        return true;
70
    }
71
72
73
    private function checkForFile(string $line)
74
    {
75
        return preg_match($this->lineMatch, $line);
76
    }
77
78
    private function addError(string $line)
79
    {
80
        $matches = [];
81
        if (preg_match($this->lineMatch, $line, $matches)) {
82
            $this->errors
83
                [$matches['fileName']]
84
                [$matches['lineNumber']][] = trim($matches['message']);
85
        }
86
    }
87
}
88