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

AbstractEmitter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 40
ccs 9
cts 18
cp 0.5
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
format() 0 1 ?
A formatTraceRecord() 0 12 2
A __construct() 0 4 1
A __invoke() 0 10 1
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