UseResponseTag::getDocBlockResponses()   A
last analyzed

Complexity

Conditions 5
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.2408
c 0
b 0
f 0
cc 5
nc 2
nop 1
1
<?php
2
3
namespace Mpociot\ApiDoc\Extracting\Strategies\Responses;
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
11
/**
12
 * Get a response from the docblock ( @response ).
13
 */
14
class UseResponseTag extends Strategy
15
{
16
    /**
17
     * @param Route $route
18
     * @param \ReflectionClass $controller
19
     * @param \ReflectionMethod $method
20
     * @param array $routeRules
21
     * @param array $context
22
     *
23
     * @throws \Exception
24
     *
25
     * @return array|null
26
     */
27 View Code Duplication
    public function __invoke(Route $route, \ReflectionClass $controller, \ReflectionMethod $method, array $routeRules, array $context = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
28
    {
29
        $docBlocks = RouteDocBlocker::getDocBlocksFromRoute($route);
30
        /** @var DocBlock $methodDocBlock */
31
        $methodDocBlock = $docBlocks['method'];
32
33
        return $this->getDocBlockResponses($methodDocBlock->getTags());
34
    }
35
36
    /**
37
     * Get the response from the docblock if available.
38
     *
39
     * @param array $tags
40
     *
41
     * @return array|null
42
     */
43
    protected function getDocBlockResponses(array $tags)
44
    {
45
        $responseTags = array_values(
46
            array_filter($tags, function ($tag) {
47
                return $tag instanceof Tag && strtolower($tag->getName()) === 'response';
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...
48
            })
49
        );
50
51
        if (empty($responseTags)) {
52
            return null;
53
        }
54
55
        $responses = array_map(function (Tag $responseTag) {
56
            preg_match('/^(\d{3})?\s?([\s\S]*)$/', $responseTag->getContent(), $result);
57
58
            $status = $result[1] ?: 200;
59
            $content = $result[2] ?: '{}';
60
61
            return ['content' => $content, 'status' => (int) $status];
62
        }, $responseTags);
63
64
        return $responses;
65
    }
66
}
67