Completed
Push — master ( 836a37...a887c3 )
by Ryosuke
06:09
created

Convert::toErrorException()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 16
Code Lines 10

Duplication

Lines 6
Ratio 37.5 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 0
Metric Value
dl 6
loc 16
ccs 9
cts 9
cp 1
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 10
nc 3
nop 2
crap 6
1
<?php
2
3
namespace mpyw\Exceper;
4
5
/**
6
 * Class Exceper
7
 * @package mpyw\Exceper
8
 * @method static mixed to(string $class, callable $callback, int $types = E_ALL | E_STRICT)
9
 * @method static mixed toException(callable $callback, int $types = E_ALL | E_STRICT)
10
 * @method static mixed toError(callable $callback, int $types = E_ALL | E_STRICT)
11
 * @method static mixed toParseError(callable $callback, int $types = E_ALL | E_STRICT)
12
 * @method static mixed toTypeError(callable $callback, int $types = E_ALL | E_STRICT)
13
 * @method static mixed toArgumentCountError(callable $callback, int $types = E_ALL | E_STRICT)
14
 * @method static mixed toArithmeticError(callable $callback, int $types = E_ALL | E_STRICT)
15
 * @method static mixed toDivisionByZeroError(callable $callback, int $types = E_ALL | E_STRICT)
16
 * @method static mixed toClosedGeneratorException(callable $callback, int $types = E_ALL | E_STRICT)
17
 * @method static mixed toDOMException(callable $callback, int $types = E_ALL | E_STRICT)
18
 * @method static mixed toLogicException(callable $callback, int $types = E_ALL | E_STRICT)
19
 * @method static mixed toBadFunctionCallException(callable $callback, int $types = E_ALL | E_STRICT)
20
 * @method static mixed toBadMethodCallException(callable $callback, int $types = E_ALL | E_STRICT)
21
 * @method static mixed toDomainException(callable $callback, int $types = E_ALL | E_STRICT)
22
 * @method static mixed toInvalidArgumentException(callable $callback, int $types = E_ALL | E_STRICT)
23
 * @method static mixed toLengthException(callable $callback, int $types = E_ALL | E_STRICT)
24
 * @method static mixed toOutOfRangeException(callable $callback, int $types = E_ALL | E_STRICT)
25
 * @method static mixed toRuntimeException(callable $callback, int $types = E_ALL | E_STRICT)
26
 * @method static mixed toOutOfBoundsException(callable $callback, int $types = E_ALL | E_STRICT)
27
 * @method static mixed toOverflowException(callable $callback, int $types = E_ALL | E_STRICT)
28
 * @method static mixed toRangeException(callable $callback, int $types = E_ALL | E_STRICT)
29
 * @method static mixed toUnderflowException(callable $callback, int $types = E_ALL | E_STRICT)
30
 * @method static mixed toUnexpectedValueException(callable $callback, int $types = E_ALL | E_STRICT)
31
 * @method static mixed toAssertionError(callable $callback, int $types = E_ALL | E_STRICT)
32
 * @method static mixed toPDOException(callable $callback, int $types = E_ALL | E_STRICT)
33
 * @method static mixed toPharException(callable $callback, int $types = E_ALL | E_STRICT)
34
 * @method static mixed toReflectionException(callable $callback, int $types = E_ALL | E_STRICT)
35
 * @method static mixed tomysqli_sql_exception(callable $callback, int $types = E_ALL | E_STRICT)
36
 * @method static mixed toSoapFault(callable $callback, int $types = E_ALL | E_STRICT)
37
 */
38
class Convert
39
{
40
    /**
41
     * @param string $method
42
     * @param array $args
43
     * @param int $required
44
     */
45 21
    protected static function validateArgumentTypes($method, array $args, $required)
46
    {
47 21
        $argc = count($args);
48 21
        if ($argc < $required) {
49 3
            throw new \InvalidArgumentException(get_called_class() . "::$method() expects at least $required parameters, $argc given");
50
        }
51 18
        if ($required === 2 and !is_string($args[0]) and !is_object($args[0]) || !method_exists($args[0], '__toString')) {
52 1
            throw new \InvalidArgumentException(get_called_class() . "::$method() expects parameter 1 to be string, " . gettype($args[0]) . ' given');
53
        }
54 17
        if (!is_callable($args[$required - 1])) {
55 3
            throw new \InvalidArgumentException(get_called_class() . "::$method() expects parameter $required to be callable, " . gettype($args[$required - 1]) . ' given');
56
        }
57 14
        if (isset($args[$required]) && !is_numeric($args[$required])) {
58 3
            throw new \InvalidArgumentException(get_called_class() . "::$method() expects parameter " . ($required + 1) . ' to be integer, ' . gettype($args[$required]) . ' given');
59
        }
60 11
    }
61
62
    /**
63
     * @param string $method
64
     * @param array $args
65
     * @return mixed
66
     */
67 22
    public static function __callStatic($method, array $args)
68
    {
69 22
        if (strcasecmp(substr($method, 0, 2), 'to')) {
70 1
            throw new \BadMethodCallException('Call to undefined method ' . get_called_class() . "::$method()");
71
        }
72
73 21
        $class = (string)substr($method, 2);
74 21
        $required = $class === '' ? 2 : 1;
75 21
        self::validateArgumentTypes($method, $args, $required);
76
77 11
        if ($class === '') {
78 5
            $class = (string)array_shift($args);
79
        }
80
81 11
        if (!class_exists($class) and !is_subclass_of($class, '\Exception') and \PHP_VERSION < 7 || !is_subclass_of($class, '\Throwable')) {
82 1
            throw new \DomainException("The class \"$class\" must be an instance of Exception or Throwable");
83
        }
84
85 10
        return Core::handle($args[0], function ($severity, $message, $file, $line) use ($class, $args) {
86 7
            if (!(error_reporting() & $severity)) {
87 2
                return;
88
            }
89 5
            if (strcasecmp($class, 'ErrorException')) {
90 4
                throw Core::rewriteLocation(new $class($message), $file, $line);
91
            } else {
92 1
                throw new \ErrorException($message, 0, $severity, $file, $line);
93
            }
94 10
        }, isset($args[1]) ? (int)$args[1] : \E_ALL | \E_STRICT);
95
    }
96
97
    /**
98
     * @param callable $callback
99
     * @param int $types
100
     * @param bool $captureExceptions
101
     * @return mixed
102
     */
103 6
    public static function silent($callback, $types = null, $captureExceptions = true)
104
    {
105 6 View Code Duplication
        if (!is_callable($callback)) {
106 1
            throw new \InvalidArgumentException(get_called_class() . '::' . __METHOD__ . '() expects parameter 1 to be callable, ' . gettype($callback) . ' given');
107
        }
108 5 View Code Duplication
        if ($types !== null && !is_numeric($types)) {
109 1
            throw new \InvalidArgumentException(get_called_class() . '::' . __METHOD__ . '() expects parameter 2 to be integer, ' . gettype($types) . ' given');
110
        }
111
112 4
        $dummyException = new \Exception;
113
        try {
114 4
            return Core::handle($callback, function ($severity) use ($dummyException) {
115 3
                if (!(error_reporting() & $severity)) {
116 1
                    return;
117
                }
118 3
                throw $dummyException;
119 4
            }, (int)($types === null ? \E_ALL | \E_STRICT : $types));
120
        } catch (\Exception $e) {
121
        } catch (\Throwable $e) {
122
        }
123
        if (!$captureExceptions && $e !== $dummyException) {
124
            throw $e;
125
        }
126
    }
127
}
128