Completed
Pull Request — master (#545)
by Richard
09:25
created

Debug::doOnce()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
namespace Xmf;
13
14
/**
15
 * Debugging tools for developers
16
 *
17
 * @category  Xmf\Debug
18
 * @package   Xmf
19
 * @author    trabis <[email protected]>
20
 * @author    Richard Griffith <[email protected]>
21
 * @copyright 2011-2016 XOOPS Project (http://xoops.org)
22
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
23
 * @link      http://xoops.org
24
 */
25
class Debug extends \Kint
26
{
27
    /**
28
     * doOnce - do some local housekeeping on first use. Any method needing this
29
     * assist just calls every time, the one time logic is all here.
30
     *
31
     * @return void
32
     */
33
    private static function doOnce()
34
    {
35
        static $done;
36
        if (true !== $done) {
37
            $done = true;
38
            $class = get_called_class();
39
            parent::$aliases[] = [$class, 'dump'];
40
            parent::$aliases[] = [$class, 'backtrace'];
41
        }
42
    }
43
44
    /**
45
     * Dump one or more variables
46
     *
47
     * @param mixed $data variable(s) to dump
48
     *
49
     * @return void
50
     */
51
    public static function dump($data = null)
52
    {
53
        $args = func_get_args();
54
55
        static::doOnce();
0 ignored issues
show
Bug introduced by
Since doOnce() is declared private, calling it with static will lead to errors in possible sub-classes. You can either use self, or increase the visibility of doOnce() to at least protected.

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

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
}

public static function getSomeVariable()
{
    return static::getTemperature();
}

}

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 {
      private static function getTemperature() {
        return "-182 °C";
    }
}

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

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

class YourClass
{
    private static function getTemperature() {
        return "3422 °C";
    }

    public static function getSomeVariable()
    {
        return self::getTemperature();
    }
}
Loading history...
56
        \Kint_Renderer_Rich::$theme = 'aante-light.css'; // options: 'original' (default), 'solarized', 'solarized-dark' and 'aante-light'
57
        forward_static_call_array(array('parent', 'dump'), $args);
58
    }
59
60
    /**
61
     * Display debug backtrace
62
     *
63
     * @return void
64
     */
65
    public static function backtrace()
66
    {
67
        static::dump(debug_backtrace());
68
    }
69
70
    /**
71
     * start_trace - turn on xdebug trace
72
     *
73
     * Requires xdebug extension
74
     *
75
     * @param string $tracefile      file name for trace file
76
     * @param string $collect_params argument for ini_set('xdebug.collect_params',?)
77
     *                             Controls display of parameters in trace output
78
     * @param string $collect_return argument for ini_set('xdebug.collect_return',?)
79
     *                             Controls display of function return value in trace
80
     *
81
     * @return void
82
     */
83
    public static function startTrace($tracefile = '', $collect_params = '3', $collect_return = 'On')
84
    {
85
        if (function_exists('xdebug_start_trace')) {
86
            ini_set('xdebug.collect_params', $collect_params);
87
            ini_set('xdebug.collect_return', $collect_return);
88
            if ($tracefile == '') {
89
                $tracefile = XOOPS_VAR_PATH . '/logs/php_trace';
90
            }
91
            xdebug_start_trace($tracefile);
92
        }
93
    }
94
95
    /**
96
     * stop_trace - turn off xdebug trace
97
     *
98
     * Requires xdebug extension
99
     *
100
     * @return void
101
     */
102
    public static function stopTrace()
103
    {
104
        if (function_exists('xdebug_stop_trace')) {
105
            xdebug_stop_trace();
106
        }
107
    }
108
}
109