Completed
Pull Request — master (#743)
by Guy
01:26
created

FromDocBlockHelper   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 1
dl 0
loc 37
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A getResponseParametersFromDocBlock() 0 32 5
1
<?php
2
3
namespace Mpociot\ApiDoc\Extracting\Strategies\ResponseParameters;
4
5
use Mpociot\ApiDoc\Extracting\ParamHelpers;
6
use Mpociot\Reflection\DocBlock\Tag;
7
8
trait FromDocBlockHelper
9
{
10
    use ParamHelpers;
11
12
    private function getResponseParametersFromDocBlock($tags)
13
    {
14
        $parameters = collect($tags)
15
            ->filter(function ($tag) {
16
                return $tag instanceof Tag && $tag->getName() === 'responseParam';
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...
17
            })
18
            ->mapWithKeys(function (Tag $tag) {
19
                // Format:
20
                // @responseParam <name> <type> <description>
21
                // Examples:
22
                // @responseParam user_id integer The ID of the user.
23
                preg_match('/(.+?)\s+(.+?)\s+(.*)/', $tag->getContent(), $content);
24
                $content = preg_replace('/\s?No-example.?/', '', $content);
25
                if (empty($content)) {
26
                    // this means only name and type were supplied
27
                    list($name, $type) = preg_split('/\s+/', $tag->getContent());
28
                } else {
29
                    list($_, $name, $type, $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...
30
                    $description = trim($description);
31
                }
32
33
                $type = $this->normalizeParameterType($type);
34
                list($description, $example) = $this->parseParamDescription($description, $type);
0 ignored issues
show
Bug introduced by
The variable $description does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
35
                $value = is_null($example) && ! $this->shouldExcludeExample($tag->getContent())
36
                    ? $this->generateDummyValue($type)
37
                    : $example;
38
39
                return [$name => compact('type', 'description', 'value', 'example')];
40
            })->toArray();
41
42
        return $parameters;
43
    }
44
}
45