GetFromQueryParamTag   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 80
Duplicated Lines 100 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __invoke() 33 33 9
B getQueryParametersFromDocBlock() 41 41 9

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Mpociot\ApiDoc\Extracting\Strategies\QueryParameters;
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 GetFromQueryParamTag 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 @queryParam 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->getQueryParametersFromDocBlock($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->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 $tag) {
62
                // Format:
63
                // @queryParam <name> <"required" (optional)> <description>
64
                // Examples:
65
                // @queryParam text string required The text.
66
                // @queryParam 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