Completed
Push — master ( 0ed20a...e8c824 )
by
unknown
10:05 queued 08:45
created

RouteDocBlocker::getCachedDocBlock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
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
     *
24
     * @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...
25
     */
26
    public static function getDocBlocksFromRoute(Route $route): array
27
    {
28
        list($className, $methodName) = Utils::getRouteClassAndMethodNames($route);
29
        $docBlocks = self::getCachedDocBlock($route, $className, $methodName);
30
        if ($docBlocks) {
31
            return $docBlocks;
32
        }
33
34
        $class = new ReflectionClass($className);
35
36
        if (! $class->hasMethod($methodName)) {
37
            throw new \Exception("Error while fetching docblock for route: Class $className does not contain method $methodName");
38
        }
39
40
        $docBlocks = [
41
            'method' => new DocBlock($class->getMethod($methodName)->getDocComment() ?: ''),
42
            'class' => new DocBlock($class->getDocComment() ?: ''),
43
        ];
44
        self::cacheDocBlocks($route, $className, $methodName, $docBlocks);
45
46
        return $docBlocks;
47
    }
48
49
    protected static function getCachedDocBlock(Route $route, string $className, string $methodName)
50
    {
51
        $routeId = self::getRouteCacheId($route, $className, $methodName);
52
53
        return self::$docBlocks[$routeId] ?? null;
54
    }
55
56
    protected static function cacheDocBlocks(Route $route, string $className, string $methodName, array $docBlocks)
57
    {
58
        $routeId = self::getRouteCacheId($route, $className, $methodName);
59
        self::$docBlocks[$routeId] = $docBlocks;
60
    }
61
62
    private static function getRouteCacheId(Route $route, string $className, string $methodName): string
63
    {
64
        return $route->uri()
65
            .':'
66
            .implode(array_diff($route->methods(), ['HEAD']))
67
            .$className
68
            .$methodName;
69
    }
70
}
71