Passed
Pull Request — master (#51)
by Dominik
02:37
created

InitCommand::canWriteToOutfile()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 6
c 1
b 0
f 0
dl 0
loc 13
ccs 0
cts 7
cp 0
rs 10
cc 3
nc 3
nop 2
crap 12
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dominikb\ComposerLicenseChecker;
6
7
use Dominikb\ComposerLicenseChecker\Traits\DependencyLoaderAwareTrait;
8
use Symfony\Component\Console\Command\Command;
9
use Symfony\Component\Console\Input\InputDefinition;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Style\SymfonyStyle;
14
15
class InitCommand extends Command
16
{
17
    use DependencyLoaderAwareTrait;
18
19
    protected function configure()
20
    {
21
        $this->setDefinition(new InputDefinition([
22
            new InputOption(
23
                'project-path',
24
                'p',
25
                InputOption::VALUE_OPTIONAL,
26
                'Path to directory of composer.json file',
27
                realpath('.')
28
            ),
29
            new InputOption(
30
                'composer',
31
                'c',
32
                InputOption::VALUE_OPTIONAL,
33
                'Path to composer executable',
34
                'composer'
35
            ),
36
            new InputOption(
37
                'no-dev',
38
                null,
39
                InputOption::VALUE_OPTIONAL,
40
                'Do not include dev dependencies',
41
                'false'
42
            ),
43
            new InputOption(
44
                'output',
45
                'o',
46
                InputOption::VALUE_OPTIONAL,
47
                'Path to file to write the used licenses to',
48
                'allowlist.txt'
49
            ),
50
            new InputOption(
51
                'force',
52
                '-f',
53
                InputOption::VALUE_NONE,
54
                'Ignore any existing allowlist file and potentially overwrite it with new content',
55
            ),
56
        ]));
57
    }
58
59
    public static function getDefaultName(): ?string
60
    {
61
        return 'init';
62
    }
63
64
    protected function execute(InputInterface $input, OutputInterface $output): int
65
    {
66
        $io = new SymfonyStyle($input, $output);
67
        $io->title('Generating list of all licenses used in the project...');
68
69
        if (! $this->canWriteToOutfile($input, $io)) {
70
            return Command::FAILURE;
71
        }
72
73
        $dependencies = $this->dependencyLoader->loadDependencies(
0 ignored issues
show
Bug introduced by
The method loadDependencies() does not exist on null. ( Ignorable by Annotation )

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

73
        /** @scrutinizer ignore-call */ 
74
        $dependencies = $this->dependencyLoader->loadDependencies(

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
74
            $input->getOption('composer'),
75
            $input->getOption('project-path'),
76
            ($input->getOption('no-dev') ?? 'true') === 'true'
77
        );
78
79
        $output = join(PHP_EOL, $this->extractLicenses($dependencies)).PHP_EOL;
80
        $io->block($output);
81
82
        if (! file_put_contents($input->getOption('output'), $output)) {
83
            $io->error("Failed to write to '".$input->getOption('output')."'.");
84
85
            return Command::FAILURE;
86
        }
87
88
        $io->success("List of used licenses written to '".$input->getOption('output')."'.");
89
90
        return Command::SUCCESS;
91
    }
92
93
    /**
94
     * @param  Dependency[]  $dependencies
95
     * @return string[]
96
     */
97
    private function extractLicenses(array $dependencies): array
98
    {
99
        $licenses = [];
100
        foreach ($dependencies as $dependency) {
101
            $licenses[] = $dependency->getLicenses();
102
        }
103
104
        $uniqueLicenses = array_values(array_unique(array_flatten($licenses)));
105
        sort($uniqueLicenses);
106
107
        return $uniqueLicenses;
108
    }
109
110
    /**
111
     * @param  InputInterface  $input
112
     * @param  SymfonyStyle  $io
113
     * @return bool
114
     */
115
    protected function canWriteToOutfile(InputInterface $input, SymfonyStyle $io): bool
116
    {
117
        if ($input->getOption('force')) {
118
            return true;
119
        }
120
121
        if (! file_exists($input->getOption('output'))) {
122
            return true;
123
        }
124
125
        $io->warning('File '.$input->getOption('output').' already exists.');
126
127
        return $io->confirm('Overwrite existing allowlist?', false);
128
    }
129
}
130