Completed
Push — master ( bf5dfa...ec0188 )
by ignace
20:47
created

ErrorToException::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4286
cc 2
eloc 8
nc 2
nop 0
1
<?php
2
/**
3
* This file is part of the Carpediem.Errors library
4
*
5
* @license http://opensource.org/licenses/MIT
6
* @link https://github.com/carpediem/errors/
7
* @version 0.1.0
8
* @package Carpediem.errors
9
*
10
* For the full copyright and license information, please view the LICENSE
11
* file that was distributed with this source code.
12
*/
13
namespace Carpediem\Errors;
14
15
use InvalidArgumentException;
16
17
/**
18
 * A class to convert Error into Exception
19
 *
20
 * <?php
21
 *
22
 * use Carpediem\Errors\CaptureError;
23
 * use Carpediem\Errors\ErrorToException;
24
 *
25
 * $touch = new ErrorToException(new CaptureError('touch', E_WARNING), 'RuntimeException');
26
 * try {
27
 *    $touch('/usr/bin/foo.bar');
28
 * } catch (RuntimeException $e) {
29
 *    echo $e->getMessage();
30
 * }
31
 */
32
class ErrorToException
33
{
34
    /**
35
     * @var CaptureError
36
     */
37
    protected $callable;
38
39
    /**
40
     * Exception class to instantiate
41
     *
42
     * @var string
43
     */
44
    protected $exception_class_name;
45
46
    /**
47
     * A new instance
48
     *
49
     * @param CaptureError $callable
50
     * @param string       $exception_class_name The exception to be thrown
51
     */
52
    public function __construct(CaptureError $callable, $exception_class_name = 'RuntimeException')
53
    {
54
        $this->callable = $callable;
55
        $this->setExceptionClass($exception_class_name);
56
    }
57
58
    /**
59
     * Set the Exception class
60
     *
61
     * @param string $className error reporting level
62
     *
63
     * @throws InvalidArgumentException If the class is not a throwable object
64
     */
65
    public function setExceptionClass($className)
66
    {
67
        if (!$this->isThrowable($className) && !$this->isException($className)) {
68
            throw new InvalidArgumentException(sprintf(
69
                'The class name provided `%s` is neither a Throwable nor an Exception object',
70
                $className
71
            ));
72
        }
73
74
        $this->exception_class_name = $className;
75
    }
76
77
    /**
78
     * Is the ClassName a Throwable Object (PHP7+)
79
     *
80
     * @param string $className
81
     *
82
     * @return bool
83
     */
84
    protected function isThrowable($className)
85
    {
86
        return interface_exists('Throwable') && is_subclass_of($className, 'Throwable');
87
    }
88
89
    /**
90
     * Is the ClassName an Exception Object (PHP5+)
91
     *
92
     * @param string $className
93
     *
94
     * @return bool
95
     */
96
    protected function isException($className)
97
    {
98
        return $className === 'Exception' || is_subclass_of($className, 'Exception');
99
    }
100
101
    /**
102
     * Get the current Error Reporting Level
103
     *
104
     * @return string
105
     */
106
    public function getExceptionClass()
107
    {
108
        return $this->exception_class_name;
109
    }
110
111
    /**
112
     * Process the callable
113
     *
114
     * @param mixed ...$args the arguments associated with the callable
115
     *
116
     * @return mixed
117
     */
118
    public function __invoke()
119
    {
120
        $result = call_user_func_array($this->callable, func_get_args());
121
        $errorLevel = $this->callable->getErrorReporting();
0 ignored issues
show
Unused Code introduced by
$errorLevel 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...
122
        $errorCode = $this->callable->getLastErrorCode();
123
        $errorMessage = $this->callable->getLastErrorMessage();
124
        if (!empty($errorMessage)) {
125
            throw new $this->exception_class_name($errorMessage, $errorCode);
126
        }
127
128
        return $result;
129
    }
130
}
131