RequestHandlerReflector   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 13
c 3
b 0
f 0
dl 0
loc 37
ccs 11
cts 11
cp 1
rs 10
wmc 8

1 Method

Rating   Name   Duplication   Size   Complexity  
B reflectRequestHandler() 0 27 8
1
<?php
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sunrise\Http\Router;
15
16
use InvalidArgumentException;
17
use Psr\Http\Server\RequestHandlerInterface;
18
use ReflectionClass;
19
use ReflectionException;
20
use ReflectionMethod;
21
22
use function is_array;
23
use function is_callable;
24
use function is_string;
25
use function is_subclass_of;
26
use function sprintf;
27
28
use const PHP_VERSION_ID;
29
30
/**
31
 * @since 3.0.0
32
 */
33
final class RequestHandlerReflector implements RequestHandlerReflectorInterface
34
{
35
    /**
36
     * @inheritDoc
37
     *
38
     * @return ReflectionClass<RequestHandlerInterface>|ReflectionMethod
39
     *
40
     * @throws InvalidArgumentException
41
     * @throws ReflectionException
42
     */
43 10
    public function reflectRequestHandler(mixed $reference): ReflectionClass|ReflectionMethod
44
    {
45 10
        if ($reference instanceof RequestHandlerInterface) {
46 1
            return new ReflectionClass($reference);
47
        }
48
49 9
        if (is_string($reference) && is_subclass_of($reference, RequestHandlerInterface::class)) {
50 4
            return new ReflectionClass($reference);
51
        }
52
53
        // https://github.com/php/php-src/blob/3ed526441400060aa4e618b91b3352371fcd02a8/Zend/zend_API.c#L3884-L3932
54 8
        if (is_array($reference) && is_callable($reference, true, $referenceName)) {
55
            try {
56
                // @codeCoverageIgnoreStart
57
                if (PHP_VERSION_ID < 80300) {
58
                    return new ReflectionMethod($referenceName);
0 ignored issues
show
Bug Best Practice introduced by
The expression return new ReflectionMethod($referenceName) returns the type ReflectionMethod which is incompatible with the documented return type ReflectionClass.
Loading history...
59
                } // @codeCoverageIgnoreEnd
60
61
                /** @psalm-var ReflectionMethod */
62 7
                return ReflectionMethod::createFromMethodName($referenceName);
0 ignored issues
show
Bug introduced by
The method createFromMethodName() does not exist on ReflectionMethod. ( Ignorable by Annotation )

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

62
                return ReflectionMethod::/** @scrutinizer ignore-call */ createFromMethodName($referenceName);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
63 2
            } catch (ReflectionException) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
64
            }
65
        }
66
67 3
        throw new InvalidArgumentException(sprintf(
68 3
            'The request handler reference "%s" could not be reflected.',
69 3
            ReferenceResolver::stringifyReference($reference),
70 3
        ));
71
    }
72
}
73