Skip to content

Commit

Permalink
prefer exact matches even if allowMatchWithoutVersion is true in find…
Browse files Browse the repository at this point in the history
…LicenseByTitle
  • Loading branch information
KurtThiemann committed Jan 8, 2025
1 parent 8462ccf commit af5902b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/Licensee.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ public function findLicenseByTitle(string $title, bool $allowMatchWithoutVersion
if (preg_match('/' . $license->getTitleRegex() . '/i', $title)) {
return $license;
}
}

if ($allowMatchWithoutVersion) {
if ($allowMatchWithoutVersion) {
foreach (License::getAll() as $license) {
if (preg_match('/' . preg_quote($license->getNameWithoutVersion(), "/") . '/i', $title)) {
return $license;
}
Expand Down
11 changes: 11 additions & 0 deletions tests/DetectLicenseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests;

use Aternos\Licensee\Generated\Spdx;
use Aternos\Licensee\License\Text\LicenseText;
use Aternos\Licensee\Licensee;
use PHPUnit\Framework\Attributes\DataProvider;
Expand Down Expand Up @@ -96,4 +97,14 @@ public function testDetectLicense(string $license, ?string $expected): void
$this->assertEquals($expected, $match->getLicense()->getKey());
}
}

public function testPreferExactTitleMatchOverMatchWithoutVersion(): void
{
$licensee = new Licensee();
$match = $licensee->findLicenseByTitle('GNU General Public License', true);
$this->assertEquals(Spdx::GPL_2_0, $match->getSpdxId());

$match = $licensee->findLicenseByTitle('GNU General Public License v3.0', true);
$this->assertEquals(Spdx::GPL_3_0, $match->getSpdxId());
}
}

0 comments on commit af5902b

Please sign in to comment.