Completed
Pull Request — master (#410)
by
unknown
02:10
created

ResponseFileStrategy::getFileResponse()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14

Duplication

Lines 14
Ratio 100 %

Importance

Changes 0
Metric Value
dl 14
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 2
nop 1
1
<?php
2
3
namespace Mpociot\ApiDoc\Tools\ResponseStrategies;
4
5
use Illuminate\Routing\Route;
6
use Illuminate\Support\Facades\Storage;
7
use Mpociot\Reflection\DocBlock\Tag;
8
9
/**
10
 * Get a response from from a file in the docblock ( @responseFile ).
11
 */
12 View Code Duplication
class ResponseFileStrategy
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...
13
{
14
    public function __invoke(Route $route, array $tags, array $routeProps)
15
    {
16
        return $this->getFileResponse($tags);
17
    }
18
19
    /**
20
     * Get the response from the file if available.
21
     *
22
     * @param array $tags
23
     *
24
     * @return mixed
25
     */
26
    protected function getFileResponse(array $tags)
27
    {
28
        $responseFileTags = array_filter($tags, function ($tag) {
29
            return $tag instanceof Tag && strtolower($tag->getName()) == 'responsefile';
30
        });
31
        if (empty($responseFileTags)) {
32
            return;
33
        }
34
        $responseFileTag = array_first($responseFileTags);
35
36
        $json = json_decode(Storage::get($responseFileTag->getContent()), true);
37
38
        return response()->json($json);
0 ignored issues
show
Bug introduced by
The method json does only exist in Illuminate\Contracts\Routing\ResponseFactory, but not in Illuminate\Http\Response.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
39
    }
40
}
41