Test Failed
Branch issue-17-add-allow-option (e1563c)
by Dominik
11:04
created

ConstraintViolationDetector::detectViolations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 5
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Dominikb\ComposerLicenseChecker;
6
7
use Dominikb\ComposerLicenseChecker\Contracts\LicenseConstraintHandler;
8
use Dominikb\ComposerLicenseChecker\Exceptions\LogicException;
9
10
class ConstraintViolationDetector implements LicenseConstraintHandler
11
{
12
    /** @var string[] */
13
    protected $blocklist = [];
14
15
    /** @var string[] */
16
    protected $allowlist = [];
17
18
    /** @var Dependency[] */
19
    private $alwaysAllowed = [];
20
21
    public function setBlocklist(array $licenses): void
22
    {
23
        $this->blocklist = $licenses;
24
    }
25
26
    public function setAllowlist(array $licenses): void
27
    {
28
        $this->allowlist = $licenses;
29
    }
30
31
    public function allow($dependencies): void
32
    {
33
        if (! is_array($dependencies)) {
34
            $dependencies = [$dependencies];
35
        }
36
37
        foreach ($dependencies as $allowed) {
38
            $this->alwaysAllowed[] = $allowed;
39
        }
40
    }
41
42
    /**
43
     * @param Dependency[] $dependencies
44
     *
45
     * @return ConstraintViolation[]
46
     * @throws LogicException
47
     */
48
    public function detectViolations(array $dependencies): array
49
    {
50
        $this->ensureConfigurationIsValid();
51
52
        $possibleViolators = $this->exceptAllowed($dependencies);
53
54
        return [
55
            $this->detectBlocklistViolation($possibleViolators),
56
            $this->detectAllowlistViolation($possibleViolators),
57
        ];
58
    }
59
60
    /**
61
     * @throws LogicException
62
     */
63
    public function ensureConfigurationIsValid(): void
64
    {
65
        $overlap = array_intersect($this->blocklist, $this->allowlist);
66
67
        if (count($overlap) > 0) {
68
            $invalidLicenseConditionals = sprintf('"%s"', implode('", "', $overlap));
69
            throw new LogicException("Licenses must not be on the block- and allowlist at the same time: ${invalidLicenseConditionals}");
70
        }
71
    }
72
73
    /**
74
     * @param Dependency[] $dependencies
75
     */
76
    private function detectBlocklistViolation(array $dependencies): ConstraintViolation
77
    {
78
        $violation = new ConstraintViolation('Blocked license found!');
79
80
        if (! empty($this->blocklist)) {
81
            foreach ($dependencies as $dependency) {
82
                if ($this->allLicensesOnList($dependency->getLicenses(), $this->blocklist)) {
83
                    $violation->add($dependency);
84
                }
85
            }
86
        }
87
88
        return $violation;
89
    }
90
91
    /**
92
     * @param Dependency[] $dependencies
93
     */
94
    private function detectAllowlistViolation(array $dependencies): ConstraintViolation
95
    {
96
        $violation = new ConstraintViolation('Unallowed license found!');
97
98
        if (! empty($this->allowlist)) {
99
            foreach ($dependencies as $dependency) {
100
                if (! $this->anyLicenseOnList($dependency->getLicenses(), $this->allowlist)) {
101
                    $violation->add($dependency);
102
                }
103
            }
104
        }
105
106
        return $violation;
107
    }
108
109
    private function allLicensesOnList(array $licenses, array $list): bool
110
    {
111
        return count(array_intersect($licenses, $list)) === count($licenses);
112
    }
113
114
    private function anyLicenseOnList(array $licenses, array $list): bool
115
    {
116
        return count(array_intersect($licenses, $list)) > 0;
117
    }
118
119
    /**
120
     * @param Dependency[] $dependencies
121
     *
122
     * @return Dependency[]
123
     */
124
    private function exceptAllowed(array $dependencies): array
125
    {
126
        $possiblyViolating = [];
127
128
        if (empty($this->alwaysAllowed)) {
129
            return $dependencies;
130
        }
131
132
        foreach ($dependencies as $dependency) {
133
            foreach ($this->alwaysAllowed as $allowedDependency) {
134
                if ($this->matches($allowedDependency, $dependency)) {
135
                    continue 2; // Outer for: test next dependency
136
                }
137
            }
138
139
            $possiblyViolating[] = $dependency;
140
        }
141
142
        return $possiblyViolating;
143
    }
144
145
    /**
146
     * Determine if the $original author and package name match for $tryMatch.
147
     * An empty string for either the author or package gets interpreted as a wilcard.
148
     *
149
     * @param Dependency $original
150
     * @param Dependency $tryMatch
151
     *
152
     * @return bool
153
     */
154
    private function matches(Dependency $original, Dependency $tryMatch): bool
155
    {
156
        if ($original->getAuthorName()) {
157
            if (! ($original->getAuthorName() === $tryMatch->getAuthorName())) {
158
                return false;
159
            }
160
        }
161
162
        if ($original->getPackageName()) {
163
            if (! ($original->getPackageName() === $tryMatch->getPackageName())) {
164
                return false;
165
            }
166
        }
167
168
        return true;
169
    }
170
}
171