Passed
Push — master ( f510e9...9822a2 )
by Paul
10:29
created

Dump   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 152
Duplicated Lines 0 %

Test Coverage

Coverage 53.95%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 25
eloc 71
dl 0
loc 152
ccs 41
cts 76
cp 0.5395
rs 10
c 1
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A inspectObject() 0 16 4
A inspectPrimitive() 0 10 4
A dump() 0 9 1
A inspect() 0 13 4
A inspectClosure() 0 7 2
A isIgnoredKey() 0 3 1
A formatKey() 0 12 3
A reset() 0 5 1
A inspectArray() 0 18 5
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules;
4
5
use Closure;
6
use ReflectionFunction;
7
8
/**
9
 * @see: https://github.com/jacobstr/dumpling
10
 */
11
class Dump
12
{
13
    public $depth;
14
    public $ignore;
15
16
    protected $level = 0;
17
    protected $result = [];
18
    protected $stack = [];
19
20
    /**
21
     * @param mixed $value
22
     * @param int $depth
23
     * @return string
24
     */
25 14
    public function dump($value, $depth = 3, array $ignore = [])
26
    {
27 14
        $this->depth = $depth;
28 14
        $this->ignore = $ignore;
29 14
        $this->reset();
30 14
        $this->inspect($value);
31 14
        $result = rtrim(implode('', $this->result), "\n");
32 14
        $this->reset();
33 14
        return $result;
34
    }
35
36
    /**
37
     * @param string $key
38
     * @return string
39
     */
40
    protected function formatKey($key)
41
    {
42
        $result = [];
43
        $result[] = str_repeat(' ', $this->level * 4).'[';
44
        if ("\0" == $key[0]) {
45
            $keyParts = explode("\0", $key);
46
            $result[] = $keyParts[2].(('*' == $keyParts[1]) ? ':protected' : ':private');
47
        } else {
48
            $result[] = $key;
49
        }
50
        $result[] = '] => ';
51
        return implode('', $result);
52
    }
53
54
    /**
55
     * @param mixed $subject
56
     * @return void
57
     */
58 14
    protected function inspect($subject)
59
    {
60 14
        ++$this->level;
61 14
        if ($subject instanceof Closure) {
62
            $this->inspectClosure($subject);
63 14
        } elseif (is_object($subject)) {
64
            $this->inspectObject($subject);
65 14
        } elseif (is_array($subject)) {
66 4
            $this->inspectArray($subject);
67
        } else {
68 14
            $this->inspectPrimitive($subject);
69
        }
70 14
        --$this->level;
71 14
    }
72
73
    /**
74
     * @return void
75
     */
76 4
    protected function inspectArray(array $subject)
77
    {
78 4
        if ($this->level > $this->depth) {
79
            $this->result[] = "Nested Array\n";
80
            return;
81
        }
82 4
        if (empty($subject)) {
83
            $this->result[] = "Array ()\n";
84
            return;
85
        }
86 4
        $this->result[] = "Array (\n";
87 4
        foreach ($subject as $key => $val) {
88 4
            if (false === $this->isIgnoredKey($key)) {
89 4
                $this->result[] = str_repeat(' ', $this->level * 4).'['.$key.'] => ';
90 4
                $this->inspect($val);
91
            }
92
        }
93 4
        $this->result[] = str_repeat(' ', ($this->level - 1) * 4).")\n";
94 4
    }
95
96
    /**
97
     * @return void
98
     */
99
    protected function inspectClosure(Closure $subject)
100
    {
101
        $reflection = new ReflectionFunction($subject);
102
        $params = array_map(function ($param) {
103
            return ($param->isPassedByReference() ? '&$' : '$').$param->name;
104
        }, $reflection->getParameters());
105
        $this->result[] = 'Closure ('.implode(', ', $params).') { ... }'."\n";
106
    }
107
108
    /**
109
     * @param object $subject
110
     * @return void
111
     */
112
    protected function inspectObject($subject)
113
    {
114
        $classname = get_class($subject);
115
        if ($this->level > $this->depth) {
116
            $this->result[] = 'Nested '.$classname." Object\n";
117
            return;
118
        }
119
        $this->result[] = $classname." Object (\n";
120
        $subject = (array) $subject;
121
        foreach ($subject as $key => $val) {
122
            if (false === $this->isIgnoredKey($key)) {
123
                $this->result[] = $this->formatKey($key);
124
                $this->inspect($val);
125
            }
126
        }
127
        $this->result[] = str_repeat(' ', ($this->level - 1) * 4).")\n";
128
    }
129
130
    /**
131
     * @param mixed $subject
132
     * @return void
133
     */
134 14
    protected function inspectPrimitive($subject)
135
    {
136 14
        if (true === $subject) {
137 2
            $subject = '(bool) true';
138 14
        } elseif (false === $subject) {
139
            $subject = '(bool) false';
140 14
        } elseif (null === $subject) {
141 2
            $subject = '(null)';
142
        }
143 14
        $this->result[] = $subject."\n";
144 14
    }
145
146
    /**
147
     * @param string $key
148
     * @return bool
149
     */
150 4
    protected function isIgnoredKey($key)
151
    {
152 4
        return in_array($key, $this->ignore);
153
    }
154
155
    /**
156
     * @return void
157
     */
158 14
    protected function reset()
159
    {
160 14
        $this->level = 0;
161 14
        $this->result = [];
162 14
        $this->stack = [];
163 14
    }
164
}
165