GenericFactory::__invoke()   D
last analyzed

Complexity

Conditions 19
Paths 325

Size

Total Lines 59
Code Lines 38

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 0
Metric Value
c 5
b 0
f 0
dl 0
loc 59
rs 4.6771
cc 19
eloc 38
nc 325
nop 3

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Dash
4
 *
5
 * @link      http://github.com/DASPRiD/Dash For the canonical source repository
6
 * @copyright 2013-2015 Ben Scholzen 'DASPRiD'
7
 * @license   http://opensource.org/licenses/BSD-2-Clause Simplified BSD License
8
 */
9
10
namespace Dash\Route;
11
12
use Dash\Parser\ParserManager;
13
use Dash\RouteCollection\LazyRouteCollection;
14
use Interop\Container\ContainerInterface;
15
16
class GenericFactory
17
{
18
    /**
19
     * {@inheritdoc}
20
     *
21
     * @return Generic
22
     */
23
    public function __invoke(ContainerInterface $container, $resolvedName, array $options = null)
0 ignored issues
show
Unused Code introduced by
The parameter $resolvedName is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
24
    {
25
        if ($options === null) {
26
            return new Generic();
27
        }
28
29
        // Parameter normalization
30
        if (!isset($options['path']) && isset($options[0])) {
31
            $options['path'] = $options[0];
32
        }
33
34
        if (isset($options['defaults']) && isset($options[1])) {
35
            $options['defaults'] += $options[1];
36
        } elseif (isset($options[1])) {
37
            $options['defaults'] = $options[1];
38
        }
39
40
        if (isset($options['methods']) && isset($options[2])) {
41
            $options['methods'] = array_merge($options[2], $options['methods']);
42
        } elseif (isset($options[2])) {
43
            $options['methods'] = $options[2];
44
        }
45
46
        // Try to retrive path and hostname parser
47
        $parserManager = $container->get(ParserManager::class);
48
49
        if (isset($options['path_parser'])) {
50
            $pathParser = $parserManager->build($options['path_parser'], $options);
51
        } elseif (isset($options['path'])) {
52
            $pathParser = $parserManager->build('PathSegment', $options);
53
        } else {
54
            $pathParser = null;
55
        }
56
57
        if (isset($options['hostname_parser'])) {
58
            $hostnameParser = $parserManager->build($options['hostname_parser'], $options);
59
        } elseif (isset($options['hostname'])) {
60
            $hostnameParser = $parserManager->build('HostnameSegment', $options);
61
        } else {
62
            $hostnameParser = null;
63
        }
64
65
        // Setup children if they exist
66
        if (isset($options['children'])) {
67
            $children = new LazyRouteCollection($container->get(RouteManager::class), $options['children']);
68
        } else {
69
            $children = null;
70
        }
71
72
        return new Generic(
73
            $pathParser,
74
            $hostnameParser,
75
            isset($options['methods']) ? $options['methods'] : null,
76
            isset($options['secure']) ? $options['secure'] : null,
77
            isset($options['port']) ? $options['port'] : null,
78
            isset($options['defaults']) ? $options['defaults'] : [],
79
            $children
80
        );
81
    }
82
}
83