Completed
Pull Request — master (#5)
by Nikola
01:43
created

AbstractEmitter::formatTraceRecord()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 12
ccs 0
cts 9
cp 0
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 9
nc 1
nop 3
crap 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WeCodeIn\ErrorHandling\Emitter;
6
7
use Throwable;
8
9
abstract class AbstractEmitter implements EmitterInterface
10
{
11
    /**
12
     * @var array
13
     */
14
    protected $options = [
15
        'includeTrace' => true,
16
    ];
17
18 3
    public function __construct(array $options = [])
19
    {
20 3
        $this->options = array_merge($this->options, $options);
21 3
    }
22
23 3
    public function __invoke(Throwable $throwable) : Throwable
24
    {
25 3
        ob_start();
26 3
        $output = $this->format($throwable);
27 3
        ob_end_clean();
28
29 3
        echo $output;
30
31 3
        return $throwable;
32
    }
33
34
    abstract protected function format(Throwable $throwable) : string;
35
36
    final protected function formatTraceRecord(array $traceRecord, int $index, int $traceLength) : string
37
    {
38
        return sprintf(
39
            '#%s %s%s%s in %s:%s',
40
            $traceLength - $index - 1,
41
            $traceRecord['class'] ?? '',
42
            isset($traceRecord['class'], $traceRecord['function']) ? ':' : '',
43
            $traceRecord['function'] ?? '',
44
            $traceRecord['file'] ?? 'unknown',
45
            $traceRecord['line'] ?? 0
46
        );
47
    }
48
}
49