AbstractFormatter   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 12
c 2
b 0
f 0
dl 0
loc 34
ccs 12
cts 12
cp 1
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A format() 0 11 2
A __construct() 0 3 1
A errorMessage() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Formatters\Core;
4
5
use AlecRabbit\Formatters\Contracts\FormatterInterface;
6
use AlecRabbit\Reports\Core\Formattable;
7
use AlecRabbit\Reports\DefaultReport;
8
9
abstract class AbstractFormatter implements FormatterInterface
10
{
11
    /** @var int */
12
    protected $options;
13
14
    /** {@inheritDoc} */
15 4
    public function __construct(?int $options = null)
16
    {
17 4
        $this->options = $options ?? 0;
18 4
    }
19
20
    /** {@inheritDoc} */
21 2
    public function format(Formattable $formattable): string
22
    {
23 2
        if ($formattable instanceof DefaultReport) {
24
            return
25 1
                sprintf(
26 1
                    '[%s]: got %s',
27 1
                    get_class($this),
28 1
                    get_class($formattable)
29
                );
30
        }
31 1
        return $this->errorMessage($formattable, DefaultReport::class);
32
    }
33
34
    /**
35
     * @param object $data
36
     * @param string $class
37
     * @return string
38
     */
39 1
    protected function errorMessage(object $data, string $class): string
40
    {
41
        return
42 1
            '[' . get_class($this) . '] ERROR: ' . $class . ' expected, ' . get_class($data) . ' given.';
43
    }
44
}
45