Passed
Push — master ( eebbf4...7a551b )
by Jakub
33s
created

PathParamsMapper::isParamRequired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Eps\Req2CmdBundle\Params\ParameterMapper;
5
6
use Eps\Req2CmdBundle\Exception\ParamMapperException;
7
use Symfony\Component\HttpFoundation\Request;
8
9
class PathParamsMapper implements ParamMapperInterface
10
{
11
    private const REQUIRED_PROPERTY_PREFIX = '!';
12
13
    public function map(Request $request, array $propsMap): array
14
    {
15
        if (!array_key_exists('path', $propsMap)) {
16
            return [];
17
        }
18
19
        $pathProps = (array)$propsMap['path'];
20
        $result = [];
21
        foreach ($pathProps as $paramName => $paramValue) {
22
            if ($required = $this->isParamRequired($paramName)) {
0 ignored issues
show
Unused Code introduced by
$required is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
23
                $paramName = substr($paramName, 1);
24
                $this->assertRequiredParamIsPresent($paramName, $request);
25
            }
26
27
            if (!$request->attributes->has($paramName)) {
28
                continue;
29
            }
30
31
            $finalPropName = $paramValue ?? $paramName;
32
            $result[$finalPropName] = $request->attributes->get($paramName);
33
        }
34
35
        return $result;
36
    }
37
38
    private function isParamRequired(string $paramName): bool
39
    {
40
        return strpos($paramName, self::REQUIRED_PROPERTY_PREFIX) === 0;
41
    }
42
43
    private function assertRequiredParamIsPresent(string $paramName, Request $request): void
44
    {
45
        if (!$request->attributes->has($paramName)) {
46
            throw ParamMapperException::noParamFound($paramName);
47
        }
48
49
        if ($request->attributes->get($paramName) === null) {
50
            throw ParamMapperException::paramEmpty($paramName);
51
        }
52
    }
53
}
54