ErrorUtil::prepareMessage()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * AppserverIo\Appserver\Core\Utilities\ErrorUtil
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2015 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/appserver-io/appserver
18
 * @link      http://www.appserver.io
19
 */
20
21
namespace AppserverIo\Appserver\Core\Utilities;
22
23
use Psr\Log\LogLevel;
24
25
// define a custom user exception constant
26
define('E_EXCEPTION', 0);
27
28
/**
29
 * Utility class that providing functionality to handle PHP errors.
30
 *
31
 * @author    Tim Wagner <[email protected]>
32
 * @copyright 2015 TechDivision GmbH <[email protected]>
33
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
34
 * @link      https://github.com/appserver-io/appserver
35
 * @link      http://www.appserver.io
36
 */
37
class ErrorUtil
38
{
39
40
    /**
41
     * The singleton instance.
42
     *
43
     * @var \AppserverIo\Appserver\ServletEngine\Utils\ErrorUtil
44
     */
45
    protected static $instance;
46
47
    /**
48
     * Create's and return's the singleton instance.
49
     *
50
     * @return \AppserverIo\Appserver\ServletEngine\Utils\ErrorUtil The singleton instance
51
     */
52
    public static function singleton()
53
    {
54
55
        // query whether or not the instance has already been created
56
        if (ErrorUtil::$instance == null) {
57
            ErrorUtil::$instance = new static();
58
        }
59
60
        // return the singleton instance
61
        return ErrorUtil::$instance;
62
    }
63
64
    /**
65
     * Create's a new error instance with the values from the passed array.
66
     *
67
     * @param array $error The array containing the error information
68
     *
69
     * @return \AppserverIo\Appserver\Core\Utilities\ErrorInterface The error instance
70
     */
71
    public function fromArray(array $error)
72
    {
73
74
        // extract the array with the error information
75
        list ($type, $message, $file, $line) = array_values($error);
76
77
        // initialize and return the error instance
78
        return new Error($type, $message, $file, $line);
79
    }
80
81
    /**
82
     * Create's a new error instance from the passed exception.
83
     *
84
     * @param \Exception $e The exception to create the error instance from
85
     *
86
     * @return \AppserverIo\Appserver\Core\Utilities\ErrorInterface The error instance
87
     */
88
    public function fromException(\Exception $e)
89
    {
90
        return new Error(E_EXCEPTION, $e->__toString(), $e->getFile(), $e->getLine(), $e->getCode());
0 ignored issues
show
Unused Code introduced by
The call to AppserverIo\Appserver\Co...es\Error::__construct() has too many arguments starting with $e->getCode(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

90
        return /** @scrutinizer ignore-call */ new Error(E_EXCEPTION, $e->__toString(), $e->getFile(), $e->getLine(), $e->getCode());

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
91
    }
92
93
    /**
94
     * Prepare's the error message for logging/rendering purposes.
95
     *
96
     * @param \AppserverIo\Appserver\Core\Utilities\ErrorInterface $error The error instance to create the message from
97
     *
98
     * @return string The error message
99
     */
100
    public function prepareMessage(ErrorInterface $error)
101
    {
102
        return sprintf('PHP %s: %s in %s on line %d', $this->mapErrorCode($error), $error->getMessage(), $error->getFile(), $error->getLine());
103
    }
104
105
    /**
106
     * Return's the log level for the passed error instance.
107
     *
108
     * @param \AppserverIo\Appserver\Core\Utilities\ErrorInterface $error The error instance to map the log level for
109
     *
110
     * @return string
111
     */
112
    public function mapLogLevel(ErrorInterface $error)
113
    {
114
115
        // initialize the log level, default is 'error'
116
        $logLevel = LogLevel::ERROR;
117
118
        // query the error type
119
        switch ($error->getType()) {
120
            case E_WARNING:
121
            case E_USER_WARNING:
122
            case E_COMPILE_WARNING:
123
            case E_RECOVERABLE_ERROR:
124
                $logLevel = LogLevel::WARNING;
125
                break;
126
127
            case E_NOTICE:
128
            case E_USER_NOTICE:
129
            case E_STRICT:
130
            case E_DEPRECATED:
131
            case E_USER_DEPRECATED:
132
                $logLevel = LogLevel::NOTICE;
133
                break;
134
135
            default:
136
                break;
137
        }
138
139
        // return the log level
140
        return $logLevel;
141
    }
142
143
    /**
144
     * Return's the a human readable error representation for the passed error instance.
145
     *
146
     * @param \AppserverIo\Appserver\Core\Utilities\ErrorInterface $error The error instance
147
     *
148
     * @return string The human readable error representation
149
     */
150
    public function mapErrorCode(ErrorInterface $error)
151
    {
152
153
        // initialize the error representation
154
        $wrapped = 'Unknown';
155
156
        // query the error type
157
        switch ($error->getType()) {
158
            case E_EXCEPTION:
159
                $wrapped = 'Exception';
160
                break;
161
162
            case E_PARSE:
163
            case E_ERROR:
164
            case E_CORE_ERROR:
165
            case E_COMPILE_ERROR:
166
            case E_USER_ERROR:
167
                $wrapped = 'Fatal Error';
168
                break;
169
170
            case E_WARNING:
171
            case E_USER_WARNING:
172
            case E_COMPILE_WARNING:
173
            case E_RECOVERABLE_ERROR:
174
                $wrapped = 'Warning';
175
                break;
176
177
            case E_NOTICE:
178
            case E_USER_NOTICE:
179
                $wrapped = 'Notice';
180
                break;
181
182
            case E_STRICT:
183
                $wrapped = 'Strict';
184
                break;
185
186
            case E_DEPRECATED:
187
            case E_USER_DEPRECATED:
188
                $wrapped = 'Deprecated';
189
                break;
190
191
            default:
192
                break;
193
        }
194
195
        // return the human readable error representation
196
        return $wrapped;
197
    }
198
199
    /**
200
     * Query whether or not the passed error type is fatal or not.
201
     *
202
     * @param integer $type The type to query for
203
     *
204
     * @return boolean TRUE if the passed type is a fatal error, else FALSE
205
     */
206
    public function isFatal($type)
207
    {
208
        return in_array($type, array(E_EXCEPTION, E_PARSE,  E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_USER_ERROR));
209
    }
210
}
211