Completed
Push — master ( 38de85...55925d )
by Andre
01:46
created

TraceSynopsis::generateValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace TheIconic\Synopsis\Exception;
4
5
use TheIconic\Synopsis\AbstractSynopsis;
6
7
/**
8
 * represents an exception trace line
9
 *
10
 * @package TheIconic\Synopsis\Exception
11
 */
12
class TraceSynopsis extends AbstractSynopsis
13
{
14
    /**
15
     * @var string the file
16
     */
17
    protected $file;
18
19
    /**
20
     * @var int the line
21
     */
22
    protected $line;
23
24
    /**
25
     * @var string the class
26
     */
27
    protected $class;
28
29
    /**
30
     * @var string the function
31
     */
32
    protected $function;
33
34
    /**
35
     * @see parent::process()
36
     * @param $value
37
     * @param $depth
38
     */
39
    public function process($value, $depth)
40
    {
41
        parent::process($value, $depth);
42
43
        $this->type = sprintf('%s()', $this->generateType($value));
44
45
        $this->value = $this->generateValue($value);
46
47
        $this->file = $value['file'] ?? '';
48
        $this->line = $value['line'] ?? '';
49
        $this->class = $value['class'] ?? '';
50
        $this->function = $value['function'] ?? '';
51
52
        if (!empty($value['args'])) {
53
            $this->length = count($value['args']);
54
55
            if ($depth) {
56
                foreach ($value['args'] as $k => $v) {
57
                    $this->addChild($this->getFactory()->synopsize($v, $depth), $k);
58
                }
59
            }
60
        }
61
    }
62
63
    /**
64
     * @param array $value
65
     * @return string
66
     */
67
    protected function generateValue(array $value): string
68
    {
69
        $file = $value['file'] ?? 'unkown file';
70
71
        if (!array_key_exists('line', $value)) {
72
            return $file;
73
        }
74
75
        return sprintf('%s (%d)', $file, $value['line']);
76
    }
77
78
    /**
79
     * @param array $value
80
     * @return string
81
     */
82
    protected function generateType(array $value): string
83
    {
84
        if (empty($value['class'])) {
85
            return $value['function'];
86
        }
87
88
        return $value['class'] . $value['type'] . $value['function'];
89
    }
90
91
    /**
92
     * @return array
93
     */
94
    public function getDetails()
95
    {
96
        return [
97
            'line' => $this->line,
98
            'file' => $this->file,
99
            'class' => $this->class,
100
            'function' => $this->function,
101
        ];
102
    }
103
104
    /**
105
     * get the file
106
     *
107
     * @return string
108
     */
109
    public function getFile()
110
    {
111
        return $this->file;
112
    }
113
114
    /**
115
     * get the line
116
     *
117
     * @return int
118
     */
119
    public function getLine()
120
    {
121
        return $this->line;
122
    }
123
124
    /**
125
     * get the class
126
     *
127
     * @return string
128
     */
129
    public function getClass()
130
    {
131
        return $this->class;
132
    }
133
134
    /**
135
     * get the function
136
     *
137
     * @return string
138
     */
139
    public function getFunction()
140
    {
141
        return $this->function;
142
    }
143
}
144