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

ErrorInfoHelper   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 0
cbo 1
dl 0
loc 55
ccs 26
cts 26
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getContent() 0 4 1
A formatTrace() 0 26 5
A getFileContent() 0 20 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
}