Issues (3098)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

framework/Koch/Debug/Debug.php (44 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * Koch Framework
5
 * Jens-André Koch © 2005 - onwards.
6
 *
7
 * This file is part of "Koch Framework".
8
 *
9
 * License: GNU/GPL v2 or any later version, see LICENSE file.
10
 *
11
 * This program is free software; you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation; either version 2 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace Koch\Debug;
26
27
/**
28
 * Debug.
29
 *
30
 * This class initializes debugging helpers like xdebug, the doctrine profiler,
31
 * firebug and printR at system start-up and displays debug
32
 * and runtime-informations on demand or at application shutdown.
33
 */
34
class Debug
0 ignored issues
show
Debug does not seem to conform to the naming convention (Utils?$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
35
{
36
    /**
37
     * This is an enhanced version of the native php function print_r().
38
     *
39
     * @param mixed/array/object $var Array or Object as Variable to display
0 ignored issues
show
The doc-type mixed/array/object could not be parsed: Unknown type name "mixed/array/object" at position 0. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
40
     * @returns Returns a better structured display of an array/object as native print_r().
41
     */
42
    public static function printR($var)
43
    {
44
        // this will handle more than one parameter
45
        if (func_num_args() > 1) {
46
            $vars = func_get_args();
47
        }
48
49
        $backtrace_array = [];
0 ignored issues
show
$backtrace_array does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
$backtrace_array is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
50
        $backtrace_array = debug_backtrace();
0 ignored issues
show
$backtrace_array does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
51
        $trace           = array_shift($backtrace_array);
0 ignored issues
show
$backtrace_array does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
52
        $file            = file($trace['file']);
53
        $trace_line      = $file[$trace['line'] - 1];
0 ignored issues
show
$trace_line does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
54
55
        echo '<pre>';
56
        echo '<b>Debugging ';
57
        echo '<font color=red>' . basename($trace['file']) . '</font>';
58
        echo ' on line <font color=red>' . $trace['line'] . '</font></b>:' . "\n";
59
        echo "<div style='background: #f5f5f5; padding: 0.2em 0em;'>" . htmlspecialchars($trace_line) . '</div>' . "\n";
0 ignored issues
show
$trace_line does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
60
61
        echo '<b>Type</b>: ' . gettype($var) . "\n"; // uhhh.. gettype is slow like hell
62
63
        // handle more than one parameter
64
        foreach ($vars as $var) {
0 ignored issues
show
The variable $vars 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...
65
            if (is_string($var)) {
66
                echo '<b>Length</b>: ' . strlen($var) . "\n";
67
            }
68
69
            if (is_array($var)) {
70
                echo '<b>Length</b>: ' . count($var) . "\n";
71
            }
72
73
            echo '<b>Value</b>: ';
74
75
            if ($var === true) {
76
                echo '<font color=green><b>true</b></font>';
77
            } elseif ($var === false) {
78
                echo '<font color=red><b>false</b></font>';
79
            } elseif ($var === null) {
80
                echo '<font color=red><b>null</b></font>';
81
            } elseif ($var === 0) {
82
                echo '0';
83
            } elseif (is_string($var) and strlen($var) === '0') {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
Unused Code Bug introduced by
The strict comparison === seems to always evaluate to false as the types of strlen($var) (integer) and '0' (string) can never be identical. Maybe you want to use a loose comparison == instead?
Loading history...
84
                echo '<font color=green>*EMPTY STRING*</font>';
85
            } elseif (is_string($var)) {
86
                echo htmlspecialchars($var);
87
            } else {
88
                $print_r = print_r($var, true);
0 ignored issues
show
$print_r does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
89
                // str_contains < or >
90
                if ((strstr($print_r, '<') !== false) || (strstr($print_r, '>') !== false)) {
0 ignored issues
show
$print_r does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
91
                    $print_r = htmlspecialchars($print_r);
0 ignored issues
show
$print_r does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
92
                }
93
                echo $print_r;
0 ignored issues
show
$print_r does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
94
            }
95
        }
96
97
        echo '</pre>';
98
99
        // save session before exit
100
        if ((bool) session_id()) {
101
            session_write_close();
102
        }
103
104
        // do not exit, if we are inside a test run
105
        if (defined('UNIT_TEST_RUN') === false or UNIT_TEST_RUN === false) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
106
            \Koch\Tools\ApplicationQuit::quit();
107
        }
108
    }
109
110
    /**
111
     * Displays the content of a variable with var_dump.
112
     * The content gets escaping and pre tags are applied for better readability.
113
     *
114
     * @param mixed $var  The variable to debug.
115
     * @param bool  $exit Stop execution after dump? Default is true (stops).
116
     */
117
    public static function dump($var, $exit = true)
118
    {
119
        // var_dump the content into a buffer and store it to variable
120
        ob_start();
121
        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...
122
        $var_dump = ob_get_clean();
0 ignored issues
show
$var_dump does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
123
124
        /*
125
         * if xdebug is on and overloaded the var_dump function,
126
         * then the output is already properly escaped and prepared for direct output.
127
         * if xdebug is off, we need to apply escaping ourself.
128
         * html pre tags are applied to structure the display a bit more.
129
         */
130
        if (false === extension_loaded('xdebug')) {
131
            $var_dump = preg_replace('/\]\=\>\n(\s+)/m', '] => ', $var_dump);
0 ignored issues
show
$var_dump does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
132
            $var_dump = '<pre>' . htmlspecialchars($var_dump, ENT_QUOTES, 'UTF-8') . '</pre>';
0 ignored issues
show
$var_dump does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
133
        }
134
135
        // display where this debug statement
136
        echo self::getOriginOfDebugCall();
137
138
        // output the content of the buffer
139
        echo $var_dump;
0 ignored issues
show
$var_dump does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
140
141
        // do not exit, if we are inside a test run
142
        if (defined('UNIT_TEST_RUN') === false or UNIT_TEST_RUN === false) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
143
            if ($exit === true) {
144
                \Koch\Tools\ApplicationQuit::quit();
145
            }
146
        }
147
    }
148
149
    /**
150
     * Debug logs the output of $var to the firebug console in your browser.
151
     *
152
     * @param mixed $var The variable to debug.
153
     * @param $logmethod The firebug method to call for logging (log,info,warn, error). Defaults to "log".
154
     *
155
     * @return FirePHP object.
0 ignored issues
show
Should the return type not be FirePHP|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
156
     */
157
    public static function firebug($var, $logmethod = 'log')
158
    {
159
        // @codeCoverageIgnoreStart
160
        // We don't need to test vendor library functionality.
161
        // @see FirePHPCore_FirePHPTest
162
163
        $firephp = \FirePHP::getInstance(true);
164
165
        /*
166
         * Adds an info message about the position of the firebug call (origin).
167
         * This is very helpful if you spread Debug::firebug() calls all over your code.
168
         */
169
        $firephp->info(self::getOriginOfDebugCall());
0 ignored issues
show
self::getOriginOfDebugCall() is of type string|null, but the function expects a object<mixes>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
170
171
        $firephp->{$logmethod}($var);
172
173
        // @codeCoverageIgnoreEnd
174
    }
175
176
    /**
177
     * Returns the position of a call.
178
     *
179
     * This is used to determine the origin of the debug call.
180
     * Its mostly used in combination with several debug calls,
181
     * like \Koch\Debug\Debug::firebug() or \Koch\Debug\Debug::printR()
182
     * which are enhanced debug displays.
183
     *
184
     * It is a very helpful reminder to find and remove debug calls,
185
     * which you spread all over your code while tracking down a bug,
186
     * but forgot the trace path or where exactly they are.
187
     *
188
     * If you have a multitude of debug calls, debug breadcrumbs or toc
189
     * would be good ;) But that's another story...
190
     *
191
     * The default level is 2 (0,1,2), because we have to skip
192
     * the 3 calls to dump() and getWhereDebugWasCalled().
193
     *
194
     * @param int $level Default 1.
195
     *
196
     * @return string Message with origin of the debug call.
0 ignored issues
show
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
197
     */
198
    public static function getOriginOfDebugCall($level = 1)
0 ignored issues
show
getOriginOfDebugCall uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
199
    {
200
        $trace = [];
0 ignored issues
show
$trace is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
201
        $file  = $line  = $function  = $class  = $object  = $trace_line  = '';
0 ignored issues
show
$trace_line does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
$trace_line is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
$object is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
$class is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
$function is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
$line is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
$file is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
202
203
        // Get the backtrace and the caller information.
204
        $trace = debug_backtrace();
205
        $file  = $trace[$level]['file'];
206
        $line  = $trace[$level]['line'];
207
        #$function = $trace[$level]['function'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
208
        #$class = $trace[$level]['class'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
209
210
        /*
211
         * Get the file, to show the exact origin of the debug call.
212
         * The line with the call, is one line above.
213
         */
214
        $file_content   = file($file);
0 ignored issues
show
$file_content does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
215
        $origin_of_call = trim($file_content[ $trace[$level]['line'] - 1 ]);
0 ignored issues
show
$origin_of_call does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
216
217
        // do not use HTML tags on CLI
218
        if (PHP_SAPI === 'cli') {
219
            if (empty($_SERVER['REMOTE_ADDR'])) {
220
                $format = 'Debugging %s on line %s: %s' . "\n";
221
            } else {
222
                $format = '<pre>';
223
                $format .= '<b>Debugging <font color="red">%s</font> on line <font color="red">%s</font>:</b>' . "\n";
224
                $format .= '<div style="background: #f5f5f5; padding: 0.2em 0em;">%s</div></pre>';
225
            }
226
        }
227
228
        echo sprintf($format, basename($file), $line, htmlspecialchars($origin_of_call));
0 ignored issues
show
$origin_of_call does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
The variable $format 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...
229
    }
230
231
    /**
232
     * The method
233
     * - lists all currently included and required files
234
     * - counts all includes files
235
     * - calculates the total size (combined filesize) of all inclusions.
236
     */
237
    public static function getIncludedFiles($returnArray = false)
238
    {
239
        // init vars
240
        $includedFiles = $files = $result = [];
0 ignored issues
show
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
$files is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
241
        $totalSize     = 0;
242
243
        // fetch all included files
244
        $files = get_included_files();
245
246
        // loop over all included files and sum up filesize
247
        foreach ($files as $file) {
248
249
            // if system under test, skip virtual file system files,
250
            // as they might be already deleted by tearDown() methods.
251
            if (defined('UNIT_TEST_RUN') or UNIT_TEST_RUN) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as or instead of || is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
252
                if (stripos($file, 'vfs:/') !== false) {
253
                    continue;
254
                }
255
            }
256
257
            $size            = filesize($file);
258
            $includedFiles[] = ['name' => $file, 'size' => $size];
259
            $totalSize += $size;
260
        }
261
262
        $result = [
263
            'count' => count($files),
264
            'size'  => \Koch\Functions\Functions::getSize($totalSize),
265
            'files' => $includedFiles,
266
        ];
267
268
        return ($returnArray === true) ? $result : self::printR($result);
269
    }
