exussum12 /
coverageChecker
| 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(): array |
||||
| 26 | { |
||||
| 27 | if (( |
||||
| 28 | !is_readable($this->fileLocation) && |
||||
| 29 | strpos($this->fileLocation, "php://") !== 0 |
||||
| 30 | )) { |
||||
| 31 | throw new InvalidArgumentException("Can't read file", 1); |
||||
| 32 | } |
||||
| 33 | |||||
| 34 | $handle = fopen($this->fileLocation, 'r'); |
||||
| 35 | |||||
| 36 | while (($line = fgets($handle)) !== false) { |
||||
|
0 ignored issues
–
show
Bug
introduced
by
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
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
Loading history...
|
|||||
| 44 | |||||
| 45 | return $this->diff->getChangedLines(); |
||||
| 46 | } |
||||
| 47 | |||||
| 48 | private function getLineHandle(string $line): DiffLineHandle |
||||
| 49 | { |
||||
| 50 | foreach ($this->diffLines as $lineType) { |
||||
| 51 | $lineType = $this->getClass($lineType); |
||||
| 52 | if ($lineType->isValid($line)) { |
||||
| 53 | return $lineType; |
||||
| 54 | } |
||||
| 55 | } |
||||
| 56 | // the line doesn't have a special meaning, its probably context |
||||
| 57 | return $this->getClass(DiffLineHandle\ContextLine::class); |
||||
| 58 | } |
||||
| 59 | |||||
| 60 | private function getClass(string $className): DiffLineHandle |
||||
| 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(string $namespace): string |
||||
| 72 | { |
||||
| 73 | $namespace = explode('\\', $namespace); |
||||
| 74 | return end($namespace); |
||||
| 75 | } |
||||
| 76 | } |
||||
| 77 |