Completed
Pull Request — master (#112)
by Daniel
19:27 queued 17:52
created

src/TerminalObject/Router/Router.php (1 issue)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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 936
    public function __construct(DynamicRouter $dynamic = null, BasicRouter $basic = null)
37
    {
38 936
        $this->dynamic = $dynamic ?: new DynamicRouter();
39 936
        $this->basic   = $basic ?: new BasicRouter();
40 936
    }
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 7
            }
56 7
        }
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 568
    public function exists($name)
67
    {
68 568
        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 556
    public function execute($name, $arguments)
80
    {
81 556
        $router = $this->getRouter($name);
82
83 556
        $router->output($this->output);
84
85 556
        $obj = $this->getObject($router, $name, $arguments);
0 ignored issues
show
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 556
        $obj->parser($this->parser);
88 556
        $obj->util($this->util);
89
90
        // If the object needs any settings, import them
91 556
        foreach ($obj->settings() as $obj_setting) {
92 76
            $setting = $this->settings->get($obj_setting);
93
94 76
            if ($setting) {
95 68
                $obj->importSetting($setting);
96 11
            }
97 139
        }
98
99 556
        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 556
    protected function getObject($router, $name, $arguments)
112
    {
113 556
        $obj = $router->path($name);
114
115 556
        if (is_string($obj)) {
116 548
            $obj = (new \ReflectionClass($obj))->newInstanceArgs($arguments);
117 137
        }
118
119 556
        if (method_exists($obj, 'arguments')) {
120 4
            call_user_func_array([$obj, 'arguments'], $arguments);
121 1
        }
122
123 556
        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 556
    protected function getRouter($name)
134
    {
135 556
        if ($this->basic->exists($name)) {
136 356
            return $this->basic;
137
        }
138
139 212
        if ($this->dynamic->exists($name)) {
140 212
            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 556
    public function settings(Manager $settings)
152
    {
153 556
        $this->settings = $settings;
154
155 556
        return $this;
156
    }
157
}
158