Passed
Push — master ( d12c1b...36b4d9 )
by Alec
02:02
created

CallerData   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 116
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0
wmc 12

11 Methods

Rating   Name   Duplication   Size   Complexity  
A getClass() 0 3 1
A getFile() 0 3 1
A __construct() 0 6 1
A getType() 0 3 1
A getArgs() 0 3 1
A buildOn() 0 7 2
A __toString() 0 3 1
A getObject() 0 3 1
A getLine() 0 3 1
A parse() 0 8 1
A getFunction() 0 3 1
1
<?php declare(strict_types=1);
2
3
namespace AlecRabbit\Accessories\Caller;
4
5
use AlecRabbit\Accessories\Caller;
6
use AlecRabbit\Accessories\Caller\Contracts\CallerDataInterface;
7
use AlecRabbit\Reports\Contracts\ReportableInterface;
8
use AlecRabbit\Reports\Contracts\ReportInterface;
9
use AlecRabbit\Reports\Core\AbstractReport;
10
11
class CallerData extends AbstractReport implements CallerDataInterface
12
{
13
    /** @var string */
14
    protected $function;
15
16
    /** @var int|null */
17
    protected $line;
18
19
    /** @var string|null */
20
    protected $file;
21
22
    /** @var string|null */
23
    protected $class;
24
25
    /** @var object|null */
26
    protected $object;
27
28
    /** @var string|null */
29
    protected $type;
30
31
    /** @var array|null */
32
    protected $args;
33
34 8
    public function __construct(
35
        array $caller = null
36
    ) {
37 8
        $caller = $caller ?? self::UNDEFINED;
38
        $this->function = $caller[self::FUNCTION];
39 8
        $this->parse($caller);
40 8
    }
41
42 8
    protected function parse(array $caller): void
43
    {
44 8
        $this->line = $caller[self::LINE] ?? null;
45 8
        $this->file = $caller[self::FILE] ?? null;
46 8
        $this->class = $caller[self::CLS] ?? null;
47 8
        $this->object = $caller[self::OBJECT] ?? null;
48 8
        $this->type = $caller[self::TYPE] ?? null;
49 8
        $this->args = $caller[self::ARGS] ?? null;
50 8
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55 4
    public function __toString(): string
56
    {
57 4
        return Caller::getFormatter()->format($this);
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 6
    public function getFunction(): string
64
    {
65 6
        return $this->function;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71 4
    public function getLine(): ?int
72
    {
73 4
        return $this->line;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 4
    public function getFile(): ?string
80
    {
81 4
        return $this->file;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 6
    public function getClass(): ?string
88
    {
89 6
        return $this->class;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 3
    public function getObject(): ?object
96
    {
97 3
        return $this->object;
98
    }
99
100
    /**
101
     * {@inheritdoc}
102
     */
103 4
    public function getType(): ?string
104
    {
105 4
        return $this->type;
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 3
    public function getArgs(): ?array
112
    {
113 3
        return $this->args;
114
    }
115
116
    /**
117
     * @param ReportableInterface $reportable
118
     * @return ReportInterface
119
     */
120 2
    public function buildOn(ReportableInterface $reportable): ReportInterface
121
    {
122 2
        if ($reportable instanceof Caller) {
123 1
            return $this;
124
        }
125 1
        throw new \InvalidArgumentException(
126 1
            Caller::class . ' expected, ' . get_class($reportable) . ' given.'
127
        );
128
    }
129
}
130