Test Setup Failed
Branch master (1c631b)
by Scott
03:15
created

DiffFileLoader   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 69
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 69
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getChangedLines() 0 21 4
A getLineHandle() 0 10 3
A getClass() 0 9 2
A getFileHandleName() 0 4 1
A __construct() 0 4 1
1
<?php
2
namespace exussum12\CoverageChecker;
3
4
use InvalidArgumentException;
5
6
class DiffFileLoader
7
{
8
    protected $fileLocation;
9
10
    protected $diffLines = [
11
        DiffLineHandle\NewVersion\NewFile::class,
12
        DiffLineHandle\NewVersion\AddedLine::class,
13
        DiffLineHandle\NewVersion\RemovedLine::class,
14
        DiffLineHandle\NewVersion\DiffStart::class,
15
    ];
16
    protected $handles = [];
17
    protected $diff;
18
19
    public function __construct($fileName)
20
    {
21
        $this->fileLocation = $fileName;
22
        $this->diff = new DiffFileState();
23
    }
24
25
    public function getChangedLines()
26
    {
27
        if ((
28
            !is_readable($this->fileLocation) &&
29
            strpos($this->fileLocation, "php://") !== 0
30
        )) {
31
            throw new InvalidArgumentException("Can't read file");
32
        }
33
34
        $handle = fopen($this->fileLocation, 'r');
35
36
        while (($line = fgets($handle)) !== false) {
0 ignored issues
show
Bug introduced by
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

36
        while (($line = fgets(/** @scrutinizer ignore-type */ $handle)) !== false) {
Loading history...
37
            // process the line read.
38
            $lineHandle = $this->getLineHandle($line);
39
            $lineHandle->handle($line);
40
            $this->diff->incrementCurrentPosition();
41
        }
42
43
        fclose($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fclose() 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

43
        fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
44
45
        return $this->diff->getChangedLines();
46
    }
47
48
    private function getLineHandle($line)
49
    {
50
        foreach ($this->diffLines as $lineType) {
51
            $lineType = $this->getClass($lineType);
52
            if ($lineType->isValid($line)) {
53
                return $lineType;
54
            }
55
        }
56
        //not found, Class it as context
57
        return $this->getClass(DiffLineHandle\ContextLine::class);
58
    }
59
60
    private function getClass($className)
61
    {
62
        if (!isset($this->handles[$this->getFileHandleName($className)])) {
63
            $this->handles[
64
                $this->getFileHandleName($className)
65
            ] = new $className($this->diff);
66
        }
67
68
        return $this->handles[$this->getFileHandleName($className)];
69
    }
70
71
    private function getFileHandleName($namespace)
72
    {
73
        $namespace = explode('\\', $namespace);
74
        return end($namespace);
75
    }
76
}
77