GetFromBodyParamTag   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

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

2 Methods

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