Test Failed
Push — master ( 5f8db2...b3d8d2 )
by Waaaaaaaaaa
02:26
created

Context   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
eloc 17
dl 0
loc 33
ccs 0
cts 17
cp 0
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getPlaceInFile() 0 21 3
A __construct() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Logfile\Inspection;
4
5
use LimitIterator;
6
use SplFileObject;
7
8
class Context
9
{
10
    protected $file;
11
12
    protected $line;
13
14
    public function __construct(string $file, int $line)
15
    {
16
        $this->file = $file;
17
        $this->line = $line;
18
    }
19
20
    public function getPlaceInFile(int $linesBefore = 4, int $linesAfter = 4): array
21
    {
22
        $context = [];
23
24
        $offset = $this->line - $linesBefore - 1;
25
26
        if ($offset < 0) {
27
            $linesBefore = 0;
28
            $offset = 0;
29
        }
30
31
        $file = new SplFileObject($this->file, 'rb');
32
        $iterator = new LimitIterator($file, $offset, $linesBefore + $linesAfter + 1);
33
        $index = $offset + 1;
34
35
        foreach ($iterator as $text) {
36
            $context[$index] = $text;
37
            $index++;
38
        }
39
40
        return $context;
41
    }
42
}
43