Router   A
last analyzed

Coupling/Cohesion

Components 1
Dependencies 9

Complexity

Total Complexity 18

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Test Coverage

Coverage 97.78%

Importance

Changes 6
Bugs 2 Features 0
Metric Value
wmc 18
lcom 1
cbo 9
dl 0
loc 147
ccs 44
cts 45
cp 0.9778
rs 10
c 6
b 2
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A exists() 0 4 2
A execute() 0 22 3
A getObject() 0 14 3
A settings() 0 6 1
A __construct() 0 5 3
A addExtension() 0 10 3
A getRouter() 0 10 3
1
<?php
2
3
namespace League\CLImate\TerminalObject\Router;
4
5
use League\CLImate\Decorator\Parser\ParserImporter;
6
use League\CLImate\Settings\Manager;
7
use League\CLImate\Settings\SettingsImporter;
8
use League\CLImate\Util\OutputImporter;
9
use League\CLImate\Util\UtilImporter;
10
11
class Router
12
{
13
    use ParserImporter, SettingsImporter, OutputImporter, UtilImporter;
14
15
    /**
16
     * An instance of the Settings Manager class
17
     *
18
     * @var \League\CLImate\Settings\Manager $settings;
19
     */
20
    protected $settings;
21
22
    /**
23
     * An instance of the Dynamic Router class
24
     *
25
     * @var \League\CLImate\TerminalObject\Router\DynamicRouter $dynamic;
26
     */
27
    protected $dynamic;
28
29
    /**
30
     * An instance of the Basic Router class
31
     *
32
     * @var \League\CLImate\TerminalObject\Router\BasicRouter $basic;
33
     */
34
    protected $basic;
35
36 948
    public function __construct(DynamicRouter $dynamic = null, BasicRouter $basic = null)
37
    {
38 948
        $this->dynamic = $dynamic ?: new DynamicRouter();
39 948
        $this->basic   = $basic ?: new BasicRouter();
40 948
    }
41
42
    /**
43
     * Register a custom class with the router
44
     *
45
     * @param string $key
46
     * @param string $class
47
     */
48 36
    public function addExtension($key, $class)
49
    {
50 36
        $extension = new ExtensionCollection($key, $class);
51
52 28
        foreach ($extension->collection() as $obj_type => $collection) {
53 28
            foreach ($collection as $obj_key => $obj_class) {
54 28
                $this->{$obj_type}->addExtension($obj_key, $obj_class);
55 28
            }
56 28
        }
57 28
    }
58
59
    /**
60
     * Check if the name matches an existing terminal object
61
     *
62
     * @param string $name
63
     *
64
     * @return boolean
65
     */
66 580
    public function exists($name)
67
    {
68 580
        return ($this->basic->exists($name) || $this->dynamic->exists($name));
69
    }
70
71
    /**
72
     * Execute a terminal object using given arguments
73
     *
74
     * @param string $name
75
     * @param mixed  $arguments
76
     *
77
     * @return null|\League\CLImate\TerminalObject\Basic\BasicTerminalObjectInterface
78
     */
79 568
    public function execute($name, $arguments)
80
    {
81 568
        $router = $this->getRouter($name);
82
83 568
        $router->output($this->output);
84
85 568
        $obj = $this->getObject($router, $name, $arguments);
0 ignored issues
show
Bug introduced by Joe Tannenbaum
It seems like $router defined by $this->getRouter($name) on line 81 can also be of type null; however, League\CLImate\TerminalO...ter\Router::getObject() does only seem to accept object<League\CLImate\Te...Router\RouterInterface>, 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...
86
87 568
        $obj->parser($this->parser);
88 568
        $obj->util($this->util);
89
90
        // If the object needs any settings, import them
91 568
        foreach ($obj->settings() as $obj_setting) {
92 80
            $setting = $this->settings->get($obj_setting);
93
94 80
            if ($setting) {
95 44
                $obj->importSetting($setting);
96 44
            }
97 568
        }
98
99 568
        return $router->execute($obj);
100
    }
101
102
    /**
103
     * Get the object whether it's a string or already instantiated
104
     *
105
     * @param \League\CLImate\TerminalObject\Router\RouterInterface $router
106
     * @param string $name
107
     * @param array $arguments
108
     *
109
     * @return \League\CLImate\TerminalObject\Dynamic\DynamicTerminalObjectInterface|\League\CLImate\TerminalObject\Basic\BasicTerminalObjectInterface
110
     */
111 568
    protected function getObject($router, $name, $arguments)
112
    {
113 568
        $obj = $router->path($name);
114
115 568
        if (is_string($obj)) {
116 560
            $obj = (new \ReflectionClass($obj))->newInstanceArgs($arguments);
117 560
        }
118
119 568
        if (method_exists($obj, 'arguments')) {
120 4
            call_user_func_array([$obj, 'arguments'], $arguments);
121 4
        }
122
123 568
        return $obj;
124
    }
125
126
    /**
127
     * Determine which type of router we are using and return it
128
     *
129
     * @param string $name
130
     *
131
     * @return \League\CLImate\TerminalObject\Router\RouterInterface|null
132
     */
133 568
    protected function getRouter($name)
134
    {
135 568
        if ($this->basic->exists($name)) {
136 356
            return $this->basic;
137
        }
138
139 224
        if ($this->dynamic->exists($name)) {
140 224
            return $this->dynamic;
141
        }
142
    }
143
144
    /**
145
     * Set the settings property
146
     *
147
     * @param  \League\CLImate\Settings\Manager $settings
148
     *
149
     * @return Router
150
     */
151 568
    public function settings(Manager $settings)
152
    {
153 568
        $this->settings = $settings;
154
155 568
        return $this;
156
    }
157
}
158