Completed
Push — master ( 563916...ca1336 )
by Adrien
05:31
created

Debug.php ➔ w()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 6
eloc 8
c 2
b 1
f 0
nc 1
nop 0
dl 0
loc 10
ccs 0
cts 7
cp 0
crap 42
rs 8.8571
1
<?php
2
3
class Debug
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
    protected static function varDump($var)
6
    {
7
        if (php_sapi_name() == 'cli') {
8
            var_dump($var);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($var); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
9
        } else {
10
            ob_start();
11
            var_dump($var);
12
            $dump = ob_get_contents();
13
            ob_end_clean();
14
            echo htmlspecialchars($dump, ENT_QUOTES);
15
        }
16
    }
17
18
    protected static function parseObject($obj)
19
    {
20
        $obj_dump = print_r($obj, 1);
21
        $ret_list = [];
22
        $ret_map = [];
23
        $ret_name = '';
24
        $dump_lines = preg_split('/[\r\n]+/', $obj_dump);
25
        $ARR_NAME = 'arr_name';
26
        $ARR_LIST = 'arr_list';
27
        $arr_index = -1;
28
29
        // get the object type...
30
        $matches = [];
31
        preg_match('/^\s*(\S+)\s+\bObject\b/i', $obj_dump, $matches);
32
        if (isset($matches[1])) {
33
            $ret_name = $matches[1];
34
        }//if
35
36
        foreach ($dump_lines as &$line) {
37
            $matches = [];
38
39
            //load up var and values...
40
            if (preg_match('/^\s*\[\s*(\S+)\s*\]\s+=>\s+(.*)$/', $line, $matches)) {
41
                if (mb_stripos($matches[2], 'array') !== false) {
42
                    $arr_map = [];
43
                    $arr_map[$ARR_NAME] = $matches[1];
44
                    $arr_map[$ARR_LIST] = [];
45
                    $arr_list[++$arr_index] = $arr_map;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$arr_list was never initialized. Although not strictly required by PHP, it is generally a good practice to add $arr_list = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
46
                } else {
47
48
                    // save normal variables and arrays differently...
49
                    if ($arr_index >= 0) {
50
                        $arr_list[$arr_index][$ARR_LIST][$matches[1]] = $matches[2];
0 ignored issues
show
Bug introduced by
The variable $arr_list does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
51
                    } else {
52
                        $ret_list[$matches[1]] = $matches[2];
53
                    }//if/else
54
                }//if/else
55
            } else {
56
57
                // save the current array to the return list...
58
                if (mb_stripos($line, ')') !== false) {
59
                    if ($arr_index >= 0) {
60
                        $arr_map = array_pop($arr_list);
61
62
                        // if there is more than one array then this array belongs to the earlier array...
63
                        if ($arr_index > 0) {
64
                            $arr_list[($arr_index - 1)][$ARR_LIST][$arr_map[$ARR_NAME]] = $arr_map[$ARR_LIST];
65
                        } else {
66
                            $ret_list[$arr_map[$ARR_NAME]] = $arr_map[$ARR_LIST];
67
                        }//if/else
68
69
                        --$arr_index;
70
                    }//if
71
                }//if
72
            }//if/else
73
        }//foreach
74
75
        $ret_map['class'] = $ret_name;
76
        $ret_map['members'] = $ret_list;
77
78
        return $ret_map;
79
    }
80
81
//method
82
83
    /**
84
     * Dump any kind of variable in a table (array, object, etc..)
85
     *
86
     * @param mixed $data
87
     */
88
    public static function dump($data, $firstLevel = true)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
89
    {
90
        if (ini_get('xdebug.overload_var_dump')) {
91
            return var_dump($data);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($data); looks like debug code. Are you sure you do not want to remove it? This might expose sensitive data.
Loading history...
92
        }
93
94
        if ($firstLevel && count($data) == 1) {
95
            $data = $data[0];
96
        }
97
98
        if (php_sapi_name() == 'cli') {
99
            return self::varDump($data);
100
        }
101
102
        if (is_object($data)) {
103
            return self::dump(self::parseObject($data), false);
104
        } elseif (is_array($data)) {
105
            echo '<table style="padding: 0px; border: solid 2px red; border-collapse:collapse;">';
106
            foreach ($data as $key1 => $elem1) {
107
                echo '<tr><td style="padding: 0px; border: solid 1px grey;"><pre>';
108
                self::varDump($key1);
109
                echo '</pre></td><td style="padding: 0px; border: solid 1px grey;">';
110
                self::dump($elem1, false);
111
                echo '</td></tr>';
112
            }
113
            echo '</table>';
114
        } elseif (null === $data) {
115
            echo 'NULL VALUE';
116
        } else {
117
            echo '<pre>';
118
            self::varDump($data);
119
            echo '</pre>';
120
121
            return;
122
        }
123
    }
124
}
125
126
function v()
0 ignored issues
show
Coding Style introduced by
This method's name is shorter than the configured minimum length of 4 characters.

Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.

Loading history...
127
{
128
    Debug::dump(func_get_args());
129
}
130
131
function w()
0 ignored issues
show
Coding Style introduced by
This method's name is shorter than the configured minimum length of 4 characters.

Even though PHP does not care about the name of your methods, it is generally a good practice to choose method names which can be easily understood by other human readers.

Loading history...
132
{
133
    $isHtml = (php_sapi_name() != 'cli');
134
    echo "\n_________________________________________________________________________________________________________________________" . ($isHtml ? '</br>' : '') . "\n";
135
    Debug::dump(func_get_args());
136
    echo "\n" . ($isHtml ? '</br>' : '') . '_________________________________________________________________________________________________________________________' . ($isHtml ? '<pre>' : '') . "\n";
137
    debug_print_backtrace();
138
    echo '' . ($isHtml ? '</pre>' : '') . '_________________________________________________________________________________________________________________________' . ($isHtml ? '</br>' : '') . "\n";
139
    die("script aborted on purpose.\n");
0 ignored issues
show
Coding Style Compatibility introduced by
The function w() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
140
}
141