Completed
Push — master ( d2a193...a63b69 )
by Michael
02:24
created

Console::loadCommands()   C

Complexity

Conditions 9
Paths 6

Size

Total Lines 51
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 90

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 51
ccs 0
cts 40
cp 0
rs 6.2727
cc 9
eloc 22
nc 6
nop 0
crap 90

How to fix   Long Method   

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
namespace Stats;
4
5
use Joomla\Application\Cli\ColorStyle;
6
use Joomla\Application\Cli\Output\Processor\ColorProcessor;
7
use Joomla\Controller\AbstractController;
8
use Joomla\DI\ContainerAwareInterface;
9
use Joomla\DI\ContainerAwareTrait;
10
11
/**
12
 * CLI Console
13
 *
14
 * @since  1.0
15
 */
16
class Console implements ContainerAwareInterface
17
{
18
	use ContainerAwareTrait;
19
20
	/**
21
	 * Array of available command objects
22
	 *
23
	 * @var    CommandInterface[]
24
	 * @since  1.0
25
	 */
26
	private $commands = [];
27
28
	/**
29
	 * Get the available commands.
30
	 *
31
	 * @return  CommandInterface[]
32
	 *
33
	 * @since   1.0
34
	 * @throws  \RuntimeException
35
	 */
36
	public function getCommands()
37
	{
38
		if (empty($this->commands))
39
		{
40
			$this->commands = $this->loadCommands();
41
		}
42
43
		return $this->commands;
44
	}
45
46
	/**
47
	 * Load the application's commands
48
	 *
49
	 * @return  CommandInterface[]
50
	 *
51
	 * @since   1.0
52
	 */
53
	private function loadCommands()
54
	{
55
		$commands = [];
56
57
		/** @var \DirectoryIterator $fileInfo */
58
		foreach (new \DirectoryIterator(__DIR__ . '/Commands') as $fileInfo)
59
		{
60
			if ($fileInfo->isDot())
61
			{
62
				continue;
63
			}
64
65
			if ($fileInfo->isDir())
66
			{
67
				$namespace = $fileInfo->getFilename();
68
69
				/** @var \DirectoryIterator $subFileInfo */
70
				foreach (new \DirectoryIterator($fileInfo->getPathname()) as $subFileInfo)
71
				{
72
					if ($subFileInfo->isDot() || !$subFileInfo->isFile())
73
					{
74
						continue;
75
					}
76
77
					$command   = $subFileInfo->getBasename('.php');
78
					$className = __NAMESPACE__ . "\\Commands\\$namespace\\$command";
79
		
80
					if (!class_exists($className))
81
					{
82
						throw new \RuntimeException(sprintf('Required class "%s" not found.', $className));
83
					}
84
		
85
					$commands[strtolower("$namespace:" . str_replace('Command', '', $command))] = $this->getContainer()->get($className);
86
				}
87
			}
88
			else
89
			{
90
				$command   = $fileInfo->getBasename('.php');
91
				$className = __NAMESPACE__ . "\\Commands\\$command";
92
	
93
				if (!class_exists($className))
94
				{
95
					throw new \RuntimeException(sprintf('Required class "%s" not found.', $className));
96
				}
97
	
98
				$commands[strtolower(str_replace('Command', '', $command))] = $this->getContainer()->get($className);
99
			}
100
		}
101
102
		return $commands;
103
	}
104
}
105