Completed
Pull Request — master (#570)
by
unknown
01:17
created

GetFromDocBlocks::__invoke()   B

Complexity

Conditions 10
Paths 10

Size

Total Lines 34

Duplication

Lines 34
Ratio 100 %

Importance

Changes 0
Metric Value
dl 34
loc 34
rs 7.6666
c 0
b 0
f 0
cc 10
nc 10
nop 5

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Mpociot\ApiDoc\Strategies\QueryParameters;
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
use Mpociot\ApiDoc\Tools\Traits\ParamHelpers;
13
use Dingo\Api\Http\FormRequest as DingoFormRequest;
14
use Illuminate\Foundation\Http\FormRequest as LaravelFormRequest;
15
16
class GetFromDocBlocks extends Strategy
17
{
18
    use ParamHelpers;
19
20 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...
21
    {
22
        foreach ($method->getParameters() as $param) {
23
            $paramType = $param->getType();
24
            if ($paramType === null) {
25
                continue;
26
            }
27
28
            $parameterClassName = version_compare(phpversion(), '7.1.0', '<')
29
                ? $paramType->__toString()
30
                : $paramType->getName();
31
32
            try {
33
                $parameterClass = new ReflectionClass($parameterClassName);
34
            } catch (\ReflectionException $e) {
35
                continue;
36
            }
37
38
            // If there's a FormRequest, we check there for @queryParam tags.
39
            if (class_exists(LaravelFormRequest::class) && $parameterClass->isSubclassOf(LaravelFormRequest::class)
40
                || class_exists(DingoFormRequest::class) && $parameterClass->isSubclassOf(DingoFormRequest::class)) {
41
                $formRequestDocBlock = new DocBlock($parameterClass->getDocComment());
42
                $queryParametersFromDocBlock = $this->getqueryParametersFromDocBlock($formRequestDocBlock->getTags());
43
44
                if (count($queryParametersFromDocBlock)) {
45
                    return $queryParametersFromDocBlock;
46
                }
47
            }
48
        }
49
50
        $methodDocBlock = RouteDocBlocker::getDocBlocksFromRoute($route)['method'];
51
52
        return $this->getqueryParametersFromDocBlock($methodDocBlock->getTags());
53
    }
54
55
    private function getQueryParametersFromDocBlock($tags)
56
    {
57
        $parameters = collect($tags)
58
            ->filter(function ($tag) {
59
                return $tag instanceof Tag && $tag->getName() === 'queryParam';
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...
60
            })
61
            ->mapWithKeys(function ($tag) {
62
                preg_match('/(.+?)\s+(required\s+)?(.*)/', $tag->getContent(), $content);
63
                $content = preg_replace('/\s?No-example.?/', '', $content);
64 View Code Duplication
                if (empty($content)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
65
                    // this means only name was supplied
66
                    list($name) = preg_split('/\s+/', $tag->getContent());
67
                    $required = false;
68
                    $description = '';
69
                } else {
70
                    list($_, $name, $required, $description) = $content;
0 ignored issues
show
Unused Code introduced by
The assignment to $_ is unused. Consider omitting it like so list($first,,$third).

This checks looks for assignemnts to variables using the list(...) function, where not all assigned variables are subsequently used.

Consider the following code example.

<?php

function returnThreeValues() {
    return array('a', 'b', 'c');
}

list($a, $b, $c) = returnThreeValues();

print $a . " - " . $c;

Only the variables $a and $c are used. There was no need to assign $b.

Instead, the list call could have been.

list($a,, $c) = returnThreeValues();
Loading history...
71
                    $description = trim($description);
72
                    if ($description == 'required' && empty(trim($required))) {
73
                        $required = $description;
74
                        $description = '';
75
                    }
76
                    $required = trim($required) == 'required' ? true : false;
77
                }
78
79
                list($description, $value) = $this->parseParamDescription($description, 'string');
80
                if (is_null($value) && ! $this->shouldExcludeExample($tag)) {
81
                    $value = str_contains($description, ['number', 'count', 'page'])
82
                        ? $this->generateDummyValue('integer')
83
                        : $this->generateDummyValue('string');
84
                }
85
86
                return [$name => compact('description', 'required', 'value')];
87
            })->toArray();
88
89
        return $parameters;
90
    }
91
}
92