CallableReflection::create()   B
last analyzed

Complexity

Conditions 11
Paths 10

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 11

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
eloc 15
c 2
b 0
f 0
nc 10
nop 1
dl 0
loc 26
ccs 15
cts 15
cp 1
crap 11
rs 7.3166

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
declare(strict_types=1);
4
5
/*
6
 * This file is part of PHP Invoker.
7
 *
8
 * PHP version 7.1 and above required
9
 *
10
 * @author    Divine Niiquaye Ibok <[email protected]>
11
 * @copyright 2019 Biurad Group (https://biurad.com/)
12
 * @license   https://opensource.org/licenses/BSD-3-Clause License
13
 *
14
 * For the full copyright and license information, please view the LICENSE
15
 * file that was distributed with this source code.
16
 */
17
18
namespace DivineNii\Invoker;
19
20
use DivineNii\Invoker\Exceptions\NotCallableException;
21
22
/**
23
 * Create a reflection object from a callable.
24
 *
25
 * @author Matthieu Napoli <[email protected]>
26
 * @author Divine Niiquaye Ibok <[email protected]>
27
 */
28
class CallableReflection
29
{
30
    /**
31
     * @param callable|mixed[]|object|string $callable
32
     *
33
     * @throws NotCallableException
34
     *
35
     * @return \ReflectionFunctionAbstract
36
     */
37 50
    public static function create($callable): \ReflectionFunctionAbstract
38
    {
39
        // Standard function and closure
40 50
        if ($callable instanceof \Closure || (\is_string($callable) && \function_exists($callable))) {
41 24
            return new \ReflectionFunction($callable);
42
        }
43
44 27
        if (is_object($callable) && method_exists($callable, '__invoke')) {
45 2
            $callable = [$callable, '__invoke'];
46 26
        } elseif (\is_string($callable) && \strpos($callable, '::') !== false) {
47 2
            $callable = \explode('::', $callable, 2);
48
        }
49
50 27
        if (!\is_callable($callable)) {
51 1
            throw new NotCallableException(\sprintf(
52 1
                '%s is not a callable',
53 1
                \is_object($callable) ? 'Instance of ' . \get_class($callable) : var_export($callable, true)
54
            ));
55
        }
56
57 26
        list($class, $method) = $callable;
58
59
        try {
60 26
            return new \ReflectionMethod($class, $method);
61 2
        } catch (\ReflectionException $e) {
62 2
            throw NotCallableException::fromInvalidCallable($callable);
63
        }
64
    }
65
}
66