CallerDataFormatter   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 29
c 3
b 0
f 0
dl 0
loc 63
ccs 30
cts 30
cp 1
rs 10
wmc 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A format() 0 21 3
A getFunction() 0 7 2
A getLineAndFile() 0 11 3
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\Reports\Core\Formattable;
9
10
class CallerDataFormatter extends AbstractFormatter implements CallerConstants
11
{
12
    /** {@inheritDoc} */
13 9
    public function __construct(?int $options = null)
14
    {
15 9
        parent::__construct($options);
16 9
        $this->options = $options ?? static::SHOW_LINE_AND_FILE;
17 9
    }
18
19
    /**
20
     * {@inheritdoc}
21
     */
22 8
    public function format(Formattable $data): string
23
    {
24 8
        if ($data instanceof CallerData) {
25 7
            if (null !== $class = $data->getClass()) {
26
                return
27 5
                    sprintf(
28 5
                        '%s%s%s%s',
29 5
                        $this->getLineAndFile($data),
30 5
                        $class,
31 5
                        (string)$data->getType(),
32 5
                        $this->getFunction($data)
33
                    );
34
            }
35
            return
36 2
                sprintf(
37 2
                    '%s%s',
38 2
                    $this->getLineAndFile($data),
39 2
                    $this->getFunction($data)
40
                );
41
        }
42 1
        return $this->errorMessage($data, CallerData::class);
43
    }
44
45
    /**
46
     * @param CallerData $caller
47
     * @return string
48
     */
49 7
    protected function getLineAndFile(CallerData $caller): string
50
    {
51 7
        if (($this->options & static::SHOW_LINE_AND_FILE) && self::STR_UNDEFINED !== $caller->getFunction()) {
52
            return
53 5
                sprintf(
54 5
                    '[%s:"%s"] ',
55 5
                    (string)$caller->getLine(),
56 5
                    (string)$caller->getFile()
57
                );
58
        }
59 2
        return '';
60
    }
61
62
    /**
63
     * @param CallerData $caller
64
     * @return string
65
     */
66 7
    protected function getFunction(CallerData $caller): string
67
    {
68 7
        $function = $caller->getFunction();
69 7
        if ($function === self::STR_UNDEFINED) {
70 1
            return ucfirst($function);
71
        }
72 6
        return $function . '()';
73
    }
74
}
75