Test Failed
Push — master ( 068d94...d12c1b )
by Alec
07:11
created

CallerDataFormatter::format()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 15
nc 3
nop 1
dl 0
loc 21
ccs 12
cts 12
cp 1
crap 3
rs 9.7666
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
4
namespace AlecRabbit\Accessories\Caller;
5
6
use AlecRabbit\Accessories\Caller\Contracts\CallerConstants;
7
use AlecRabbit\Formatters\Core\AbstractFormatter;
8
use AlecRabbit\Formatters\Core\Formattable;
9
10
class CallerDataFormatter extends AbstractFormatter implements CallerConstants
11
{
12
    /** {@inheritDoc} */
13 3
    public function __construct(?int $options = null)
14
    {
15 3
        parent::__construct($options);
16 3
        $this->options = $options ?? static::SHOW_LINE_AND_FILE;
17 3
    }
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 6
    public function format(Formattable $data): string
23
    {
24 6
        if ($data instanceof CallerData) {
25
            if (null !== $class = $data->getClass()) {
26 4
                return
27 4
                    sprintf(
28 4
                        '%s%s%s%s',
29 4
                        $this->getLineAndFile($data),
30 4
                        $class,
31 4
                        (string)$data->getType(),
32
                        $this->getFunction($data)
33
                    );
34
            }
35 2
            return
36 2
                sprintf(
37 2
                    '%s%s',
38 2
                    $this->getLineAndFile($data),
39
                    $this->getFunction($data)
40
                );
41
        }
42
        return $this->errorMessage($data, CallerData::class);
43
    }
44
45
    /**
46 6
     * @param CallerData $caller
47
     * @return string
48 6
     */
49
    protected function getLineAndFile(CallerData $caller): string
50 4
    {
51 4
        if (($this->options & static::SHOW_LINE_AND_FILE) && self::STR_UNDEFINED !== $caller->getFunction()) {
52 4
            return
53 4
                sprintf(
54
                    '[%s:"%s"] ',
55
                    (string)$caller->getLine(),
56 2
                    (string)$caller->getFile()
57
                );
58
        }
59
        return '';
60
    }
61
62
    /**
63 6
     * @param CallerData $caller
64
     * @return string
65 6
     */
66 6
    protected function getFunction(CallerData $caller): string
67 1
    {
68
        $function = $caller->getFunction();
69 5
        if ($function === self::STR_UNDEFINED) {
70
            return ucfirst($function);
71
        }
72
        return $function . '()';
73
    }
74
}
75