ListCommand   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 9.62 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 4.76%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 5
dl 5
loc 52
ccs 1
cts 21
cp 0.0476
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 6 1
B execute() 5 33 7

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
 * ListCommand.php
4
 *
5
 * @copyright      More in license.md
6
 * @license        https://www.ipublikuj.eu
7
 * @author         Adam Kadlec <[email protected]>
8
 * @package        iPublikuj:Packages!
9
 * @subpackage     Commands
10
 * @since          2.0.0
11
 *
12
 * @date           19.07.16
13
 */
14
15
declare(strict_types = 1);
16
17
namespace IPub\Packages\Commands;
18
19
use Symfony\Component\Console\Input;
20
use Symfony\Component\Console\Output;
21
22
use IPub\Packages\Exceptions;
23
24
/**
25
 * List packages command
26
 *
27
 * @package        iPublikuj:Packages!
28
 * @subpackage     Commands
29
 *
30
 * @author         Josef Kříž <[email protected]>
31
 * @author         Adam Kadlec <[email protected]>
32
 */
33 1
final class ListCommand extends Command
34
{
35
	/**
36
	 * @return void
37
	 */
38
	protected function configure() : void
39
	{
40
		$this
41
			->setName('ipub:packages:list')
42
			->setDescription('List of installed packages.');
43
	}
44
45
	/**
46
	 * @param Input\InputInterface $input
47
	 * @param Output\OutputInterface $output
48
	 *
49
	 * @return void
50
	 */
51
	protected function execute(Input\InputInterface $input, Output\OutputInterface $output) : void
52
	{
53
		// Register available
54 View Code Duplication
		foreach ($this->packageManager->registerAvailable() as $item) {
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...
55
			foreach ($item as $name => $action) {
56
				$output->writeln(sprintf('<info>%s : %s</info>', $action, $name));
57
			}
58
		}
59
60
		try {
61
			$packages = $this->repository->getPackages();
62
63
			$maxLength = 0;
64
65
			foreach ($packages as $package) {
66
				$length = strlen($package->getName());
67
68
				$maxLength = $maxLength > $length ? $maxLength : $length;
69
			}
70
71
			foreach ($packages as $package) {
72
				$output->writeln(sprintf(
73
					'<info>%' . $maxLength . 's</info> | status: <comment>%-12s</comment> | version: <comment>%s</comment>',
74
					$package->getName(),
75
					$this->packageManager->getStatus($package),
76
					$this->packageManager->getVersion($package)
77
				));
78
			}
79
80
		} catch (Exceptions\InvalidArgumentException $e) {
81
			$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
82
		}
83
	}
84
}
85