1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Dominikb\ComposerLicenseChecker; |
6
|
|
|
|
7
|
|
|
use Dominikb\ComposerLicenseChecker\Exceptions\LogicException; |
8
|
|
|
use Dominikb\ComposerLicenseChecker\Contracts\LicenseConstraintHandler; |
9
|
|
|
|
10
|
|
|
class ConstraintViolationDetector implements LicenseConstraintHandler |
11
|
|
|
{ |
12
|
|
|
/** @var string[] */ |
13
|
|
|
protected $blacklist = []; |
14
|
|
|
|
15
|
|
|
/** @var string[] */ |
16
|
|
|
protected $whitelist = []; |
17
|
|
|
|
18
|
|
|
public function setBlacklist(array $licenses): void |
19
|
|
|
{ |
20
|
|
|
$this->blacklist = $licenses; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function setWhitelist(array $licenses): void |
24
|
|
|
{ |
25
|
|
|
$this->whitelist = $licenses; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param Dependency[] $dependencies |
30
|
|
|
* |
31
|
|
|
* @return ConstraintViolation[] |
32
|
|
|
* @throws LogicException |
33
|
|
|
*/ |
34
|
|
|
public function detectViolations(array $dependencies): array |
35
|
|
|
{ |
36
|
|
|
$this->ensureConfigurationIsValid(); |
37
|
|
|
|
38
|
|
|
return [ |
39
|
|
|
$this->detectBlacklistViolation($dependencies), |
40
|
|
|
$this->detectWhitelistViolation($dependencies), |
41
|
|
|
]; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @throws LogicException |
46
|
|
|
*/ |
47
|
|
|
public function ensureConfigurationIsValid(): void |
48
|
|
|
{ |
49
|
|
|
$overlap = array_intersect($this->blacklist, $this->whitelist); |
50
|
|
|
|
51
|
|
|
if (count($overlap) > 0) { |
52
|
|
|
$invalidLicenseConditionals = sprintf('"%s"', implode('", "', $overlap)); |
53
|
|
|
throw new LogicException("Licenses must not be black- and whitelisted at the same time: ${invalidLicenseConditionals}"); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param Dependency[] $dependencies |
59
|
|
|
*/ |
60
|
|
|
private function detectBlacklistViolation(array $dependencies): ConstraintViolation |
61
|
|
|
{ |
62
|
|
|
$violation = new ConstraintViolation('Blacklisted license found!'); |
63
|
|
|
|
64
|
|
|
if (! empty($this->blacklist)) { |
65
|
|
|
foreach ($dependencies as $dependency) { |
66
|
|
|
if ($this->allLicensesOnList($dependency->getLicenses(), $this->blacklist)) { |
67
|
|
|
$violation->add($dependency); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $violation; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param Dependency[] $dependencies |
77
|
|
|
*/ |
78
|
|
|
private function detectWhitelistViolation(array $dependencies): ConstraintViolation |
79
|
|
|
{ |
80
|
|
|
$violation = new ConstraintViolation('Non white-listed license found!'); |
81
|
|
|
|
82
|
|
|
if (! empty($this->whitelist)) { |
83
|
|
|
foreach ($dependencies as $dependency) { |
84
|
|
|
if (! $this->anyLicenseOnList($dependency->getLicenses(), $this->whitelist)) { |
85
|
|
|
$violation->add($dependency); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $violation; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function allLicensesOnList(array $licenses, array $list): bool |
94
|
|
|
{ |
95
|
|
|
return count(array_intersect($licenses, $list)) === count($licenses); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function anyLicenseOnList(array $licenses, array $list): bool |
99
|
|
|
{ |
100
|
|
|
return count(array_intersect($licenses, $list)) > 0; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|