Ajde_Dump::getAll()   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
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
class Ajde_Dump extends Ajde_Object_Static
4
{
5
    public static $dump = [];
6
    public static $warn = [];
7
8
    public static function dump($var, $expand = true)
9
    {
10
        $i = 0;
11
        $line = null;
12
        foreach (debug_backtrace() as $item) {
13
            try {
14
                $source = sprintf('%s. dumped from <em>%s</em>%s<strong>%s</strong> (line %s)',
15
                    count(self::$dump) + 1,
16
                    isset($item['class']) ? (is_object($item['class']) ? get_class($item['class']) : $item['class']) : '&lt;unknown class&gt; (in <span style=\'font-size: 0.8em;\'>'.print_r($item['args'][0]).'</span>)',
17
                    // Assume of no classname is available, dumped from template.. (naive)
18
                    !empty($item['type']) ? $item['type'] : '::',
19
                    !empty($item['function']) ? $item['function'] : '&lt;unknown function&gt;',
20
                    $line);
21
                $line = issetor($item['line'], null);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $line is correct as issetor($item['line'], null) (which targets issetor()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
22
            } catch (Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
23
            }
24
25
            if ($i == 2) {
26
                break;
27
            }
28
            $i++;
29
        }
30
        self::$dump[$source] = [$var, $expand];
0 ignored issues
show
Bug introduced by
The variable $source 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...
31
    }
32
33
    public static function warn($message)
34
    {
35
        self::$warn[] = $message;
36
    }
37
38
    public static function getAll()
39
    {
40
        return self::$dump;
41
    }
42
43
    public static function getWarnings()
44
    {
45
        return self::$warn;
46
    }
47
}
48