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

Dependencies::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 2.0263

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 16
nc 2
nop 2
dl 0
loc 26
ccs 13
cts 16
cp 0.8125
crap 2.0263
rs 9.7333
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace drupol\ComposerPackages\Commands;
6
7
use Composer\Command\BaseCommand as Command;
8
use ComposerPackages\Dependencies as PackageDependencies;
9
use ComposerPackages\Packages;
10
use ComposerPackages\Versions;
11
use PBergman\Console\Helper\TreeHelper;
12
use Symfony\Component\Console\Input\InputInterface;
13
use Symfony\Component\Console\Input\InputOption;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Question\Question;
16
17
class Dependencies extends Command
18
{
19
    protected $color;
20
21 2
    protected function configure(): void
22
    {
23 2
        $this->setName('dependencies');
24 2
        $this->setDescription('Return the package dependencies.');
25 2
        $this->addOption('recursive', 'r', InputOption::VALUE_NONE, 'Return dependencies recursively.');
26 2
    }
27
28 2
    protected function execute(InputInterface $input, OutputInterface $output)
29
    {
30 2
        $helper = $this->getHelper('question');
31 2
        $tree = new TreeHelper();
32
33 2
        $packages = array_keys(iterator_to_array(Packages::packages()));
34 2
        $question = new Question('<question>Please type the package name:</question>' . \PHP_EOL);
35
36 2
        if (method_exists($question, 'setAutocompleterCallback')) {
37
            $callback = static function (string $userInput) use ($packages): array {
38
                return array_filter($packages, static function (string $packageName) use ($userInput) {
39
                    return false !== mb_stripos($packageName, $userInput);
40
                });
41 2
            };
42 2
            $question->setAutocompleterCallback($callback);
43
        } else {
44
            $question->setAutocompleterValues($packages);
45
        }
46
47 2
        $rows = $this->getDependenciesOf(
48 2
            $helper->ask($input, $output, $question) ?? '',
49 2
            (bool) $input->getOption('recursive')
50
        );
51
52 2
        $tree->addArray($rows);
53 2
        $tree->printTree($output);
54 2
    }
55
56 2
    private function getDependenciesOf(string $search, bool $isRecursive, bool $isChild = false): array
0 ignored issues
show
Unused Code introduced by
The parameter $isChild is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

56
    private function getDependenciesOf(string $search, bool $isRecursive, /** @scrutinizer ignore-unused */ bool $isChild = false): array

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
57
    {
58 2
        $rows = [];
59 2
        $dependencies = PackageDependencies::get($search);
60
61 2
        foreach ($dependencies as $number => $dependency) {
62 2
            $row = $dependency . ': <fg=yellow>' . (Versions::get($dependency) ?? '~') . '</>';
63
64 2
            if ($isRecursive) {
65 1
                $rows += $this->getDependenciesOf($dependency, $isRecursive, true);
66
            } else {
67 1
                $rows[$row] = [];
68
            }
69
        }
70
71 2
        return [$search . ': <fg=yellow>' . (Versions::get($search) ?? '~') . '</>' => $rows];
72
    }
73
}
74