Completed
Push — master ( 80261e...48b4e4 )
by Ryosuke
03:25 queued 01:24
created

Convert::validateArgumentTypes()   C

Complexity

Conditions 13
Paths 18

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 13

Importance

Changes 0
Metric Value
dl 0
loc 23
ccs 16
cts 16
cp 1
rs 5.2908
c 0
b 0
f 0
cc 13
eloc 15
nc 18
nop 4
crap 13

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 to(string $class, int $code, callable $callback, int $types = E_ALL | E_STRICT)
10
 * @method static mixed toException(callable $callback, int $types = E_ALL | E_STRICT)
11
 * @method static mixed toException(int $code, callable $callback, int $types = E_ALL | E_STRICT)
12
 * @method static mixed toError(callable $callback, int $types = E_ALL | E_STRICT)
13
 * @method static mixed toError(int $code, callable $callback, int $types = E_ALL | E_STRICT)
14
 * @method static mixed toParseError(callable $callback, int $types = E_ALL | E_STRICT)
15
 * @method static mixed toParseError(int $code, callable $callback, int $types = E_ALL | E_STRICT)
16
 * @method static mixed toTypeError(callable $callback, int $types = E_ALL | E_STRICT)
17
 * @method static mixed toTypeError(int $code, callable $callback, int $types = E_ALL | E_STRICT)
18
 * @method static mixed toArgumentCountError(callable $callback, int $types = E_ALL | E_STRICT)
19
 * @method static mixed toArgumentCountError(int $code, callable $callback, int $types = E_ALL | E_STRICT)
20
 * @method static mixed toArithmeticError(callable $callback, int $types = E_ALL | E_STRICT)
21
 * @method static mixed toArithmeticError(int $code, callable $callback, int $types = E_ALL | E_STRICT)
22
 * @method static mixed toDivisionByZeroError(callable $callback, int $types = E_ALL | E_STRICT)
23
 * @method static mixed toDivisionByZeroError(int $code, callable $callback, int $types = E_ALL | E_STRICT)
24
 * @method static mixed toClosedGeneratorException(callable $callback, int $types = E_ALL | E_STRICT)
25
 * @method static mixed toClosedGeneratorException(int $code, callable $callback, int $types = E_ALL | E_STRICT)
26
 * @method static mixed toDOMException(callable $callback, int $types = E_ALL | E_STRICT)
27
 * @method static mixed toDOMException(int $code, callable $callback, int $types = E_ALL | E_STRICT)
28
 * @method static mixed toLogicException(callable $callback, int $types = E_ALL | E_STRICT)
29
 * @method static mixed toLogicException(int $code, callable $callback, int $types = E_ALL | E_STRICT)
30
 * @method static mixed toBadFunctionCallException(callable $callback, int $types = E_ALL | E_STRICT)
31
 * @method static mixed toBadFunctionCallException(int $code, callable $callback, int $types = E_ALL | E_STRICT)
32
 * @method static mixed toBadMethodCallException(callable $callback, int $types = E_ALL | E_STRICT)
33
 * @method static mixed toBadMethodCallException(int $code, callable $callback, int $types = E_ALL | E_STRICT)
34
 * @method static mixed toDomainException(callable $callback, int $types = E_ALL | E_STRICT)
35
 * @method static mixed toDomainException(int $code, callable $callback, int $types = E_ALL | E_STRICT)
36
 * @method static mixed toInvalidArgumentException(callable $callback, int $types = E_ALL | E_STRICT)
37
 * @method static mixed toInvalidArgumentException(int $code, callable $callback, int $types = E_ALL | E_STRICT)
38
 * @method static mixed toLengthException(callable $callback, int $types = E_ALL | E_STRICT)
39
 * @method static mixed toLengthException(int $code, callable $callback, int $types = E_ALL | E_STRICT)
40
 * @method static mixed toOutOfRangeException(callable $callback, int $types = E_ALL | E_STRICT)
41
 * @method static mixed toOutOfRangeException(int $code, callable $callback, int $types = E_ALL | E_STRICT)
42
 * @method static mixed toRuntimeException(callable $callback, int $types = E_ALL | E_STRICT)
43
 * @method static mixed toRuntimeException(int $code, callable $callback, int $types = E_ALL | E_STRICT)
44
 * @method static mixed toOutOfBoundsException(callable $callback, int $types = E_ALL | E_STRICT)
45
 * @method static mixed toOutOfBoundsException(int $code, callable $callback, int $types = E_ALL | E_STRICT)
46
 * @method static mixed toOverflowException(callable $callback, int $types = E_ALL | E_STRICT)
47
 * @method static mixed toOverflowException(int $code, callable $callback, int $types = E_ALL | E_STRICT)
48
 * @method static mixed toRangeException(callable $callback, int $types = E_ALL | E_STRICT)
49
 * @method static mixed toRangeException(int $code, callable $callback, int $types = E_ALL | E_STRICT)
50
 * @method static mixed toUnderflowException(callable $callback, int $types = E_ALL | E_STRICT)
51
 * @method static mixed toUnderflowException(int $code, callable $callback, int $types = E_ALL | E_STRICT)
52
 * @method static mixed toUnexpectedValueException(callable $callback, int $types = E_ALL | E_STRICT)
53
 * @method static mixed toUnexpectedValueException(int $code, callable $callback, int $types = E_ALL | E_STRICT)
54
 * @method static mixed toAssertionError(callable $callback, int $types = E_ALL | E_STRICT)
55
 * @method static mixed toAssertionError(int $code, callable $callback, int $types = E_ALL | E_STRICT)
56
 * @method static mixed toPDOException(callable $callback, int $types = E_ALL | E_STRICT)
