Completed
Push — master ( bb46b9...a32f93 )
by Scott
02:02
created

PhpStanLoader::getLines()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 9
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 14
rs 9.4285
1
<?php
2
namespace exussum12\CoverageChecker;
3
4
/**
5
 * Class PhpStanLoader
6
 * Used for parsing phpstan standard output
7
 * @package exussum12\CoverageChecker
8
 */
9
class PhpStanLoader implements FileChecker
10
{
11
    protected $lineRegex = '/^\s+(?<lineNumber>[0-9]+)/';
12
    /**
13
     * @var string
14
     */
15
    protected $file;
16
17
    /**
18
     * @var array
19
     */
20
    protected $errors = [];
21
22
    /**
23
     * @var array
24
     */
25
    protected $errorRanges = [];
26
27
    /**
28
     * @param string $filename the path to the phpstan.txt file
29
     */
30
    public function __construct($filename)
31
    {
32
        $this->file = fopen($filename, 'r');
0 ignored issues
show
Documentation Bug introduced by
It seems like fopen($filename, 'r') of type resource is incompatible with the declared type string of property $file.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function getLines()
39
    {
40
        $filename = '';
41
        while (($line = fgets($this->file)) !== false) {
42
            $filename = $this->checkForFileName($line, $filename);
43
            if ($lineNumber = $this->getLineNumber($line)) {
44
                $this->invalidLines
0 ignored issues
show
Bug introduced by
The property invalidLines does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
45
                [$filename]
46
                [$lineNumber] = $this->getMessage($line);
47
            }
48
        }
49
50
        return $this->invalidLines;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function isValidLine($file, $lineNumber)
57
    {
58
        return empty($this->isValidLines[$file][$lineNumber]);
0 ignored issues
show
Bug introduced by
The property isValidLines does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
59
    }
60
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function handleNotFoundFile()
66
    {
67
        return null;
68
    }
69
70
    /**
71
     * @param string $line
72
     * @param string $currentFile
73
     * @return string the currentFileName
74
     */
75
    protected function checkForFilename($line, $currentFile)
76
    {
77
        if (strpos($line, " Line ")) {
78
            return trim(str_replace('Line', '',$line));
79
        }
80
        return $currentFile;
81
    }
82
83
    protected function getLineNumber($line)
84
    {
85
       $matches = [];
86
       if (!preg_match('' . $this->lineRegex, $line, $matches)) {
87
           return false;
88
       }
89
90
       return $matches['lineNumber'];
91
    }
92
93
    protected function getMessage($line)
94
    {
95
        return trim(preg_replace($this->lineRegex, "", $line));
96
    }
97
}
98