Completed
Push — master ( 037316...f099cd )
by Pierre
04:19
created

RoutingNormalizer::normalizeDeclaration()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 8

Duplication

Lines 3
Ratio 16.67 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 5
eloc 8
c 3
b 0
f 0
nc 4
nop 1
dl 3
loc 18
rs 8.8571
1
<?php
2
3
namespace Knp\Rad\ResourceResolver;
4
5
class RoutingNormalizer
6
{
7
    /**
8
     * Normalizes string and array declarations into associative array.
9
     *
10
     * @param string|array $declaration
11
     *
12
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,null|boolean|array>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
13
     */
14
    public function normalizeDeclaration($declaration)
15
    {
16
        if (is_string($declaration)) {
17
            return $this->normalizeString($declaration);
18
        }
19
20
        // Normalize numerically indexed array
21
        if (array_keys($declaration) === array_keys(array_values($declaration))) {
22
            return $this->normalizeArray($declaration);
23
        }
24
25 View Code Duplication
        if (isset($declaration['arguments']) && !is_array($declaration['arguments'])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
26
            throw new \InvalidArgumentException('The "arguments" parameter should be an array of arguments.');
27
        }
28
29
        // Adds default value to associative array
30
        return array_merge(['method' => null, 'required' => true, 'arguments' => []], $declaration);
31
    }
32
33
    private function normalizeString($declaration)
34
    {
35
        $service = $declaration;
36
        $method  = null;
37
38 View Code Duplication
        if (strpos($declaration, ':') !== false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
39
            list($service, $method) = explode(':', $declaration);
40
        }
41
42
        return [
43
            'service'   => $service,
44
            'method'    => $method,
45
            'arguments' => [],
46
            'required'  => true,
47
        ];
48
    }
49
50
    private function normalizeArray($declaration)
51
    {
52 View Code Duplication
        if (isset($declaration[1]) && !is_array($declaration[1])) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
53
            throw new \InvalidArgumentException('The second argument for a resource configuration, when expressed with a numerically indexed array, should be an array of arguments.');
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 180 characters; contains 183 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
54
        }
55
56
        $service = $declaration[0];
57
        $method  = null;
58
59 View Code Duplication
        if (false !== strpos($declaration[0], ':')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
60
            list($service, $method) = explode(':', $declaration[0]);
61
        }
62
63
        return [
64
            'service'   => $service,
65
            'method'    => $method,
66
            'arguments' => isset($declaration[1]) ? $declaration[1] : [],
67
            'required'  => isset($declaration[2]) ? (bool) $declaration[2] : true,
68
        ];
69
    }
70
}
71