Passed
Push — master ( 6b33a9...df14c7 )
by Alec
01:59
created

CallerDataFormatter::getLineAndFile()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2.0116

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 1
dl 0
loc 11
ccs 6
cts 7
cp 0.8571
crap 2.0116
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
9
class CallerDataFormatter implements CallerDataFormatterInterface, CallerConstants
10
{
11 4
    public function process(CallerData $data): string
12
    {
13 4
        if (null !== $class = $data->getClass()) {
14
            return
15 3
                sprintf(
16 3
                    '%s%s%s%s',
17 3
                    $class,
18 3
                    $data->getType(),
19 3
                    $this->getFunction($data),
20 3
                    $this->getLineAndFile($data)
21
                );
22
        }
23
        return
24 1
            sprintf(
25 1
                '%s%s',
26 1
                $this->getFunction($data),
27 1
                $this->getLineAndFile($data)
28
            );
29
    }
30
31 4
    private function getFunction(CallerData $caller)
32
    {
33 4
        if (self::STR_UNDEFINED === $function = $caller->getFunction()) {
34
            $function = ucfirst($function);
35
        } else {
36 4
            $function .= '()';
37
        }
38 4
        return $function;
39
    }
40
41
    /**
42
     * @param CallerData $caller
43
     * @return string
44
     */
45 4
    private function getLineAndFile(CallerData $caller): string
46
    {
47 4
        if (self::STR_UNDEFINED !== $caller->getFunction()) {
48
            return
49 4
                sprintf(
50 4
                    ' [%s:"%s"]',
51 4
                    $caller->getLine(),
52 4
                    $caller->getFile()
53
                );
54
        }
55
        return '';
56
    }
57
}
58