Completed
Push — refactor ( 0ce6e5 )
by Marco
01:39
created

Factory   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 7

Importance

Changes 0
Metric Value
wmc 10
lcom 0
cbo 7
dl 0
loc 40
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
D fromArray() 0 32 10
1
<?php
2
3
namespace MJanssen\Route;
4
5
use InvalidArgumentException;
6
7
class Factory
8
{
9
    /**
10
     * @param array $route
11
     *
12
     * @return Route
13
     */
14
    public static function fromArray(array $route)
15
    {
16
        if (!isset($route['pattern'])) {
17
            throw new InvalidArgumentException('Required parameter pattern is not set.');
18
        }
19
20
        if (!isset($route['method'])) {
21
            throw new InvalidArgumentException('Required parameter method is not set.');
22
        }
23
24
        if (!isset($route['controller'])) {
25
            throw new InvalidArgumentException('Required parameter controller is not set.');
26
        }
27
28
        if (isset($route['value']) && !is_array($route['value'])) {
29
            $route['value'] = [];
30
        }
31
32
        $methods = !is_array($route['method']) ? [$route['method']] : $route['method'];
33
        $assert = isset($route['assert']) ? $route['assert'] : [];
34
        $value = isset($route['value']) ? $route['value'] : [];
35
        $name = isset($route['name']) ? $route['name'] : '';
36
37
        return new Route(
38
            new Methods($methods),
39
            new Pattern($route['pattern']),
40
            new Controller($route['controller']),
41
            new Asserts($assert),
42
            new Values($value),
43
            new Name($name)
0 ignored issues
show
Bug introduced by
It seems like $name defined by isset($route['name']) ? $route['name'] : '' on line 35 can also be of type array; however, MJanssen\Route\Name::__construct() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
44
        );
45
    }
46
}