Completed
Pull Request — master (#690)
by
unknown
01:34
created

RouteDocBlocker::getDocBlocksFromRoute()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 8.8497
c 0
b 0
f 0
cc 6
nc 4
nop 1
1
<?php
2
3
namespace Mpociot\ApiDoc\Extracting;
4
5
use Illuminate\Routing\Route;
6
use Mpociot\ApiDoc\Tools\Utils;
7
use Mpociot\Reflection\DocBlock;
8
use ReflectionClass;
9
use ReflectionFunction;
10
11
/**
12
 * Class RouteDocBlocker
13
 * Utility class to help with retrieving doc blocks from route classes and methods.
14
 * Also caches them so repeated access is faster.
15
 */
16
class RouteDocBlocker
17
{
18
    protected static $docBlocks = [];
19
20
    /**
21
     * @param Route $route
22
     *
23
     * @throws \ReflectionException
24
     * @throws \Exception
25
     *
26
     * @return array<string, DocBlock> Method and class docblocks
0 ignored issues
show
Documentation introduced by
The doc-type array<string, could not be parsed: Expected ">" at position 5, but found "end of type". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
27
     */
28
    public static function getDocBlocksFromRoute(Route $route): array
29
    {
30
        list($className, $methodName) = Utils::getRouteClassAndMethodNames($route);
31
        $normalizedClassName = static::normalizeClassName($className);
32
        $docBlocks = self::getCachedDocBlock($route, $normalizedClassName, $methodName);
33
34
        if ($docBlocks) {
35
            return $docBlocks;
36
        }
37
38
        $class = new ReflectionClass($className);
39
40
        if (! $class->hasMethod($methodName)) {
41
            throw new \Exception("Error while fetching docblock for route: Class $className does not contain method $methodName");
42
        }
43
44
        $method = $className instanceof \Closure
45
            ? new ReflectionFunction($className)
46
            : $class->getMethod($methodName);
47
48
        $docBlocks = [
49
            'method' => new DocBlock($method->getDocComment() ?: ''),
50
            'class' => new DocBlock($class->getDocComment() ?: ''),
51
        ];
52
        self::cacheDocBlocks($route, $normalizedClassName, $methodName, $docBlocks);
53
54
        return $docBlocks;
55
    }
56
57
    /**
58
     * @param string|object $classNameOrInstance
59
     *
60
     * @return string
61
     */
62
    protected static function normalizeClassName($classNameOrInstance): string
63
    {
64
        if (is_object($classNameOrInstance)) {
65
            // route handlers are not destroyed until the script
66
            // ends so this should be perfectly safe.
67
            $classNameOrInstance = get_class($classNameOrInstance).'::'.spl_object_id($classNameOrInstance);
68
        }
69
        return $classNameOrInstance;
70
    }
71
72
    protected static function getCachedDocBlock(Route $route, string $className, string $methodName)
73
    {
74
        $routeId = self::getRouteCacheId($route, $className, $methodName);
75
76
        return self::$docBlocks[$routeId] ?? null;
77
    }
78
79
    protected static function cacheDocBlocks(Route $route, string $className, string $methodName, array $docBlocks)
80
    {
81
        $routeId = self::getRouteCacheId($route, $className, $methodName);
82
        self::$docBlocks[$routeId] = $docBlocks;
83
    }
84
85
    private static function getRouteCacheId(Route $route, string $className, string $methodName): string
86
    {
87
        return $route->uri()
88
            .':'
89
            .implode(array_diff($route->methods(), ['HEAD']))
90
            .$className
91
            .$methodName;
92
    }
93
}
94