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

Context::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 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