Skip to content

Commit af5902b

Browse files
committed
prefer exact matches even if allowMatchWithoutVersion is true in findLicenseByTitle
1 parent 8462ccf commit af5902b

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

src/Licensee.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,10 @@ public function findLicenseByTitle(string $title, bool $allowMatchWithoutVersion
4545
if (preg_match('/' . $license->getTitleRegex() . '/i', $title)) {
4646
return $license;
4747
}
48+
}
4849

49-
if ($allowMatchWithoutVersion) {
50+
if ($allowMatchWithoutVersion) {
51+
foreach (License::getAll() as $license) {
5052
if (preg_match('/' . preg_quote($license->getNameWithoutVersion(), "/") . '/i', $title)) {
5153
return $license;
5254
}

tests/DetectLicenseTest.php

+11
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Tests;
44

5+
use Aternos\Licensee\Generated\Spdx;
56
use Aternos\Licensee\License\Text\LicenseText;
67
use Aternos\Licensee\Licensee;
78
use PHPUnit\Framework\Attributes\DataProvider;
@@ -96,4 +97,14 @@ public function testDetectLicense(string $license, ?string $expected): void
9697
$this->assertEquals($expected, $match->getLicense()->getKey());
9798
}
9899
}
100+
101+
public function testPreferExactTitleMatchOverMatchWithoutVersion(): void
102+
{
103+
$licensee = new Licensee();
104+
$match = $licensee->findLicenseByTitle('GNU General Public License', true);
105+
$this->assertEquals(Spdx::GPL_2_0, $match->getSpdxId());
106+
107+
$match = $licensee->findLicenseByTitle('GNU General Public License v3.0', true);
108+
$this->assertEquals(Spdx::GPL_3_0, $match->getSpdxId());
109+
}
99110
}

0 commit comments

Comments
 (0)