Completed
Push — master ( 10f95b...bf15d4 )
by Michael
09:02
created

Console   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 97
Duplicated Lines 8.25 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
c 0
b 0
f 0
lcom 1
cbo 2
dl 8
loc 97
ccs 0
cts 27
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getCommands() 0 9 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
 * @since  1.0
19
 */
20
class Console implements ContainerAwareInterface
21
{
22
	use ContainerAwareTrait;
23
24
	/**
25
	 * Array of available command objects
26
	 *
27
	 * @var    CommandInterface[]
28
	 * @since  1.0
29
	 */
30
	private $commands = [];
31
32
	/**
33
	 * Get the available commands.
34
	 *
35
	 * @return  CommandInterface[]
36
	 *
37
	 * @since   1.0
38
	 * @throws  \RuntimeException
39
	 */
40
	public function getCommands() : array
41
	{
42
		if (empty($this->commands))
43
		{
44
			$this->commands = $this->loadCommands();
45
		}
46
47
		return $this->commands;
48
	}
49
50
	/**
51
	 * Load the application's commands
52
	 *
53
	 * @return  CommandInterface[]
54
	 *
55
	 * @since   1.0
56
	 */
57
	private function loadCommands() : array
58
	{
59
		$commands = [];
60
61
		/** @var \DirectoryIterator $fileInfo */
62
		foreach (new \DirectoryIterator(__DIR__ . '/Commands') as $fileInfo)
63
		{
64
			if ($fileInfo->isDot())
65
			{
66
				continue;
67
			}
68
69
			if ($fileInfo->isDir())
70
			{
71
				$namespace = $fileInfo->getFilename();
72
73
				/** @var \DirectoryIterator $subFileInfo */
74
				foreach (new \DirectoryIterator($fileInfo->getPathname()) as $subFileInfo)
75
				{
76
					if ($subFileInfo->isDot() || !$subFileInfo->isFile())
77
					{
78
						continue;
79
					}
80
81
					$command   = $subFileInfo->getBasename('.php');
82
					$className = __NAMESPACE__ . "\\Commands\\$namespace\\$command";
83
84
					if (!class_exists($className))
85
					{
86
						throw new \RuntimeException(sprintf('Required class "%s" not found.', $className));
87
					}
88
89
					// If the class isn't instantiable, it isn't a valid command
90 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...
91
					{
92
						$commands[strtolower("$namespace:" . str_replace('Command', '', $command))] = $this->getContainer()->get($className);
93
					}
94
				}
95
			}
96
			else
97
			{
98
				$command   = $fileInfo->getBasename('.php');
99
				$className = __NAMESPACE__ . "\\Commands\\$command";
100
101
				if (!class_exists($className))
102
				{
103
					throw new \RuntimeException(sprintf('Required class "%s" not found.', $className));
104
				}
105
106
				// If the class isn't instantiable, it isn't a valid command
107 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...
108
				{
109
					$commands[strtolower(str_replace('Command', '', $command))] = $this->getContainer()->get($className);
110
				}
111
			}
112
		}
113
114
		return $commands;
115
	}
116
}
117