Passed
Push — master ( 3c333b...51dbda )
by Alec
01:37
created

CallerDataFormatter::assertOptions()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

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