getRouteGroupDescriptionAndTitle()   B
last analyzed

Complexity

Conditions 7
Paths 9

Size

Total Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 47
rs 8.223
c 0
b 0
f 0
cc 7
nc 9
nop 2
1
<?php
2
3
namespace Mpociot\ApiDoc\Extracting\Strategies\Metadata;
4
5
use Illuminate\Routing\Route;
6
use Mpociot\ApiDoc\Extracting\RouteDocBlocker;
7
use Mpociot\ApiDoc\Extracting\Strategies\Strategy;
8
use Mpociot\Reflection\DocBlock;
9
use Mpociot\Reflection\DocBlock\Tag;
10
use ReflectionClass;
11
use ReflectionMethod;
12
13
class GetFromDocBlocks extends Strategy
14
{
15
    public function __invoke(Route $route, ReflectionClass $controller, ReflectionMethod $method, array $routeRules, array $context = [])
16
    {
17
        $docBlocks = RouteDocBlocker::getDocBlocksFromRoute($route);
18
        /** @var DocBlock $methodDocBlock */
19
        $methodDocBlock = $docBlocks['method'];
20
        $classDocBlock = $docBlocks['class'];
21
22
        list($routeGroupName, $routeGroupDescription, $routeTitle) = $this->getRouteGroupDescriptionAndTitle($methodDocBlock, $classDocBlock);
23
24
        return [
25
            'groupName' => $routeGroupName,
26
            'groupDescription' => $routeGroupDescription,
27
            'title' => $routeTitle ?: $methodDocBlock->getShortDescription(),
28
            'description' => $methodDocBlock->getLongDescription()->getContents(),
29
            'authenticated' => $this->getAuthStatusFromDocBlock($classDocBlock->getTags())?:$this->getAuthStatusFromDocBlock($methodDocBlock->getTags()),
30
        ];
31
    }
32
33
    /**
34
     * @param array $tags Tags in the method doc block
35
     *
36
     * @return bool
37
     */
38
    protected function getAuthStatusFromDocBlock(array $tags)
39
    {
40
        $authTag = collect($tags)
41
            ->first(function ($tag) {
42
                return $tag instanceof Tag && strtolower($tag->getName()) === 'authenticated';
0 ignored issues
show
Bug introduced by
The class Mpociot\Reflection\DocBlock\Tag does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
43
            });
44
45
        return (bool) $authTag;
46
    }
47
48
    /**
49
     * @param DocBlock $methodDocBlock
50
     * @param DocBlock $controllerDocBlock
51
     *
52
     * @return array The route group name, the group description, and the route title
53
     */
54
    protected function getRouteGroupDescriptionAndTitle(DocBlock $methodDocBlock, DocBlock $controllerDocBlock)
55
    {
56
        // @group tag on the method overrides that on the controller
57
        if (! empty($methodDocBlock->getTags())) {
58
            foreach ($methodDocBlock->getTags() as $tag) {
59
                if ($tag->getName() === 'group') {
60
                    $routeGroupParts = explode("\n", trim($tag->getContent()));
61
                    $routeGroupName = array_shift($routeGroupParts);
62
                    $routeGroupDescription = trim(implode("\n", $routeGroupParts));
63
64
                    // If the route has no title (the methodDocBlock's "short description"),
65
                    // we'll assume the routeGroupDescription is actually the title
66
                    // Something like this:
67
                    // /**
68
                    //   * Fetch cars. <-- This is route title.
69
                    //   * @group Cars <-- This is group name.
70
                    //   * APIs for cars. <-- This is group description (not required).
71
                    //   **/
72
                    // VS
73
                    // /**
74
                    //   * @group Cars <-- This is group name.
75
                    //   * Fetch cars. <-- This is route title, NOT group description.
76
                    //   **/
77
78
                    // BTW, this is a spaghetti way of doing this.
79
                    // It shall be refactored soon. Deus vult!💪
80
                    if (empty($methodDocBlock->getShortDescription())) {
81
                        return [$routeGroupName, '', $routeGroupDescription];
82
                    }
83
84
                    return [$routeGroupName, $routeGroupDescription, $methodDocBlock->getShortDescription()];
85
                }
86
            }
87
        }
88
89
        foreach ($controllerDocBlock->getTags() as $tag) {
90
            if ($tag->getName() === 'group') {
91
                $routeGroupParts = explode("\n", trim($tag->getContent()));
92
                $routeGroupName = array_shift($routeGroupParts);
93
                $routeGroupDescription = implode("\n", $routeGroupParts);
94
95
                return [$routeGroupName, $routeGroupDescription, $methodDocBlock->getShortDescription()];
96
            }
97
        }
98
99
        return [$this->config->get('default_group'), '', $methodDocBlock->getShortDescription()];
100
    }
101
}
102