57
 * @method static mixed toPDOException(int $code, callable $callback, int $types = E_ALL | E_STRICT)
58
 * @method static mixed toPharException(callable $callback, int $types = E_ALL | E_STRICT)
59
 * @method static mixed toPharException(int $code, callable $callback, int $types = E_ALL | E_STRICT)
60
 * @method static mixed toReflectionException(callable $callback, int $types = E_ALL | E_STRICT)
61
 * @method static mixed toReflectionException(int $code, callable $callback, int $types = E_ALL | E_STRICT)
62
 * @method static mixed tomysqli_sql_exception(callable $callback, int $types = E_ALL | E_STRICT)
63
 * @method static mixed tomysqli_sql_exception(int $code, callable $callback, int $types = E_ALL | E_STRICT)
64
 * @method static mixed toSoapFault(callable $callback, int $types = E_ALL | E_STRICT)
65
 * @method static mixed toSoapFault(int $code, callable $callback, int $types = E_ALL | E_STRICT)
66
 */
67
class Convert
68
{
69
    /**
70
     * @param string $method
71
     * @param array $args
72
     * @param int $required
73
     * @param int &$code
74
     */
75 33
    protected static function validateArgumentTypes($method, array $args, $required, &$code = null)
76
    {
77 33
        $argc = count($args);
78 33
        if ($argc < $required) {
79 3
            throw new \InvalidArgumentException(get_called_class() . "::$method() expects at least $required parameters, $argc given");
80
        }
81 30
        if ($required === 2 and !is_string($args[0]) and !is_object($args[0]) || !method_exists($args[0], '__toString')) {
82 2
            throw new \InvalidArgumentException(get_called_class() . "::$method() expects parameter 1 to be string, " . gettype($args[0]) . ' given');
83
        }
84 28
        if (isset($args[$required - 1]) && is_numeric($args[$required - 1])) {
85 14
            $code = $args[$required++ - 1];
86
        }
87 28
        $orInteger = $code === null ? " or integer" : '';
88 28
        if (!array_key_exists($required - 1, $args)) {
89 2
            throw new \InvalidArgumentException(get_called_class() . "::$method() expects parameter $required to be callable$orInteger, none given");
90
        }
91 26
        if (!is_callable($args[$required - 1])) {
92 4
            throw new \InvalidArgumentException(get_called_class() . "::$method() expects parameter $required to be callable$orInteger, " . gettype($args[$required - 1]) . ' given');
93
        }
94 22
        if (isset($args[$required]) && !is_numeric($args[$required])) {
95 4
            throw new \InvalidArgumentException(get_called_class() . "::$method() expects parameter " . ($required + 1) . " to be integer, " . gettype($args[$required]) . ' given');
96
        }
97 18
    }
98
99
    /**
100
     * @param string $method
101
     * @param array $args
102
     * @return mixed
103
     */
104 34
    public static function __callStatic($method, array $args)
105
    {
106 34
        if (strcasecmp(substr($method, 0, 2), 'to')) {
107 1
            throw new \BadMethodCallException('Call to undefined method ' . get_called_class() . "::$method()");
108
        }
109
110 33
        $class = (string)substr($method, 2);
111 33
        $required = $class === '' ? 2 : 1;
112 33
        self::validateArgumentTypes($method, $args, $required, $code);
113
114 18
        if ($class === '') {
115 10
            $class = (string)array_shift($args);
116
        }
117
118 18
        if (!class_exists($class) and !is_subclass_of($class, '\Exception') and \PHP_VERSION < 7 || !is_subclass_of($class, '\Throwable')) {
119 2
            throw new \DomainException("The class \"$class\" must be an instance of Exception or Throwable");
120
        }
121
122 16
        return Core::handle($args[$code !== null], function ($severity, $message, $file, $line) use ($class, $code) {
123 11
            if (!(error_reporting() & $severity)) {
124 2
                return;
125
            }
126 9
            if (strcasecmp($class, 'ErrorException') && strcasecmp($class, '\ErrorException')) {
127 8
                throw Core::rewriteLocation(new $class($message, $code ?: 0), $file, $line);
128
            } else {
129 1
                throw new \ErrorException($message, 0, $severity, $file, $line);
130
            }
131 16
        }, isset($args[1 + ($code !== null)]) ? (int)$args[1 + ($code !== null)] : \E_ALL | \E_STRICT);
132
    }
133
134
    /**
135
     * @param callable $callback
136
     * @param int $types
137
     * @param bool $captureExceptions
138
     * @return mixed
139
     */
140 6
    public static function silent($callback, $types = null, $captureExceptions = true)
141
    {
142 6 View Code Duplication
        if (!is_callable($callback)) {
143 1
            throw new \InvalidArgumentException(get_called_class() . '::' . __METHOD__ . '() expects parameter 1 to be callable, ' . gettype($callback) . ' given');
144
        }
145 5 View Code Duplication
        if ($types !== null && !is_numeric($types)) {
146 1
            throw new \InvalidArgumentException(get_called_class() . '::' . __METHOD__ . '() expects parameter 2 to be integer, ' . gettype($types) . ' given');
147
        }
148
149 4
        $dummyException = new \Exception;
150
        try {
151 4
            return Core::handle($callback, function ($severity) use ($dummyException) {
152 3
                if (!(error_reporting() & $severity)) {
153 1
                    return;
154
                }
155 3
                throw $dummyException;
156 4
            }, (int)($types === null ? \E_ALL | \E_STRICT : $types));
157
        } catch (\Exception $e) {
158
        } catch (\Throwable $e) {
159
        }
160
        if (!$captureExceptions && $e !== $dummyException) {
161
            throw $e;
162
        }
163
    }
164
}
165