Completed
Push — master ( b96870...b9bbf6 )
by Dusan
11s
created

AbstractEmitter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
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 38
ccs 9
cts 18
cp 0.5
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 10 1
format() 0 1 ?
A formatTraceRecord() 0 12 2
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 bool
13
     */
14
    protected $includeTrace;
15
16 3
    public function __construct(bool $includeTrace = true)
17
    {
18 3
        $this->includeTrace = $includeTrace;
19 3
    }
20
21 3
    public function __invoke(Throwable $throwable) : Throwable
22
    {
23 3
        ob_start();
24 3
        $output = $this->format($throwable);
25 3
        ob_end_clean();
26
27 3
        echo $output;
28
29 3
        return $throwable;
30
    }
31
32
    abstract protected function format(Throwable $throwable) : string;
33
34
    final protected function formatTraceRecord(array $traceRecord, int $index, int $traceLength) : string
35
    {
36
        return sprintf(
37
            '#%s %s%s%s in %s:%s',
38
            $traceLength - $index - 1,
39
            $traceRecord['class'] ?? '',
40
            isset($traceRecord['class'], $traceRecord['function']) ? ':' : '',
41
            $traceRecord['function'] ?? '',
42
            $traceRecord['file'] ?? 'unknown',
43
            $traceRecord['line'] ?? 0
44
        );
45
    }
46
}
47