command::go()   B
last analyzed

Complexity

Conditions 10
Paths 8

Size

Total Lines 31
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 31
c 0
b 0
f 0
rs 7.6666
cc 10
nc 8
nop 1

How to fix   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
/**
4
 * © 2018 - Phoponent
5
 * Author: Nicolas Choquet
6
 * Email: [email protected]
7
 * LICENSE GPL ( GNU General Public License )
8
 */
9
10
namespace phoponent\framework\static_classe;
11
12
use Exception;
13
use phoponent\framework\command\help;
14
use phoponent\framework\traits\static_class;
15
16
class command {
17
	use static_class;
18
19
	private static function clean_argv($argv) {
20
		unset($argv[0]);
21
		$argv_tmp = [];
22
		foreach ($argv as $item) {
23
			$argv_tmp[] = $item;
24
		}
25
		return $argv_tmp;
26
	}
27
28
	/**
29
	 * @param $argv
30
	 * @throws Exception
31
	 */
32
	public static function go($argv) {
33
		$argv = self::clean_argv($argv);
34
		if(empty($argv) || (!empty($argv) && $argv[0] === '-h') || (!empty($argv) && $argv[0] === '--help')) {
35
			$argv = self::clean_argv($argv);
36
			if(is_file("phoponent/commands/help.php")) {
37
				require_once "phoponent/commands/help.php";
38
			}
39
			else {
40
				throw new Exception("command help not found !");
41
			}
42
			$command = new help($argv);
43
			$command->execute();
44
			return;
45
		}
46
		$command = explode(':', $argv[0]);
47
		$command_class = $command[0];
48
		$command_method = isset($command[1]) ? $command[1] : null;
49
		$argv = self::clean_argv($argv);
50
		if(is_file("phoponent/commands/{$command_class}.php")) {
51
			require_once "phoponent/commands/{$command_class}.php";
52
		}
53
		else {
54
			throw new Exception("command {$command_class} not found !");
55
		}
56
		$command_class = "\\phoponent\\framework\\command\\{$command_class}";
57
		$command_obj = new $command_class($argv);
58
		if($command_method) {
59
            $command_obj->execute($command_method);
60
        }
61
        else {
62
            $command_obj->execute();
63
        }
64
	}
65
}