270
271
    /**
272
     * Lists all user defined constants (Application Constants).
273
     */
274
    public static function getApplicationConstants($returnArray = false)
0 ignored issues
show
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...
275
    {
276
        $constants = get_defined_constants(true);
277
        $result    = $constants['user'];
278
279
        return ($returnArray === true) ? $result : self::printR($result);
280
    }
281
282
    /**
283
     * Displayes the debug backtrace.
284
     *
285
     * @param int Limit the number of stack frames returned. Returns all stack frames by default (limit=0).
286
     * @param bool
287
     */
288
    public static function getBacktrace($limit = 0, $returnArray = false)
289
    {
290
        $result = debug_backtrace($limit);
291
292
        return ($returnArray === true) ? $result : self::printR($result);
293
    }
294
295
    /**
296
     * Returns an array with the name of the defined interfaces.
297
     */
298
    public static function getInterfaces($returnArray = false)
299
    {
300
        $result = get_declared_interfaces();
301
302
        return ($returnArray === true) ? $result : self::printR($result);
303
    }
304
305
    /**
306
     * Returns an array with the name of the defined classes.
307
     */
308
    public static function getClasses($returnArray = false)
309
    {
310
        $result = get_declared_classes();
311
312
        return ($returnArray === true) ? $result : self::printR($result);
313
    }
