|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Dominikb\ComposerLicenseChecker; |
|
6
|
|
|
|
|
7
|
|
|
use Dominikb\ComposerLicenseChecker\Contracts\DependencyLoaderAware; |
|
8
|
|
|
use Dominikb\ComposerLicenseChecker\Traits\DependencyLoaderAwareTrait; |
|
9
|
|
|
use Symfony\Component\Console\Helper\Table; |
|
10
|
|
|
use Symfony\Component\Console\Command\Command; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
|
12
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
13
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
14
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
15
|
|
|
use Dominikb\ComposerLicenseChecker\Contracts\LicenseLookupAware; |
|
16
|
|
|
use Dominikb\ComposerLicenseChecker\Traits\LicenseLookupAwareTrait; |
|
17
|
|
|
|
|
18
|
|
|
class ReportCommand extends Command implements LicenseLookupAware, DependencyLoaderAware |
|
19
|
|
|
{ |
|
20
|
|
|
use LicenseLookupAwareTrait, DependencyLoaderAwareTrait; |
|
21
|
|
|
|
|
22
|
|
|
const LINES_BEFORE_DEPENDENCY_VERSIONS = 2; |
|
23
|
|
|
|
|
24
|
|
|
protected static $defaultName = 'report'; |
|
25
|
|
|
|
|
26
|
|
|
protected function configure() |
|
27
|
|
|
{ |
|
28
|
|
|
$this->setDefinition(new InputDefinition([ |
|
29
|
|
|
new InputOption( |
|
30
|
|
|
'project-path', |
|
31
|
|
|
'p', |
|
32
|
|
|
InputOption::VALUE_OPTIONAL, |
|
33
|
|
|
'Path to directory of composer.json file', |
|
34
|
|
|
realpath('.') |
|
35
|
|
|
), |
|
36
|
|
|
new InputOption( |
|
37
|
|
|
'composer', |
|
38
|
|
|
'c', |
|
39
|
|
|
InputOption::VALUE_OPTIONAL, |
|
40
|
|
|
'Path to composer executable', |
|
41
|
|
|
realpath('./vendor/bin/composer') |
|
42
|
|
|
) |
|
43
|
|
|
])); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
47
|
|
|
{ |
|
48
|
|
|
$dependencies = $this->dependencyLoader->loadDependencies( |
|
49
|
|
|
$input->getOption('composer'), |
|
50
|
|
|
$input->getOption('project-path') |
|
51
|
|
|
); |
|
52
|
|
|
|
|
53
|
|
|
$groupedByLicense = $this->groupDependenciesByLicense($dependencies); |
|
54
|
|
|
|
|
55
|
|
|
$licenses = $this->lookUpLicenses(array_keys($groupedByLicense)); |
|
56
|
|
|
|
|
57
|
|
|
/** @var License $license */ |
|
58
|
|
|
foreach ($licenses as $license) { |
|
59
|
|
|
$usageCount = count($groupedByLicense[$license->getShortName()]); |
|
60
|
|
|
$headline = sprintf("\nCount %d - %s (%s)", $usageCount, $license->getShortName(), $license->getSource()); |
|
61
|
|
|
$output->writeln($headline); |
|
62
|
|
|
$licenseTable = new Table($output); |
|
63
|
|
|
$licenseTable->setHeaders(['CAN', 'CAN NOT', 'MUST']); |
|
64
|
|
|
|
|
65
|
|
|
$can = $license->getCan(); |
|
66
|
|
|
$cannot = $license->getCannot(); |
|
67
|
|
|
$must = $license->getMust(); |
|
68
|
|
|
$columnWidth = max(count($can), count($cannot), count($must)); |
|
69
|
|
|
|
|
70
|
|
|
$can = array_pad($can, $columnWidth, null); |
|
71
|
|
|
$cannot = array_pad($cannot, $columnWidth, null); |
|
72
|
|
|
$must = array_pad($must, $columnWidth, null); |
|
73
|
|
|
|
|
74
|
|
|
$inlineHeading = function ($key) { |
|
75
|
|
|
return is_string($key) ? $key : ''; |
|
76
|
|
|
}; |
|
77
|
|
|
|
|
78
|
|
|
$can = array_map_keys($can, $inlineHeading); |
|
79
|
|
|
$cannot = array_map_keys($cannot, $inlineHeading); |
|
80
|
|
|
$must = array_map_keys($must, $inlineHeading); |
|
81
|
|
|
|
|
82
|
|
|
for ($i = 0; $i < $columnWidth; $i++) { |
|
83
|
|
|
$licenseTable->addRow([ |
|
84
|
|
|
'CAN' => $can[$i], |
|
85
|
|
|
'CANNOT' => $cannot[$i], |
|
86
|
|
|
'MUST' => $must[$i], |
|
87
|
|
|
]); |
|
88
|
|
|
} |
|
89
|
|
|
$licenseTable->render(); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param Dependency[] $dependencies |
|
95
|
|
|
* |
|
96
|
|
|
* @return array |
|
97
|
|
|
*/ |
|
98
|
|
|
private function groupDependenciesByLicense(array $dependencies) |
|
99
|
|
|
{ |
|
100
|
|
|
$grouped = []; |
|
101
|
|
|
|
|
102
|
|
|
foreach ($dependencies as $dependency) { |
|
103
|
|
|
[$license] = $dependency->getLicenses(); |
|
|
|
|
|
|
104
|
|
|
|
|
105
|
|
|
if (! isset($grouped[$license])) { |
|
106
|
|
|
$grouped[$license] = []; |
|
107
|
|
|
} |
|
108
|
|
|
$grouped[$license][] = $dependency; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $grouped; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
private function lookUpLicenses(array $licenses) |
|
115
|
|
|
{ |
|
116
|
|
|
$lookedUp = []; |
|
117
|
|
|
foreach ($licenses as $license) { |
|
118
|
|
|
$lookedUp[$license] = $this->licenseLookup->lookUp($license); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
return $lookedUp; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.