Completed
Pull Request — master (#570)
by Marcel
01:55
created

GetFromDocBlocks   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 3
dl 0
loc 88
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 16 2
A getAuthStatusFromDocBlock() 0 9 2
B getRouteGroupDescriptionAndTitle() 0 47 7
1
<?php
2
3
namespace Mpociot\ApiDoc\Strategies\Metadata;
4
5
use ReflectionClass;
6
use ReflectionMethod;
7
use Illuminate\Routing\Route;
8
use Mpociot\Reflection\DocBlock;
9
use Mpociot\Reflection\DocBlock\Tag;
10
use Mpociot\ApiDoc\Strategies\Strategy;
11
use Mpociot\ApiDoc\Tools\RouteDocBlocker;
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
21
        list($routeGroupName, $routeGroupDescription, $routeTitle) = $this->getRouteGroupDescriptionAndTitle($methodDocBlock, $docBlocks['class']);
22
23
        return [
24
                'groupName' => $routeGroupName,
25
                'groupDescription' => $routeGroupDescription,
26
                'title' => $routeTitle ?: $methodDocBlock->getShortDescription(),
27
                'description' => $methodDocBlock->getLongDescription()->getContents(),
28
                'authenticated' => $this->getAuthStatusFromDocBlock($methodDocBlock->getTags()),
29
        ];
30
    }
31
32
    /**
33
     * @param array $tags Tags in the method doc block
34
     *
35
     * @return bool
36
     */
37
    protected function getAuthStatusFromDocBlock(array $tags)
38
    {
39
        $authTag = collect($tags)
40
            ->first(function ($tag) {
41
                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...
42
            });
43
44
        return (bool) $authTag;
45
    }
46
47
    /**
48
     * @param DocBlock $methodDocBlock
49
     * @param DocBlock $controllerDocBlock
50
     *
51
     * @return array The route group name, the group description, ad the route title
52
     */
53
    protected function getRouteGroupDescriptionAndTitle(DocBlock $methodDocBlock, DocBlock $controllerDocBlock)
54
    {
55
        // @group tag on the method overrides that on the controller
56
        if (! empty($methodDocBlock->getTags())) {
57
            foreach ($methodDocBlock->getTags() as $tag) {
58
                if ($tag->getName() === 'group') {
59
                    $routeGroupParts = explode("\n", trim($tag->getContent()));
60
                    $routeGroupName = array_shift($routeGroupParts);
61
                    $routeGroupDescription = trim(implode("\n", $routeGroupParts));
62
63
                    // If the route has no title (the methodDocBlock's "short description"),
64
                    // we'll assume the routeGroupDescription is actually the title
65
                    // Something like this:
66
                    // /**
67
                    //   * Fetch cars. <-- This is route title.
68
                    //   * @group Cars <-- This is group name.
69
                    //   * APIs for cars. <-- This is group description (not required).
70
                    //   **/
71
                    // VS
72
                    // /**
73
                    //   * @group Cars <-- This is group name.
74
                    //   * Fetch cars. <-- This is route title, NOT group description.
75
                    //   **/
76
77
                    // BTW, this is a spaghetti way of doing this.
78
                    // It shall be refactored soon. Deus vult!💪
79
                    if (empty($methodDocBlock->getShortDescription())) {
80
                        return [$routeGroupName, '', $routeGroupDescription];
81
                    }
82
83
                    return [$routeGroupName, $routeGroupDescription, $methodDocBlock->getShortDescription()];
84
                }
85
            }
86
        }
87
88
        foreach ($controllerDocBlock->getTags() as $tag) {
89
            if ($tag->getName() === 'group') {
90
                $routeGroupParts = explode("\n", trim($tag->getContent()));
91
                $routeGroupName = array_shift($routeGroupParts);
92
                $routeGroupDescription = implode("\n", $routeGroupParts);
93
94
                return [$routeGroupName, $routeGroupDescription, $methodDocBlock->getShortDescription()];
95
            }
96
        }
97
98
        return [$this->config->get('default_group'), '', $methodDocBlock->getShortDescription()];
99
    }
100
}
101