Completed
Push — master ( 06a790...975a47 )
by maxime
03:26
created

ErrorInfoHelper::getFileContent()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 9.6
c 0
b 0
f 0
cc 3
nc 3
nop 2
crap 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Aidphp\Framework;
6
7
class ErrorInfoHelper
8
{
9 2
    public function getContent(ErrorInfo $error): string
10
    {
11 2
        return $this->getFileContent($error->getFile(), $error->getLine());
12
    }
13
14 2
    public function formatTrace(ErrorInfo $error): array
15
    {
16 2
        $traces = [];
17
18 2
        foreach ($error->getTrace() as $trace)
19
        {
20 2
            if (! isset($trace['file']))
21
            {
22 1
                continue;
23
            }
24
25 2
            if (! isset($trace['line']))
26
            {
27 1
                continue;
28
            }
29
30 1
            $traces[] = [
31 1
                'file' => $trace['file'],
32 1
                'line' => $trace['line'],
33 1
                'func' => (isset($trace['class']) ? $trace['class'] . '::' : '') . $trace['function'],
34 1
                'code' => $this->getFileContent($trace['file'], $trace['line']),
35
            ];
36
        }
37
38 2
        return $traces;
39
    }
40
41 3
    private function getFileContent(string $file, int $line): string
42
    {
43 3
        if (! is_file($file))
44
        {
45 1
            return '';
46
        }
47
48 2
        $content = file_get_contents($file);
49 2
        $lines   = explode("\n", $content);
50 2
        $start   = $line - 5;
51 2
        $length  = 10;
52 2
        if ($start < 0)
53
        {
54 1
            $start = 0;
55
        }
56
57 2
        $code = array_slice($lines, $start, $length, true);
58
59 2
        return implode("\n", $code);
60
    }
61
}