GetFromUrlParamTag::getUrlParametersFromDocBlock()   B
last analyzed

Complexity

Conditions 9
Paths 1

Size

Total Lines 41

Duplication

Lines 41
Ratio 100 %

Importance

Changes 0
Metric Value
dl 41
loc 41
rs 7.7084
c 0
b 0
f 0
cc 9
nc 1
nop 1
1
<?php
2
3
namespace Mpociot\ApiDoc\Extracting\Strategies\UrlParameters;
4
5
use Dingo\Api\Http\FormRequest as DingoFormRequest;
6
use Illuminate\Foundation\Http\FormRequest as LaravelFormRequest;
7
use Illuminate\Routing\Route;
8
use Illuminate\Support\Str;
9
use Mpociot\ApiDoc\Extracting\ParamHelpers;
10
use Mpociot\ApiDoc\Extracting\RouteDocBlocker;
11
use Mpociot\ApiDoc\Extracting\Strategies\Strategy;
12
use Mpociot\Reflection\DocBlock;
13
use Mpociot\Reflection\DocBlock\Tag;
14
use ReflectionClass;
15
use ReflectionMethod;
16
17 View Code Duplication
class GetFromUrlParamTag extends Strategy
0 ignored issues
show
Duplication introduced by
This class 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...
18
{
19
    use ParamHelpers;
20
21
    public function __invoke(Route $route, ReflectionClass $controller, ReflectionMethod $method, array $routeRules, array $context = [])
22
    {
23
        foreach ($method->getParameters() as $param) {
24
            $paramType = $param->getType();
25
            if ($paramType === null) {
26
                continue;
27
            }
28
29
            $parameterClassName = $paramType->getName();
30
31
            try {
32
                $parameterClass = new ReflectionClass($parameterClassName);
33
            } catch (\ReflectionException $e) {
34
                continue;
35
            }
36
37
            // If there's a FormRequest, we check there for @urlParam tags.
38
            if (class_exists(LaravelFormRequest::class) && $parameterClass->isSubclassOf(LaravelFormRequest::class)
39
                || class_exists(DingoFormRequest::class) && $parameterClass->isSubclassOf(DingoFormRequest::class)) {
40
                $formRequestDocBlock = new DocBlock($parameterClass->getDocComment());
41
                $queryParametersFromDocBlock = $this->getUrlParametersFromDocBlock($formRequestDocBlock->getTags());
42
43
                if (count($queryParametersFromDocBlock)) {
44
                    return $queryParametersFromDocBlock;
45
                }
46
            }
47
        }
48
49
        /** @var DocBlock $methodDocBlock */
50
        $methodDocBlock = RouteDocBlocker::getDocBlocksFromRoute($route)['method'];
51
52
        return $this->getUrlParametersFromDocBlock($methodDocBlock->getTags());
53
    }
54
55
    private function getUrlParametersFromDocBlock($tags)
56
    {
57
        $parameters = collect($tags)
58
            ->filter(function ($tag) {
59
                return $tag instanceof Tag && $tag->getName() === 'urlParam';
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 $tag) {
62
                // Format:
63
                // @urlParam <name> <"required" (optional)> <description>
64
                // Examples:
65
                // @urlParam id string required The id of the post.
66
                // @urlParam user_id The ID of the user.
67
                preg_match('/(.+?)\s+(required\s+)?(.*)/', $tag->getContent(), $content);
68
                $content = preg_replace('/\s?No-example.?/', '', $content);
69
                if (empty($content)) {
70
                    // this means only name was supplied
71
                    list($name) = preg_split('/\s+/', $tag->getContent());
72
                    $required = false;
73
                    $description = '';
74
                } else {
75
                    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...
76
                    $description = trim($description);
77
                    if ($description == 'required' && empty(trim($required))) {
78
                        $required = $description;
79
                        $description = '';
80
                    }
81
                    $required = trim($required) == 'required' ? true : false;
82
                }
83
84
                list($description, $value) = $this->parseParamDescription($description, 'string');
85
                if (is_null($value) && ! $this->shouldExcludeExample($tag->getContent())) {
86
                    $value = Str::contains($description, ['number', 'count', 'page'])
87
                        ? $this->generateDummyValue('integer')
88
                        : $this->generateDummyValue('string');
89
                }
90
91
                return [$name => compact('description', 'required', 'value')];
92
            })->toArray();
93
94
        return $parameters;
95
    }
96
}
97