Completed
Pull Request — master (#7)
by
unknown
12:18 queued 10:05
created

PackagesType::execute()   B

Complexity

Conditions 8
Paths 72

Size

Total Lines 88
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 43
CRAP Score 8.3318

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 54
nc 72
nop 2
dl 0
loc 88
ccs 43
cts 52
cp 0.8269
crap 8.3318
rs 7.7591
c 2
b 0
f 0

How to fix   Long Method   

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
declare(strict_types=1);
4
5
namespace drupol\ComposerPackages\Commands;
6
7
use Composer\Command\BaseCommand as Command;
8
use Composer\Package\CompletePackage;
9
use ComposerPackages\Packages;
10
use ComposerPackages\Types;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Question\Question;
14
15
class PackagesType extends Command
16
{
17
    protected $color;
18
19 1
    protected function configure()
20
    {
21 1
        $this->setName('packages-type');
22 1
        $this->setDescription('Return the packages of a type.');
23 1
    }
24
25 1
    protected function execute(InputInterface $input, OutputInterface $output): ?int
26
    {
27 1
        $types = Types::getTypes();
28
29 1
        $question = new Question('<question>Please provide type of packages:</question>' . \PHP_EOL);
30
31 1
        if (method_exists($question, 'setAutocompleterCallback')) {
32
            $callback = static function (string $userInput) use ($types): array {
33
                return array_filter($types, static function (string $type) use ($userInput) {
34
                    return false !== mb_stripos($type, $userInput);
35
                });
36 1
            };
37 1
            $question->setAutocompleterCallback($callback);
38
        } else {
39
            $question->setAutocompleterValues($types);
40
        }
41
42 1
        $helper = $this->getHelper('question');
43
44 1
        $type = $helper->ask($input, $output, $question) ?? '';
45
        /** @var CompletePackage[] $packages */
46 1
        $packages = iterator_to_array(Types::get($type));
47
48
        $names = array_map(static function (CompletePackage $completePackage) {
49 1
            return $completePackage->getName();
50 1
        }, $packages);
51
52 1
        foreach ($packages as $package) {
53 1
            $output->writeln($package->getName());
54
        }
55
56 1
        $question = new Question('<question>Please type the package name you want to explore:</question>' . \PHP_EOL);
57
58 1
        if (method_exists($question, 'setAutocompleterCallback')) {
59
            $callback = static function (string $userInput) use ($names): array {
60
                return array_filter($names, static function (string $packageName) use ($userInput) {
61
                    return false !== mb_stripos($packageName, $userInput);
62
                });
63 1
            };
64 1
            $question->setAutocompleterCallback($callback);
65
        } else {
66
            $question->setAutocompleterValues($names);
67
        }
68
69 1
        $packageName = $helper->ask($input, $output, $question) ?? '';
70 1
        $package = Packages::get($packageName);
71
72 1
        if (null === $package) {
73
            return null;
74
        }
75
76 1
        $output->writeln('<fg=green>name    </> :' . $package->getName());
77 1
        $output->writeln('<fg=green>descrip.</> :' . $package->getDescription() ?? '~');
78 1
        $output->writeln('<fg=green>keywords</> :' . implode(', ', $package->getKeywords() ?? ['~']));
79 1
        $output->writeln('<fg=green>version </> :' . $package->getVersion() ?? '~');
80 1
        $output->writeln('<fg=green>type    </> :' . $package->getType());
81 1
        $output->writeln('<fg=green>license </> :' . implode(' ', $package->getLicense() ?? ['~']));
82 1
        $output->writeln(
83 1
            '<fg=green>source  </> :[' . $package->getSourceType() . '] ' .
84 1
                        $package->getSourceUrl() . ' ' . $package->getSourceReference()
85
        );
86 1
        $output->writeln(
87 1
            '<fg=green>dist    </> :[' . $package->getDistType() . '] ' .
88 1
                    $package->getDistUrl() . ' ' . $package->getDistReference()
89
        );
90
91 1
        $output->writeln('');
92 1
        $output->writeln('<fg=green>requires</>');
93
94 1
        foreach ($package->getRequires() as $link) {
95 1
            $output->writeln($link->getTarget() . ' <fg=yellow>' . $link->getPrettyConstraint() . '</>');
96
        }
97
98 1
        $output->writeln('');
99 1
        $output->writeln('<fg=green>requires-dev</>');
100
101 1
        foreach ($package->getDevRequires() as $link) {
102
            $output->writeln($link->getTarget() . ' <fg=yellow>' . $link->getPrettyConstraint() . '</>');
103
        }
104
105 1
        $output->writeln('');
106 1
        $output->writeln('<fg=green>suggest</>');
107
108 1
        foreach ($package->getSuggests() as $name => $suggest) {
109
            $output->writeln("{$name} <fg=yellow>{$suggest}</>");
110
        }
111
112 1
        return 0;
113
    }
114
}
115