error   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 0
loc 112
rs 10
c 0
b 0
f 0
ccs 14
cts 14
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A set() 0 5 2
A trigger() 0 8 3
A exception() 0 7 2
A standard() 0 7 2
1
<?php
2
/**
3
 * Error Handler Class
4
 */
5
6
namespace projectcleverweb\color;
7
8
/**
9
 * Error Handler Class
10
 */
11
class error {
12
	
13
	/**
14
	 * Error Type Constants
15
	 * ====================
16
	 * These constants map to descriptive exception classes
17
	 */
18
	
19
	const GENERAL_ERROR    = 0;
20
	const INVALID_CONFIG   = 1;
21
	const INVALID_VALUE    = 2;
22
	const INVALID_ARGUMENT = 4;
23
	const INVALID_COLOR    = 8;
24
	const OUT_OF_RANGE     = 16;
25
	
26
	/**
27
	 * Controls whether or not error reporting is active.
28
	 * @var boolean
29
	 */
30
	protected static $active = TRUE;
31 92
	
32 92
	/**
33 92
	 * Controls whether or not to use trigger_error() or to throw exceptions.
34
	 * @var boolean
35 92
	 */
36
	protected static $use_exceptions = TRUE;
37
	
38
	/**
39
	 * Maps each constant to a readable type string
40
	 * 
41
	 * @var array
42
	 */
43
	protected static $type_map = array(
44 11
		self::GENERAL_ERROR    => 'GENERAL',
45 11
		self::INVALID_CONFIG   => 'INVALID_CONFIG',
46 4
		self::INVALID_VALUE    => 'INVALID_VALUE',
47 8
		self::INVALID_ARGUMENT => 'INVALID_ARGUMENT',
48 1
		self::INVALID_COLOR    => 'INVALID_COLOR',
49
		self::OUT_OF_RANGE     => 'OUT_OF_RANGE',
50 7
	);
51
	
52
	/**
53
	 * Maps each constant to a descriptive exception class
54
	 * 
55
	 * @var array
56
	 */
57
	protected static $exception_map = array(
58 4
		self::GENERAL_ERROR    => __NAMESPACE__.'\\exceptions\\general_error',
59 4
		self::INVALID_CONFIG   => __NAMESPACE__.'\\exceptions\\invalid_config',
60
		self::INVALID_VALUE    => __NAMESPACE__.'\\exceptions\\invalid_value',
61
		self::INVALID_ARGUMENT => __NAMESPACE__.'\\exceptions\\invalid_argument',
62
		self::INVALID_COLOR    => __NAMESPACE__.'\\exceptions\\invalid_color',
63
		self::OUT_OF_RANGE     => __NAMESPACE__.'\\exceptions\\out_of_range',
64
	);
65
	
66
	/**
67
	 * Allows the modifying of configuration vars for this class.
68 1
	 * 
69 1
	 * @param string $var   The variable to modify
70
	 * @param bool   $value The value to set the variable to
71
	 */
72
	public static function set(string $var, bool $value) {
73
		if (isset(self::$$var)) {
74
			static::$$var = $value;
75
		}
76
	}
77
	
78
	/**
79
	 * This function chooses the method to report the error if static::$active is
80
	 * equal to TRUE.
81
	 * 
82
	 * @param  int    $code    The error code constant that was passed
83
	 * @param  string $message The message describing the error
84
	 * @return void
85
	 */
86
	public static function trigger(int $code, string $message) {
87
		if (!static::$active) {
88
			return;
89
		} elseif(static::$use_exceptions) {
90
			static::exception($message, $code);
91
		}
92
		static::standard($message, $code);
93
	}
94
	
95
	/**
96
	 * Throws an exception with $message
97
	 * 
98
	 * @param  string $message The message describing the error
99
	 * @return void
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use NoType.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
100
	 */
101
	protected static function exception(string $message, int $code = 0) {
102
		if (!isset(static::$exception_map[$code])) {
103
			throw new exceptions\general_error('Unknown error type for error: '.$message, $code);
104
			$code = 0;
0 ignored issues
show
Unused Code introduced by
$code = 0; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
105
		}
106
		throw new static::$exception_map[$code]($message, $code);
107
	}
108
	
109
	/**
110
	 * Triggers a E_USER_WARNING error with $message
111
	 * 
112
	 * @param  string $message The message describing the error
113
	 * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be boolean?

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...
114
	 */
115
	protected static function standard(string $message, int $code = 0) {
116
		if (!isset(static::$type_map[$code])) {
117
			trigger_error('Unknown error type for error: '.$message);
118
			$code = 0;
119
		}
120
		return trigger_error(sprintf('[%s] %s', static::$type_map[$code], $message), E_USER_WARNING);
121
	}
122
}
123