AbstractFormatter::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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