3 declare(strict_types=1);
7 function findLicenseFile(string $packageName, string $packagePath): string
9 $licenseNameOptions = [
10 'license', 'LICENSE', 'License',
11 'license.*', 'LICENSE.*', 'License.*',
12 'license-*.*', 'LICENSE-*.*', 'License-*.*',
14 $packageDir = dirname($packagePath);
17 foreach ($licenseNameOptions as $option) {
18 $search = glob("{$packageDir}/$option");
19 array_push($foundLicenses, ...$search);
22 if (count($foundLicenses) > 1) {
23 warn("Package {$packageName}: more than one license file found");
26 if (count($foundLicenses) > 0) {
27 $fileName = basename($foundLicenses[0]);
28 return "{$packageDir}/{$fileName}";
31 warn("Package {$packageName}: no license files found");
35 function findCopyright(string $licenseFile): string
37 $fileContents = file_get_contents($licenseFile);
38 $pattern = '/^.*?copyright (\(c\)|\d{4})[\s\S]*?(\n\n|\.\n)/mi';
40 preg_match($pattern, $fileContents, $matches);
41 $copyright = trim($matches[0] ?? '');
43 if (str_contains($copyright, 'i.e.')) {
47 $emailPattern = '/([a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,})/i';
48 return preg_replace_callback($emailPattern, obfuscateEmail(...), $copyright);
51 function obfuscateEmail(array $matches): string
53 return preg_replace('/[^@.]/', '*', $matches[1]);
56 function warn(string $text): void
59 $warnings[] = "WARN:" . $text;
62 function getWarnings(): array