Completed
Push — master ( 494091...32c874 )
by David
27s
created

lib/Dwoo/Plugins/Functions/PluginDump.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Copyright (c) 2013-2016
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-2016 David Sanchez
11
 * @license   http://dwoo.org/LICENSE Modified BSD License
12
 * @version   1.3.0
13
 * @date      2016-09-19
14
 * @link      http://dwoo.org/
15
 */
16
17
namespace Dwoo\Plugins\Functions;
18
19
use Dwoo\Plugin;
20
21
/**
22
 * Dumps values of the given variable, or the entire data if nothing provided
23
 * <pre>
24
 *  * var : the variable to display
25
 *  * show_methods : if set to true, the public methods of any object encountered are also displayed
26
 * </pre>
27
 * This software is provided 'as-is', without any express or implied warranty.
28
 * In no event will the authors be held liable for any damages arising from the use of this software.
29
 */
30
class PluginDump extends Plugin
31
{
32
    protected $outputObjects;
33
    protected $outputMethods;
34
35
    public function process($var = '$', $show_methods = false)
36
    {
37
        $this->outputMethods = $show_methods;
38
        if ($var === '$') {
39
            $var = $this->core->getData();
40
            $out = '<div style="background:#aaa; padding:5px; margin:5px; color:#000;">data';
41
        } else {
42
            $out = '<div style="background:#aaa; padding:5px; margin:5px; color:#000;">dump';
43
        }
44
45
        $this->outputObjects = array();
46
47
        if (!is_array($var)) {
48
            if (is_object($var)) {
49
                return $this->exportObj('', $var);
50
            } else {
51
                return $this->exportVar('', $var);
52
            }
53
        }
54
55
        $scope = $this->core->getScope();
56
57
        if ($var === $scope) {
58
            $out .= ' (current scope): <div style="background:#ccc;">';
59
        } else {
60
            $out .= ':<div style="padding-left:20px;">';
61
        }
62
63
        $out .= $this->export($var, $scope);
64
65
        return $out . '</div></div>';
66
    }
67
68
    protected function export($var, $scope)
69
    {
70
        $out = '';
71
        foreach ($var as $i => $v) {
72
            if (is_array($v) || (is_object($v) && $v instanceof Iterator)) {
0 ignored issues
show
The class Dwoo\Plugins\Functions\Iterator does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
73
                $out .= $i . ' (' . (is_array($v) ? 'array' : 'object: ' . get_class($v)) . ')';
74
                if ($v === $scope) {
75
                    $out .= ' (current scope):<div style="background:#ccc;padding-left:20px;">' . $this->export($v, $scope) . '</div>';
76
                } else {
77
                    $out .= ':<div style="padding-left:20px;">' . $this->export($v, $scope) . '</div>';
78
                }
79
            } elseif (is_object($v)) {
80
                $out .= $this->exportObj($i . ' (object: ' . get_class($v) . '):', $v);
81
            } else {
82
                $out .= $this->exportVar($i . ' = ', $v);
83
            }
84
        }
85
86
        return $out;
87
    }
88
89
    protected function exportVar($i, $v)
90
    {
91
        if (is_string($v) || is_bool($v) || is_numeric($v)) {
92
            return $i . htmlentities(var_export($v, true)) . '<br />';
93
        } elseif (is_null($v)) {
94
            return $i . 'null<br />';
95
        } elseif (is_resource($v)) {
96
            return $i . 'resource(' . get_resource_type($v) . ')<br />';
97
        } else {
98
            return $i . htmlentities(var_export($v, true)) . '<br />';
99
        }
100
    }
101
102
    protected function exportObj($i, $obj)
103
    {
104
        if (array_search($obj, $this->outputObjects, true) !== false) {
105
            return $i . ' [recursion, skipped]<br />';
106
        }
107
108
        $this->outputObjects[] = $obj;
109
110
        $list = (array)$obj;
111
112
        $protectedLength = strlen(get_class($obj)) + 2;
113
114
        $out = array();
115
116
        if ($this->outputMethods) {
117
            $ref = new ReflectionObject($obj);
118
119
            foreach ($ref->getMethods() as $method) {
120
                if (!$method->isPublic()) {
121
                    continue;
122
                }
123
124
                if (empty($out['method'])) {
125
                    $out['method'] = '';
126
                }
127
128
                $params = array();
129
                foreach ($method->getParameters() as $param) {
130
                    $params[] = ($param->isPassedByReference() ? '&' : '') . '$' . $param->getName() . ($param->isOptional() ? ' = ' . var_export($param->getDefaultValue(), true) : '');
131
                }
132
133
                $out['method'] .= '(method) ' . $method->getName() . '(' . implode(', ', $params) . ')<br />';
134
            }
135
        }
136
137
        foreach ($list as $attributeName => $attributeValue) {
138
            if (property_exists($obj, $attributeName)) {
139
                $key = 'public';
140
            } elseif (substr($attributeName, 0, 3) === "\0*\0") {
141
                $key           = 'protected';
142
                $attributeName = substr($attributeName, 3);
143
            } else {
144
                $key           = 'private';
145
                $attributeName = substr($attributeName, $protectedLength);
146
            }
147
148
            if (empty($out[$key])) {
149
                $out[$key] = '';
150
            }
151
152
            $out[$key] .= '(' . $key . ') ';
153
154
            if (is_array($attributeValue)) {
155
                $out[$key] .= $attributeName . ' (array):<br />
156
							<div style="padding-left:20px;">' . $this->export($attributeValue, false) . '</div>';
157
            } elseif (is_object($attributeValue)) {
158
                $out[$key] .= $this->exportObj($attributeName . ' (object: ' . get_class($attributeValue) . '):', $attributeValue);
159
            } else {
160
                $out[$key] .= $this->exportVar($attributeName . ' = ', $attributeValue);
161
            }
162
        }
163
164
        $return = $i . '<br /><div style="padding-left:20px;">';
165
166
        if (!empty($out['method'])) {
167
            $return .= $out['method'];
168
        }
169
170
        if (!empty($out['public'])) {
171
            $return .= $out['public'];
172
        }
173
174
        if (!empty($out['protected'])) {
175
            $return .= $out['protected'];
176
        }
177
178
        if (!empty($out['private'])) {
179
            $return .= $out['private'];
180
        }
181
182
        return $return . '</div>';
183
    }
184
}
185