Completed
Pull Request — master (#690)
by
unknown
02:14 queued 52s
created

RouteDocBlocker::getDocBlocksFromRoute()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 9.1928
c 0
b 0
f 0
cc 5
nc 3
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
10
/**
11
 * Class RouteDocBlocker
12
 * Utility class to help with retrieving doc blocks from route classes and methods.
13
 * Also caches them so repeated access is faster.
14
 */
15
class RouteDocBlocker
16
{
17
    protected static $docBlocks = [];
18
19
    /**
20
     * @param Route $route
21
     *
22
     * @throws \ReflectionException
23
     * @throws \Exception
24
     *
25
     * @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...
26
     */
27
    public static function getDocBlocksFromRoute(Route $route): array
28
    {
29
        list($className, $methodName) = Utils::getRouteClassAndMethodNames($route);
30
        $normalizedClassName = static::normalizeClassName($className);
31
        $docBlocks = self::getCachedDocBlock($route, $normalizedClassName, $methodName);
32
33
        if ($docBlocks) {
34
            return $docBlocks;
35
        }
36
37
        $class = new ReflectionClass($className);
38
39
        if (! $class->hasMethod($methodName)) {
40
            throw new \Exception("Error while fetching docblock for route: Class $className does not contain method $methodName");
41
        }
42
43
        $method = Utils::reflectRouteMethod([$className, $methodName]);
44
45
        $docBlocks = [
46
            'method' => new DocBlock($method->getDocComment() ?: ''),
47
            'class' => new DocBlock($class->getDocComment() ?: ''),
48
        ];
49
        self::cacheDocBlocks($route, $normalizedClassName, $methodName, $docBlocks);
50
51
        return $docBlocks;
52
    }
53
54
    /**
55
     * @param string|object $classNameOrInstance
56
     *
57
     * @return string
58
     */
59
    protected static function normalizeClassName($classNameOrInstance): string
60
    {
61
        if (is_object($classNameOrInstance)) {
62
            // route handlers are not destroyed until the script
63
            // ends so this should be perfectly safe.
64
            $classNameOrInstance = get_class($classNameOrInstance).'::'.spl_object_id($classNameOrInstance);
65
        }
66
67
        return $classNameOrInstance;
68
    }
69
70
    protected static function getCachedDocBlock(Route $route, string $className, string $methodName)
71
    {
72
        $routeId = self::getRouteCacheId($route, $className, $methodName);
73
74
        return self::$docBlocks[$routeId] ?? null;
75
    }
76
77
    protected static function cacheDocBlocks(Route $route, string $className, string $methodName, array $docBlocks)
78
    {
79
        $routeId = self::getRouteCacheId($route, $className, $methodName);
80
        self::$docBlocks[$routeId] = $docBlocks;
81
    }
82
83
    private static function getRouteCacheId(Route $route, string $className, string $methodName): string
84
    {
85
        return $route->uri()
86
            .':'
87
            .implode(array_diff($route->methods(), ['HEAD']))
88
            .$className
89
            .$methodName;
90
    }
91
}
92