EnableCommand::execute()   C
last analyzed

Complexity

Conditions 12
Paths 132

Size

Total Lines 57

Duplication

Lines 15
Ratio 26.32 %

Code Coverage

Tests 0
CRAP Score 156

Importance

Changes 0
Metric Value
dl 15
loc 57
ccs 0
cts 28
cp 0
rs 6.2981
c 0
b 0
f 0
cc 12
nc 132
nop 2
crap 156

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
 * EnableCommand.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\Helper\QuestionHelper;
20
use Symfony\Component\Console\Input;
21
use Symfony\Component\Console\Output;
22
23
use IPub\Packages\DependencyResolver;
24
use IPub\Packages\Exceptions;
25
use Symfony\Component\Console\Question\ConfirmationQuestion;
26
27
/**
28
 * Package enable command
29
 *
30
 * @package        iPublikuj:Packages!
31
 * @subpackage     Commands
32
 *
33
 * @author         Josef Kříž <[email protected]>
34
 * @author         Adam Kadlec <[email protected]>
35
 */
36 1
final class EnableCommand extends Command
37
{
38
	/**
39
	 * @return void
40
	 */
41 View Code Duplication
	protected function configure() : void
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
42
	{
43
		$this
44
			->setName('ipub:packages:enable')
45
			->addArgument('package', Input\InputArgument::REQUIRED, 'Package name')
46
			->addOption('noconfirm', NULL, Input\InputOption::VALUE_NONE, 'do not ask for any confirmation')
47
			->setDescription('Enable package.');
48
	}
49
50
	/**
51
	 * @param Input\InputInterface $input
52
	 * @param Output\OutputInterface $output
53
	 *
54
	 * @return void
55
	 */
56
	protected function execute(Input\InputInterface $input, Output\OutputInterface $output) : void
57
	{
58
		// Register available
59 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...
60
			foreach ($item as $name => $action) {
61
				$output->writeln(sprintf('<info>%s : %s</info>', $action, $name));
62
			}
63
		}
64
65
		$package = $this->repository->findPackage($input->getArgument('package'));
66
67
		try {
68
			$problem = $this->packageManager->testEnable($package);
0 ignored issues
show
Bug introduced by
It seems like $package defined by $this->repository->findP...getArgument('package')) on line 65 can also be of type boolean; however, IPub\Packages\IPackagesManager::testEnable() does only seem to accept object<IPub\Packages\Entities\IPackage>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
69
70
		} catch (Exceptions\InvalidArgumentException $e) {
71
			$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
72
73
			return;
74
		}
75
76
		if (!$input->getOption('noconfirm') && count($problem->getSolutions()) > 0) {
77
			$output->writeln(sprintf('<info>enabling : %s</info>', $package->getName()));
78
79
			foreach ($problem->getSolutions() as $job) {
80
				$output->writeln(sprintf('<info>%s : %s</info>', $job->getAction(), $job->getPackage()->getName()));
81
			}
82
83
			/** @var QuestionHelper $dialog */
84
			$dialog = $this->getHelperSet()->get('question');
85
86
			$question = new ConfirmationQuestion('Continue with this action? [y/N]', FALSE);
87
88
			if (!$dialog->ask($input, $output, $question)) {
89
				return;
90
			}
91
		}
92
93
		try {
94 View Code Duplication
			foreach ($problem->getSolutions() as $job) {
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...
95
				if ($job->getAction() === DependencyResolver\Job::ACTION_ENABLE) {
96
					$this->packageManager->enable($job->getPackage());
97
					$output->writeln(sprintf('Package \'%s\' has been enabled.', $job->getPackage()->getName()));
98
99
				} elseif ($job->getAction() === DependencyResolver\Job::ACTION_DISABLE) {
100
					$this->packageManager->disable($job->getPackage());
101
					$output->writeln(sprintf('Package \'%s\' has been disabled.', $job->getPackage()->getName()));
102
				}
103
			}
104
105
			$this->packageManager->enable($package);
0 ignored issues
show
Bug introduced by
It seems like $package defined by $this->repository->findP...getArgument('package')) on line 65 can also be of type boolean; however, IPub\Packages\IPackagesManager::enable() does only seem to accept object<IPub\Packages\Entities\IPackage>, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
106
107
			$output->writeln(sprintf('Package \'%s\' has been enabled.', $input->getArgument('package')));
108
109
		} catch (Exceptions\InvalidArgumentException $e) {
110
			$output->writeln(sprintf('<error>%s</error>', $e->getMessage()));
111
		}
112
	}
113
}
114