Completed
Push — mysql_improvements ( 2e95ce...dedbef )
by Michael
03:52
created

Console::loadCommands()   C

Complexity

Conditions 11
Paths 8

Size

Total Lines 59
Code Lines 24

Duplication

Lines 8
Ratio 13.56 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 0
Metric Value
dl 8
loc 59
ccs 0
cts 23
cp 0
rs 6.3545
c 0
b 0
f 0
cc 11
eloc 24
nc 8
nop 0
crap 132

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
 * Joomla! Statistics Server
4
 *
5
 * @copyright  Copyright (C) 2013 - 2017 Open Source Matters, Inc. All rights reserved.
6
 * @license    http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License Version 2 or Later
7
 */
8
9
namespace Joomla\StatsServer;
10
11
use Joomla\DI\{
12
	ContainerAwareInterface, ContainerAwareTrait
13
};
14
15
/**
16
 * CLI Console
17
 */
18
class Console implements ContainerAwareInterface
19
{
20
	use ContainerAwareTrait;
21
22
	/**
23
	 * Array of available command objects
24
	 *
25
	 * @var  CommandInterface[]
26
	 */
27
	private $commands = [];
28
29
	/**
30
	 * Get the available commands.
31
	 *
32
	 * @return  CommandInterface[]
33
	 *
34
	 * @throws  \RuntimeException
35
	 */
36
	public function getCommands() : array
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
	private function loadCommands() : array
52
	{
53
		$commands = [];
54
55
		/** @var \DirectoryIterator $fileInfo */
56
		foreach (new \DirectoryIterator(__DIR__ . '/Commands') as $fileInfo)
57
		{
58
			if ($fileInfo->isDot())
59
			{
60
				continue;
61
			}
62
63
			if ($fileInfo->isDir())
64
			{
65
				$namespace = $fileInfo->getFilename();
66
67
				/** @var \DirectoryIterator $subFileInfo */
68
				foreach (new \DirectoryIterator($fileInfo->getPathname()) as $subFileInfo)
69
				{
70
					if ($subFileInfo->isDot() || !$subFileInfo->isFile())
71
					{
72
						continue;
73
					}
74
75
					$command   = $subFileInfo->getBasename('.php');
76
					$className = __NAMESPACE__ . "\\Commands\\$namespace\\$command";
77
78
					if (!class_exists($className))
79
					{
80
						throw new \RuntimeException(sprintf('Required class "%s" not found.', $className));
81
					}
82
83
					// If the class isn't instantiable, it isn't a valid command
84 View Code Duplication
					if ((new \ReflectionClass($className))->isInstantiable())
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
85
					{
86
						$commands[strtolower("$namespace:" . str_replace('Command', '', $command))] = $this->getContainer()->get($className);
87
					}
88
				}
89
			}
90
			else
91
			{
92
				$command   = $fileInfo->getBasename('.php');
93
				$className = __NAMESPACE__ . "\\Commands\\$command";
94
95
				if (!class_exists($className))
96
				{
97
					throw new \RuntimeException(sprintf('Required class "%s" not found.', $className));
98
				}
99
100
				// If the class isn't instantiable, it isn't a valid command
101 View Code Duplication
				if ((new \ReflectionClass($className))->isInstantiable())
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
102
				{
103
					$commands[strtolower(str_replace('Command', '', $command))] = $this->getContainer()->get($className);
104
				}
105
			}
106
		}
107
108
		return $commands;
109
	}
110
}
111