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

Console   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 93
Duplicated Lines 8.6 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getCommands() 0 9 2
C loadCommands() 8 59 11

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
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