ErrorHandler::isDebug()   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
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * ErrorHandler
4
 *
5
 * @category    Erdiko
6
 * @package     Core
7
 * @copyright   Copyright (c) 2016, Arroyo Labs, http://www.arroyolabs.com
8
 * @author      Leo Daidone, [email protected]
9
 */
10
11
namespace erdiko\core;
12
13
use ToroHook;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, erdiko\core\ToroHook.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
14
15
class ErrorHandler 
16
{
17
	public static function init()
18
	{
19
		ini_set('html_errors',0); // @todo review this line
20
		// error_reporting((E_ALL | E_STRICT)); // @note we shouldn't override by default, but we could inject
0 ignored issues
show
Unused Code Comprehensibility introduced by
47% 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...
21
		set_error_handler("\\erdiko\\core\\ErrorHandler::errorHandler");
22
		register_shutdown_function("\\erdiko\\core\\ErrorHandler::fatalErrorShutdownHandler");
23
	}
24
25
	public static function errorHandler($errno, $errstr, $errfile, $errline)
0 ignored issues
show
Coding Style Best Practice introduced by
Please use __construct() instead of a PHP4-style constructor that is named after the class.
Loading history...
26
	{
27
		$debug = \erdiko\core\ErrorHandler::isDebug();
28
		if ( ! ( error_reporting() & $errno ) || empty( $errstr ) ) {
29
			return null;
30
		}
31
32
        $errorHook = "500";
33
34
		switch ( $errno ) {
35
			case E_USER_ERROR:
36
				$vars['msg_type']        = "USER ERROR";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$vars was never initialized. Although not strictly required by PHP, it is generally a good practice to add $vars = 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...
37
				$vars['msg_description'] = " Fatal error in line $errline of $errfile file";
38
				$vars['msg_description'] .= ", PHP " . PHP_VERSION . " (" . PHP_OS . ")";
39
				break;
40
41
			case E_USER_WARNING:
42
				$vars['msg_type'] = "USER WARNING";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$vars was never initialized. Although not strictly required by PHP, it is generally a good practice to add $vars = 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...
43
				break;
44
45
			case E_USER_NOTICE:
46
				$vars['msg_type'] = "USER NOTICE";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$vars was never initialized. Although not strictly required by PHP, it is generally a good practice to add $vars = 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...
47
				break;
48
49
			case E_ERROR:
50
				$vars['msg_type'] = "ERROR";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$vars was never initialized. Although not strictly required by PHP, it is generally a good practice to add $vars = 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...
51
				$vars['msg_description'] = print_r(debug_backtrace(),1);
52
				break;
53
54
			default:
55
				$vars['msg_type'] = "Type of error: [$errno] $errstr";
0 ignored issues
show
Coding Style Comprehensibility introduced by
$vars was never initialized. Although not strictly required by PHP, it is generally a good practice to add $vars = 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...
56
				$vars['msg_description'] = print_r(debug_backtrace(),1);
57
				break;
58
		}
59
60
		$vars['code']               = $errno;
61
		$vars['error']              = trim($errstr);
62
		$vars['path_info']          = $errfile . " on line " . $errline;
63
        	$vars['debug']              = $debug;
64
65
        	ToroHook::fire($errorHook, $vars );
66
67
		return false;
68
	}
69
70
	public static function fatalErrorShutdownHandler()
71
	{
72
		$last_error = error_get_last();
73
		static::errorHandler(E_ERROR, 
74
			$last_error['message'], $last_error['file'], $last_error['line']);
75
	}
76
77
	/**
78
	 * isDebug
79
	 *
80
	 * @return bool
81
	 */
82
	public static function isDebug()
83
	{
84
		return (getenv("ERDIKO_DEBUG")=='1');
85
	}
86
}
87