ParamFetcher::get()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 8
cts 8
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 2
crap 4
1
<?php
2
3
namespace Nicofuma\SwaggerBundle\FosRest;
4
5
use FOS\RestBundle\Request\ParamFetcherInterface;
6
use Nicofuma\SwaggerBundle\Exception\NoValidatorException;
7
use Nicofuma\SwaggerBundle\Validator\Validator;
8
use Nicofuma\SwaggerBundle\Validator\ValidatorMap;
9
use Symfony\Component\HttpFoundation\RequestStack;
10
11
class ParamFetcher implements ParamFetcherInterface
12
{
13
    /** @var ParamFetcherInterface */
14
    private $decorated;
15
16
    /** @var ValidatorMap */
17
    private $validatorMap;
18
19
    /** @var RequestStack */
20
    private $requestStack;
21
22
    /** @var Validator */
23
    private $currentValidator;
24
25
    /** @var \stdClass[] */
26
    private $currentSchema;
27
28
    private $usingSwagger = false;
29
30 6
    public function __construct(ParamFetcherInterface $decorated, ValidatorMap $validatorMap, RequestStack $requestStack)
31
    {
32 6
        $this->decorated = $decorated;
33 6
        $this->validatorMap = $validatorMap;
34 6
        $this->requestStack = $requestStack;
35 6
    }
36
37
    /**
38
     * {@inheritdoc}
39
     */
40 6
    public function setController($controller)
41
    {
42 6
        $request = $this->requestStack->getCurrentRequest();
43
44
        // Workaround for https://github.com/symfony/symfony/issues/19749
45 6
        if ($this->requestStack->getMasterRequest() !== $request) {
46
            $this->usingSwagger = false;
47
            $this->decorated->setController($controller);
48
49
            return;
50
        }
51
52
        try {
53 6
            $this->currentValidator = $this->validatorMap->getValidator($request);
0 ignored issues
show
Bug introduced by
It seems like $request defined by $this->requestStack->getCurrentRequest() on line 42 can be null; however, Nicofuma\SwaggerBundle\V...atorMap::getValidator() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
54 5
            $this->currentSchema = null;
0 ignored issues
show
Documentation Bug introduced by
It seems like null of type null is incompatible with the declared type array<integer,object<stdClass>> of property $currentSchema.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55 5
            $this->usingSwagger = true;
56 6
        } catch (NoValidatorException $e) {
57 1
            $this->usingSwagger = false;
58 1
            $this->decorated->setController($controller);
59
        }
60 6
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65 6
    public function get($name, $strict = null)
66
    {
67 6
        if ($this->usingSwagger) {
68 5
            $currentSchema = $this->getCurrentSchema();
69 5
            if (array_key_exists($name, $currentSchema)) {
70 4
                $default = isset($currentSchema[$name]->default) ? $currentSchema[$name]->default : null;
71
72 4
                return $this->requestStack->getCurrentRequest()->query->get($name, $default);
73
            } else {
74 1
                throw new \InvalidArgumentException(sprintf("No swagger definition for parameter '%s'.", $name));
75
            }
76
        } else {
77 1
            return $this->decorated->get($name, $strict);
78
        }
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 2
    public function all($strict = null)
85
    {
86 2
        if ($this->usingSwagger) {
87 1
            $parameters = [];
88 1
            foreach ($this->getCurrentSchema() as $item) {
89 1
                $parameters[$item->name] = $this->get($item->name);
90 1
            }
91
92 1
            return $parameters;
93
        } else {
94 1
            return $this->decorated->all($strict);
0 ignored issues
show
Bug introduced by
It seems like $strict defined by parameter $strict on line 84 can also be of type null; however, FOS\RestBundle\Request\P...FetcherInterface::all() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
95
        }
96
    }
97
98
    /**
99
     * @return \stdClass[]
100
     */
101 5
    private function getCurrentSchema()
102
    {
103 5
        $request = $this->requestStack->getCurrentRequest();
104 5
        $path = $request->getPathInfo();
105
106 5
        if ($this->currentSchema === null) {
107 5
            $schemaManager = $this->currentValidator->getSchemaManager();
108
109 5
            $this->currentSchema = [];
110 5
            if ($schemaManager->findPathInTemplates($path, $template, $params)) {
111 5
                $currentSchema = $schemaManager->getRequestQueryParameters($template, $request->getMethod());
112
113 5
                foreach ($currentSchema as $keySchema) {
114 5
                    $this->currentSchema[$keySchema->name] = $keySchema;
115 5
                }
116 5
            }
117 5
        }
118
119 5
        return $this->currentSchema;
120
    }
121
}
122