Completed
Pull Request — master (#7)
by
unknown
14:27
created

PackagesType::execute()   B

Complexity

Conditions 8
Paths 72

Size

Total Lines 88
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 8
eloc 54
c 2
b 0
f 0
nc 72
nop 2
dl 0
loc 88
rs 7.7591

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
    protected function configure()
20
    {
21
        $this->setName('packages-type');
22
        $this->setDescription('Return the packages of a type.');
23
    }
24
25
    protected function execute(InputInterface $input, OutputInterface $output): ?int
26
    {
27
        $types = Types::getTypes();
28
29
        $question = new Question('<question>Please provide type of packages:</question>' . \PHP_EOL);
30
31
        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
            };
37
            $question->setAutocompleterCallback($callback);
38
        } else {
39
            $question->setAutocompleterValues($types);
40
        }
41
42
        $helper = $this->getHelper('question');
43
44
        $type = $helper->ask($input, $output, $question) ?? '';
45
        /** @var CompletePackage[] $packages */
46
        $packages = iterator_to_array(Types::get($type));
47
48
        $names = array_map(static function (CompletePackage $completePackage) {
49
            return $completePackage->getName();
50
        }, $packages);
51
52
        foreach ($packages as $package) {
53
            $output->writeln($package->getName());
54
        }
55
56
        $question = new Question('<question>Please type the package name you want to explore:</question>' . \PHP_EOL);
57
58
        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
            };
64
            $question->setAutocompleterCallback($callback);
65
        } else {
66
            $question->setAutocompleterValues($names);
67
        }
68
69
        $packageName = $helper->ask($input, $output, $question) ?? '';
70
        $package = Packages::get($packageName);
71
72
        if (null === $package) {
73
            return null;
74
        }
75
76
        $output->writeln('<fg=green>name    </> :' . $package->getName());
77
        $output->writeln('<fg=green>descrip.</> :' . $package->getDescription() ?? '~');
78
        $output->writeln('<fg=green>keywords</> :' . implode(', ', $package->getKeywords() ?? ['~']));
79
        $output->writeln('<fg=green>version </> :' . $package->getVersion() ?? '~');
80
        $output->writeln('<fg=green>type    </> :' . $package->getType());
81
        $output->writeln('<fg=green>license </> :' . implode(' ', $package->getLicense() ?? ['~']));
82
        $output->writeln(
83
            '<fg=green>source  </> :[' . $package->getSourceType() . '] ' .
84
                        $package->getSourceUrl() . ' ' . $package->getSourceReference()
85
        );
86
        $output->writeln(
87
            '<fg=green>dist    </> :[' . $package->getDistType() . '] ' .
88
                    $package->getDistUrl() . ' ' . $package->getDistReference()
89
        );
90
91
        $output->writeln('');
92
        $output->writeln('<fg=green>requires</>');
93
94
        foreach ($package->getRequires() as $link) {
95
            $output->writeln($link->getTarget() . ' <fg=yellow>' . $link->getPrettyConstraint() . '</>');
96
        }
97
98
        $output->writeln('');
99
        $output->writeln('<fg=green>requires-dev</>');
100
101
        foreach ($package->getDevRequires() as $link) {
102
            $output->writeln($link->getTarget() . ' <fg=yellow>' . $link->getPrettyConstraint() . '</>');
103
        }
104
105
        $output->writeln('');
106
        $output->writeln('<fg=green>suggest</>');
107
108
        foreach ($package->getSuggests() as $name => $suggest) {
109
            $output->writeln("{$name} <fg=yellow>{$suggest}</>");
110
        }
111
112
        return 0;
113
    }
114
}
115