Debug::getDefaultDumper()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace nyx\diagnostics;
2
3
// Internal dependencies
4
use nyx\diagnostics\debug\handlers;
5
use nyx\diagnostics\debug\interfaces;
6
7
/**
8
 * Debug
9
 *
10
 * Registers the Error and Exception Handlers contained within this component and gives static access to them.
11
 * Please note that the Handler can only be enabled once using self::enable() during script execution. If they
12
 * get unregistered from PHP you'll have to manually register the exact same instances again in order to avoid
13
 * potentially weird behaviour due to this class being completely static.
14
 *
15
 * Important note: Neither this class nor any of the Handlers will fiddle with your php.ini settings with regards
16
 * to error reporting, displaying errors etc. Please consult the Error Handler's docs {@see handlers\Error} for
17
 * more information on what the threshold means and how not setting any ini directives here affects its behaviour.
18
 *
19
 * @package     Nyx\Diagnostics\Debug
20
 * @version     0.0.3
21
 * @author      Michal Chojnacki <[email protected]>
22
 * @copyright   2012-2016 Nyx Dev Team
23
 * @link        http://docs.muyo.io/nyx/diagnostics/index.html
24
 * @todo        Needs to seamlessly hook into already set error/exception handlers and handle less strict types
25
 *              than our handler interfaces just as well.
26
 * @todo        Move to Utils instead once properly abstracted?
27
 */
