Passed
Push — master ( c34495...8adf73 )
by Darío
03:13
created

ErrorHandler   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 37.5%

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 47
ccs 3
cts 8
cp 0.375
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A errorControlOperator() 0 6 2
A toException() 0 10 2
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Error;
12
13
/**
14
 * ErrorHandler class
15
 *
16
 * This class handles errors with user defined functions
17
 */
18
class ErrorHandler
19
{
20
    /**
21
     * Handles errors and transform it to exceptions
22
     *
23
     * @param integer $errno
24
     * @param string $errstr
25
     * @param string $errfile
26
     * @param integer $errline
27
     *
28
     * @throws RuntimeException
29
     *
30
     * @return null|boolean
31
     */
32
    public static function toException($errno, $errstr, $errfile, $errline)
33
    {
34
        if (!(error_reporting() & $errno)) {
35
            // This error code is not included in error_reporting, so let it fall
36
            // through to the standard PHP error handler
37
            return false;
38
        }
39
40
        throw new \RuntimeException(
41
            "<strong>Error:</strong> $errstr in <strong>$errfile</strong> on line <strong>$errline</strong>"
42
        );
43
    }
44
45
    /**
46
     * Better way to use the error-control operator @
47
     *
48
     * By default @ operator hides Fatal errors (Ex: Maximum time execution).
49
     * The errorControlOperator could be setted as handler with set_error_handler($callable $e, E_ALL)
50
     * and it takes the same behavior of @, but it does not ignore Fatal errors.
51
     *
52
     * @param integer $errno
53
     * @param string  $errstr
54
     * @param string  $errfile
55
     * @param integer $errline
56
     *
57
     * @return null|boolean
58
     */
59 12
    public static function errorControlOperator($errno, $errstr, $errfile, $errline)
0 ignored issues
show
Unused Code introduced by
The parameter $errline is not used and could be removed. ( Ignorable by Annotation )

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

59
    public static function errorControlOperator($errno, $errstr, $errfile, /** @scrutinizer ignore-unused */ $errline)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $errstr is not used and could be removed. ( Ignorable by Annotation )

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

59
    public static function errorControlOperator($errno, /** @scrutinizer ignore-unused */ $errstr, $errfile, $errline)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $errfile is not used and could be removed. ( Ignorable by Annotation )

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

59
    public static function errorControlOperator($errno, $errstr, /** @scrutinizer ignore-unused */ $errfile, $errline)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
60
    {
61 12
        if (!(error_reporting() & $errno)) {
62
            // This error code is not included in error_reporting, so let it fall
63
            // through to the standard PHP error handler
64 2
            return false;
65
        }
66 10
    }
67
}
68