314
315
    /**
316
     * Returns an array with the name of the defined classes.
317
     */
318
    public static function getFunctions($returnArray = false)
319
    {
320
        $result = get_defined_functions();
321
322
        return ($returnArray === true) ? $result : self::printR($result);
323
    }
324
325
    /**
326
     * Lists all php extensions.
327
     */
328
    public static function getExtensions($returnArray = false)
329
    {
330
        $result = get_loaded_extensions();
331
332
        return ($returnArray === true) ? $result : self::printR($result);
333
    }
334
335
    /**
336
     * Lists all php.ini settings.
337
     */
338
    public static function getPhpIni($returnArray = false)
339
    {
340
        $result = parse_ini_file(get_cfg_var('cfg_file_path'), true);
341
342
        return ($returnArray === true) ? $result : self::printR($result);
343
    }
344
345
    /**
346
     * Lists all available wrappers.
347
     */
348
    public static function getWrappers($returnArray = false)
349
    {
350
        $result = [];
351
352
        $wrappers = stream_get_wrappers();
353
354
        $result['openssl'] = (extension_loaded('openssl')) ? 'yes' : 'no';
355
        $result['http']    = in_array('http', $wrappers, true) ? 'yes' : 'no';
356
        $result['https']   = in_array('https', $wrappers, true) ? 'yes' : 'no';
357
        $result['all']     = $wrappers;
358
359
        return ($returnArray === true) ? $result : self::printR($result);
360
    }
361
}
362