28
class Debug
29
{
30
    /**
31
     * @var bool                            Whether the Handlers already got registered (using this class) or not.
32
     */
33
    private static $enabled;
34
35
    /**
36
     * @var interfaces\handlers\Error       An Error Handler instance once registered.
37
     */
38
    private static $errorHandler;
39
40
    /**
41
     * @var interfaces\handlers\Exception   An Exception Handler instance once registered.
42
     */
43
    private static $exceptionHandler;
44
45
    /**
46
     * @var interfaces\Dumper|callable      The Dumper in use.
47
     */
48
    private static $dumper;
49
50
    /**
51
     * Enables the bundled Error and Exception Handlers by registering them with PHP.
52
     *
53
     * Important note: The return values. When this method returns false, it merely means that the Handlers are
54
     * already registered and therefore could not be enabled again. This method will only return true for the call
55
     * that actually enables them. This is a little hack to make checking for certain conditions easier.
56
     *
57
     * @param   interfaces\handlers\Error       $error      An optional already instantiated Error Handler instance.
58
     *                                                      If none is given, a new one will be instantiated.
59
     * @param   interfaces\handlers\Exception   $exception  An optional already instantiated Exception Handler instance.
60
     *                                                      If none is given, a new one will be instantiated.
61
     * @param   int                             $threshold  {@see handlers\Error::setThreshold()}
62
     * @return  bool                                        True when Debug was not yet enabled, false otherwise.
63
     */
64
    public static function enable(interfaces\handlers\Error $error = null, interfaces\handlers\Exception $exception = null, $threshold = null) : bool
65
    {
66
        // Only enable the Handlers once. See the class description for more on this.
67
        if (static::$enabled) {
0 ignored issues
show
Bug introduced by
Since $enabled is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $enabled to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
68
            return false;
69
        }
70
71
        // Register the Handlers.
72
        static::$errorHandler     = handlers\Error::register($error, $threshold);
0 ignored issues
show
Bug introduced by
Since $errorHandler is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $errorHandler to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
73
        static::$exceptionHandler = handlers\Exception::register($exception);
0 ignored issues
show
Bug introduced by
Since $exceptionHandler is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $exceptionHandler to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
74
75
        return static::$enabled = true;
0 ignored issues
show
Bug introduced by
Since $enabled is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $enabled to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
76
    }
77
78
    /**
79
     * Dumps the given variable(s), providing information about their type, contents and others.
80
     * Accepts multiple values as parameters.
81
     *
82
     * @param   mixed   ...$vars    The variable(s) to dump info about.
83
     */
84
    public static function dump(...$vars)
85
    {
86
        // If we've got no Dumper specified we will fall back to a sane default.
87
        if (!isset(static::$dumper)) {
0 ignored issues
show
Bug introduced by
Since $dumper is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $dumper to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
88
            static::setDumper(static::getDefaultDumper());
89
        }
90
91
        call_user_func(static::$dumper, ...$vars);
0 ignored issues
show
Bug introduced by
Since $dumper is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $dumper to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
92
    }
93
94
    /**
95
     * Checks whether Debug is enabled.
96
     *
97
     * @return  bool    True when Debug is enabled, false otherwise.
98
     */
99
    public static function isEnabled() : bool
100
    {
101
        return true === static::$enabled;
0 ignored issues
show
Bug introduced by
Since $enabled is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $enabled to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
102
    }
103
104
    /**
105
     * Returns the Error Handler in use.
106
     *
107
     * @return  interfaces\handlers\Error       The Error Handler in use, otherwise null if it has not been
108
     *                                          registered using self::enable().
109
     */
110
    public static function getErrorHandler() : interfaces\handlers\Error
111
    {
112
        return static::$errorHandler;
0 ignored issues
show
Bug introduced by
Since $errorHandler is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $errorHandler to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
113
    }
114
115
    /**
116
     * Returns the Exception Handler in use.
117
     *
118
     * @return  interfaces\handlers\Exception   The Exception Handler in use, otherwise null if it has not been
119
     *                                          registered using self::enable().
120
     */
121
    public static function getExceptionHandler() : interfaces\handlers\Exception
122
    {
123
        return static::$exceptionHandler;
0 ignored issues
show
Bug introduced by
Since $exceptionHandler is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $exceptionHandler to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
124
    }
125
126
    /**
127
     * Returns the Dumper in use.
128
     *
129
     * @return  interfaces\Dumper|callable
130
     */
131
    public static function getDumper() : interfaces\Dumper
132
    {
133
        return static::$dumper;
0 ignored issues
show
Bug introduced by
Since $dumper is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $dumper to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
134
    }
135
136
    /**
137
     * Sets the Dumper to be used.
138
     *
139
     * @param   interfaces\Dumper|callable   $dumper
140
     */
141
    public static function setDumper($dumper)
142
    {
143
        if (!$dumper instanceof interfaces\Dumper && !is_callable($dumper)) {
144
            throw new \InvalidArgumentException('Expected an instance of ['.interfaces\Dumper::class.'] or a callable, got ['.static::getTypeName($dumper).'] instead.');
145
        }
146
147
        static::$dumper = $dumper;
0 ignored issues
show
Bug introduced by
Since $dumper is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $dumper to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
148
    }
149
150
    /**
151
     * Returns the type of the given value - the class name for objects or the
152
     * type for all other types.
153
     *
154
     * @param   mixed   $value
155
     * @return  string
156
     */
157
    public static function getTypeName($value) : string
158
    {
159
        return is_object($value) ? get_class($value) : gettype($value);
160
    }
161
162
    /**
163
     * Converts an Exception to an array in the format as returned by \Exception::getTrace().
164
     *
165
     * @param   \Throwable  $throwable  The Exception to convert.
166
     * @return  array
167
     */
168
    public static function throwableToArray(\Throwable $throwable) : array
169
    {
170
        return [
171
            'type'  => $throwable->getCode(),
172
            'file'  => $throwable->getFile(),
173
            'line'  => $throwable->getLine(),
174
            'class' => get_class($throwable),
175
            'args'  => [$throwable->getMessage()]
176
        ];
177
    }
178
179
    /**
180
     * Returns a default variable dumper to be used by self::dump() if no other has been set.
181
     *
182
     * @return  interfaces\Dumper|callable  $dumper
183
     */
184
    protected static function getDefaultDumper()
185
    {
186
        return 'var_dump';
187
    }
188
}
189