GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — 3.x (#2763)
by
unknown
01:25
created

CallableResolver::parseCallable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
1
<?php
2
/**
3
 * Slim Framework (https://slimframework.com)
4
 *
5
 * @license https://github.com/slimphp/Slim/blob/3.x/LICENSE.md (MIT License)
6
 */
7
8
namespace Slim;
9
10
use Psr\Container\ContainerInterface;
11
use RuntimeException;
12
use Slim\Interfaces\CallableResolverInterface;
13
14
/**
15
 * This class resolves a string of the format 'class:method' into a closure
16
 * that can be dispatched.
17
 */
18
final class CallableResolver implements CallableResolverInterface
19
{
20
    const CALLABLE_PATTERN = '!^([^\:]+)\:([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)$!';
21
22
    /**
23
     * @var ContainerInterface
24
     */
25
    private $container;
26
27
    /**
28
     * @param ContainerInterface $container
29
     */
30
    public function __construct(ContainerInterface $container)
31
    {
32
        $this->container = $container;
33
    }
34
35
    /**
36
     * Resolve toResolve into a closure so that the router can dispatch.
37
     *
38
     * If toResolve is of the format 'class:method', then try to extract 'class'
39
     * from the container otherwise instantiate it and then dispatch 'method'.
40
     *
41
     * @param callable|string $toResolve
42
     *
43
     * @return callable
44
     *
45
     * @throws RuntimeException If the callable does not exist
46
     * @throws RuntimeException If the callable is not resolvable
47
     */
48
    public function resolve($toResolve)
49
    {
50
        if (is_callable($toResolve)) {
51
            return $toResolve;
52
        }
53
54
        if (!is_string($toResolve)) {
55
            $this->assertCallable($toResolve);
56
        }
57
58
        list($class, $method) = $this->parseCallable($toResolve);
59
        $resolved = $this->resolveCallable($class, $method);
60
        $this->assertCallable($resolved);
61
        return $resolved;
62
    }
63
64
    /**
65
     * Extract class and method from toResolve
66
     *
67
     * @param string $toResolve
68
     *
69
     * @return array
70
     */
71
    protected function parseCallable($toResolve)
72
    {
73
        if (preg_match(self::CALLABLE_PATTERN, $toResolve, $matches)) {
74
            return [$matches[1], $matches[2]];
75
        }
76
        return [$toResolve, '__invoke'];
77
    }
78
79
    /**
80
     * Check if string is something in the DIC
81
     * that's callable or is a class name which has an __invoke() method.
82
     *
83
     * @param string $class
84
     * @param string $method
85
     *
86
     * @return callable
87
     *
88
     * @throws RuntimeException if the callable does not exist
89
     */
90
    protected function resolveCallable($class, $method)
91
    {
92
        if ($this->container->has($class)) {
93
            return [$this->container->get($class), $method];
94
        }
95
96
        if (!class_exists($class)) {
97
            throw new RuntimeException(sprintf('Callable %s does not exist', $class));
98
        }
99
100
        return [new $class($this->container), $method];
101
    }
102
103
    /**
104
     * @param Callable $callable
105
     *
106
     * @throws RuntimeException if the callable is not resolvable
107
     */
108
    protected function assertCallable($callable)
109
    {
110
        if (!is_callable($callable)) {
111
            throw new RuntimeException(sprintf(
112
                '%s is not resolvable',
113
                is_array($callable) || is_object($callable) ? json_encode($callable) : $callable
114
            ));
115
        }
116
    }
117
}
118