Debug::append()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 11
cp 0
rs 9.9
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 6
1
<?php
2
$defflip = (!cfip()) ? exit(header('HTTP/1.1 401 Unauthorized')) : 1;
3
4
/**
5
 * This file defines the debug class used in this site to enable
6
 * verbose deugging outout.
7
 * @package debug
8
 * @author Sebastian 'Seraph' Grewe
9
 * @copyright Sebastian Grewe
10
 * @version 1.0
11
 * */
12
class Debug {
13
14
    /**
15
     * @var integer $DEBUG enable (1) or disable (0) debugging
16
     */
17
    private $DEBUG;
18
19
    /**
20
     * @var array $debugInfo Data array with debugging information
21
     */
22
    private $arrDebugInfo;
23
24
    /**
25
     * @var float $startTime Start time of our debugger
26
     */
27
    private $floatStartTime;
28
29
    /**
30
     * Construct our class
31
     * @param integer $DEBUG [optional] Enable (>=1) or disable (0) debugging
32
     * @return none
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
33
     */
34
    function __construct($log, $DEBUG=0) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
35
      $this->log = $log;
0 ignored issues
show
Bug introduced by
The property log does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
36
        $this->DEBUG = $DEBUG;
37
        if ($DEBUG >= 1) {
38
            $this->floatStartTime = microtime(true);
39
            $this->append("Debugging enabled", 1);
40
        }
41
    }
42
43
    /**
44
     * If we want to set our debugging level in a child class this allows us to do so
45
     * @param integer $DEBUG our debugging level
46
     */
47
    function setDebug($DEBUG) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
48
        $this->DEBUG = $DEBUG;
49
    }
50
    
51
    /**
52
     * Return a backtrace strin
53
     * @return string Full backtrace <file>:<line> but no refereces to debug class 
54
     */
55
    public function getBacktrace() {
56
        $bth = debug_backtrace();
57
        while (strpos($bth[0]['file'], "classes/debug") != false) array_shift($bth);
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing strpos($bth[0]['file'], 'classes/debug') of type integer to the boolean false. If you are specifically checking for non-zero, consider using something more explicit like > 0 or !== 0 instead.
Loading history...
58
        foreach ($bth as $x) {
59
            $backtrace[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$backtrace was never initialized. Although not strictly required by PHP, it is generally a good practice to add $backtrace = 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...
60
                    'file' => substr($x['file'], strrpos($x['file'], '/') + 1),
61
                    'line' => $x['line'],
62
                    'function' => $x['function'],
63
                );
64
        }
65
        return $backtrace;
0 ignored issues
show
Bug introduced by
The variable $backtrace 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...
66
    }
67
68
    /**
69
     * We fill our data array here
70
     * @param string $msg Debug Message
71
     * @param string $file [optional] File name
0 ignored issues
show
Bug introduced by
There is no parameter named $file. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
72
     * @param integer $line [optional] Line inside the $file
0 ignored issues
show
Bug introduced by
There is no parameter named $line. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
73
     * @param integer $debug [optional] Debugging level, default 1
74
     * @param string $class [optional] Class this is called from
0 ignored issues
show
Bug introduced by
There is no parameter named $class. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
75
     * @param string $method [optional] Method this is called from
0 ignored issues
show
Bug introduced by
There is no parameter named $method. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
76
     * @return none
77
     */
78
    function append($msg, $debug=1) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
79
        if ($this->DEBUG >= $debug) {
80
            $this->arrDebugInfo[] = array(
81
                'level' => $debug,
82
                'time' => round((microtime(true) - $this->floatStartTime) * 1000, 2),
83
                'backtrace' => $this->getBacktrace(),
84
                'message' => $msg,
85
            );
86
            $this->log->log("debug", $msg);
87
        }
88
    }
89
90
    /**
91
     * Return the created strDebugInfo array
92
     * @return none
93
     */
94
    public function getDebugInfo() {
95
        return $this->arrDebugInfo;
96
    }
97
98
    /**
99
     * Directly print the debugging information, table formatted
100
     * @return none
101
     */
102
    function printDebugInfo() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
103
        echo "<table><tr><td>Timestamp</td><td>File</td><td>Line</td><td>Class</td><td>Method</td><td>Message</td></tr>";
104
        foreach ($this->getDebugInfo() as $content) {
105
            echo "<tr><td>" . $content['time'] . "</td><td>" . $content['backtrace'] . "</td><td>" . $content['message'] . "</td></tr>";
106
        }
107
        echo "</table>";
108
    }
109
110
}
111
112
// Instantiate this class
113
$debug = new Debug($log, $config['DEBUG']);
114