Passed
Push — master ( b3d8d2...49adaa )
by Waaaaaaaaaa
02:33
created

Context   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
eloc 17
dl 0
loc 33
ccs 17
cts 17
cp 1
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 5
    public function __construct(string $file, int $line)
15
    {
16 5
        $this->file = $file;
17 5
        $this->line = $line;
18 5
    }
19
20 4
    public function getPlaceInFile(int $linesBefore = 4, int $linesAfter = 4): array
21
    {
22 4
        $context = [];
23
24 4
        $offset = $this->line - $linesBefore - 1;
25
26 4
        if ($offset < 0) {
27 2
            $linesBefore = 0;
28 2
            $offset = 0;
29
        }
30
31 4
        $file = new SplFileObject($this->file, 'rb');
32 4
        $iterator = new LimitIterator($file, $offset, $linesBefore + $linesAfter + 1);
33 4
        $index = $offset + 1;
34
35 4
        foreach ($iterator as $text) {
36 4
            $context[$index] = $text;
37 4
            $index++;
38
        }
39
40 4
        return $context;
41
    }
42
}
43