PluginDump::exportObj()   F
last analyzed

Complexity

Conditions 19
Paths 785

Size

Total Lines 82

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 82
rs 0.6485
c 0
b 0
f 0
cc 19
nc 785
nop 2

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Copyright (c) 2013-2017
4
 *
5
 * @category  Library
6
 * @package   Dwoo\Plugins\Functions
7
 * @author    Jordi Boggiano <[email protected]>
8
 * @author    David Sanchez <[email protected]>
9
 * @copyright 2008-2013 Jordi Boggiano
10
 * @copyright 2013-2017 David Sanchez
11
 * @license   http://dwoo.org/LICENSE Modified BSD License
12
 * @version   1.3.2
13
 * @date      2017-01-06
14
 * @link      http://dwoo.org/
15
 */
16
17
namespace Dwoo\Plugins\Functions;
18
19
use Dwoo\Plugin;
20
use Iterator;
21
use ReflectionObject;
22
23
/**
24
 * Dumps values of the given variable, or the entire data if nothing provided
25
 * <pre>
26
 *  * var : the variable to display
27
 *  * show_methods : if set to true, the public methods of any object encountered are also displayed
28
 * </pre>
29
 * This software is provided 'as-is', without any express or implied warranty.
30
 * In no event will the authors be held liable for any damages arising from the use of this software.
31
 */
32
class PluginDump extends Plugin
33
{
34
    protected $outputObjects;
35
    protected $outputMethods;
36
37
    /**
38
     * @param string $var
39
     * @param bool   $show_methods
40
     *
41
     * @return string
42
     */
43
    public function process($var = '$', $show_methods = false)
44
    {
45
        $this->outputMethods = $show_methods;
46
        if ($var === '$') {
47
            $var = $this->core->getData();
48
            $out = '<div style="background:#aaa; padding:5px; margin:5px; color:#000;">data';
49
        } else {
50
            $out = '<div style="background:#aaa; padding:5px; margin:5px; color:#000;">dump';
51
        }
52
53
        $this->outputObjects = array();
54
55
        if (!is_array($var)) {
56
            if (is_object($var)) {
57
                return $this->exportObj('', $var);
58
            } else {
59
                return $this->exportVar('', $var);
60
            }
61
        }
62
63
        $scope = $this->core->getScope();
64
65
        if ($var === $scope) {
66
            $out .= ' (current scope): <div style="background:#ccc;">';
67
        } else {
68
            $out .= ':<div style="padding-left:20px;">';
69
        }
70
71
        $out .= $this->export($var, $scope);
72
73
        return $out . '</div></div>';
74
    }
75
76
    /**
77
     * @param $var
78
     * @param $scope
79
     *
80
     * @return string
81
     */
82
    protected function export($var, $scope)
83
    {
84
        $out = '';
85
        foreach ($var as $i => $v) {
86
            if (is_array($v) || (is_object($v) && $v instanceof Iterator)) {
87
                $out .= $i . ' (' . (is_array($v) ? 'array' : 'object: ' . get_class($v)) . ')';
88
                if ($v === $scope) {
89
                    $out .= ' (current scope):<div style="background:#ccc;padding-left:20px;">' . $this->export($v, $scope) . '</div>';
90
                } else {
91
                    $out .= ':<div style="padding-left:20px;">' . $this->export($v, $scope) . '</div>';
92
                }
93
            } elseif (is_object($v)) {
94
                $out .= $this->exportObj($i . ' (object: ' . get_class($v) . '):', $v);
95
            } else {
96
                $out .= $this->exportVar($i . ' = ', $v);
97
            }
98
        }
99
100
        return $out;
101
    }
102
103
    /**
104
     * @param $i
105
     * @param $v
106
     *
107
     * @return string
108
     */
109
    protected function exportVar($i, $v)
110
    {
111
        if (is_string($v) || is_bool($v) || is_numeric($v)) {
112
            return $i . htmlentities(var_export($v, true)) . '<br />';
113
        } elseif (is_null($v)) {
114
            return $i . 'null<br />';
115
        } elseif (is_resource($v)) {
116
            return $i . 'resource(' . get_resource_type($v) . ')<br />';
117
        } else {
118
            return $i . htmlentities(var_export($v, true)) . '<br />';
119
        }
120
    }
121
122
    /**
123
     * @param $i
124
     * @param $obj
125
     *
126
     * @return string
127
     */
128
    protected function exportObj($i, $obj)
129
    {
130
        if (array_search($obj, $this->outputObjects, true) !== false) {
131
            return $i . ' [recursion, skipped]<br />';
132
        }
133
134
        $this->outputObjects[] = $obj;
135
136
        $list = (array)$obj;
137
138
        $protectedLength = strlen(get_class($obj)) + 2;
139
140
        $out = array();
141
142
        if ($this->outputMethods) {
143
            $ref = new ReflectionObject($obj);
144
145
            foreach ($ref->getMethods() as $method) {
146
                if (!$method->isPublic()) {
147
                    continue;
148
                }
149
150
                if (empty($out['method'])) {
151
                    $out['method'] = '';
152
                }
153
154
                $params = array();
155
                foreach ($method->getParameters() as $param) {
156
                    $params[] = ($param->isPassedByReference() ? '&' : '') . '$' . $param->getName() . ($param->isOptional() ? ' = ' . var_export($param->getDefaultValue(), true) : '');
157
                }
158
159
                $out['method'] .= '(method) ' . $method->getName() . '(' . implode(', ', $params) . ')<br />';
160
            }
161
        }
162
163
        foreach ($list as $attributeName => $attributeValue) {
164
            if (property_exists($obj, $attributeName)) {
165
                $key = 'public';
166
            } elseif (substr($attributeName, 0, 3) === "\0*\0") {
167
                $key           = 'protected';
168
                $attributeName = substr($attributeName, 3);
169
            } else {
170
                $key           = 'private';
171
                $attributeName = substr($attributeName, $protectedLength);
172
            }
173
174
            if (empty($out[$key])) {
175
                $out[$key] = '';
176
            }
177
178
            $out[$key] .= '(' . $key . ') ';
179
180
            if (is_array($attributeValue)) {
181
                $out[$key] .= $attributeName . ' (array):<br />
182
							<div style="padding-left:20px;">' . $this->export($attributeValue, false) . '</div>';
183
            } elseif (is_object($attributeValue)) {
184
                $out[$key] .= $this->exportObj($attributeName . ' (object: ' . get_class($attributeValue) . '):', $attributeValue);
185
            } else {
186
                $out[$key] .= $this->exportVar($attributeName . ' = ', $attributeValue);
187
            }
188
        }
189
190
        $return = $i . '<br /><div style="padding-left:20px;">';
191
192
        if (!empty($out['method'])) {
193
            $return .= $out['method'];
194
        }
195
196
        if (!empty($out['public'])) {
197
            $return .= $out['public'];
198
        }
199
200
        if (!empty($out['protected'])) {
201
            $return .= $out['protected'];
202
        }
203
204
        if (!empty($out['private'])) {
205
            $return .= $out['private'];
206
        }
207
208
        return $return . '</div>';
209
    }
210
}
211