Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
6 / 6 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
| Resolver | |
100.00% |
6 / 6 |
|
100.00% |
3 / 3 |
4 | |
100.00% |
1 / 1 |
| isSupported | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| resolve | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| normalize | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * This file is part of fast-forward/dev-tools. |
| 7 | * |
| 8 | * This source file is subject to the license bundled |
| 9 | * with this source code in the file LICENSE. |
| 10 | * |
| 11 | * @copyright Copyright (c) 2026 Felipe Sayão Lobato Abreu <github@mentordosnerds.com> |
| 12 | * @license https://opensource.org/licenses/MIT MIT License |
| 13 | * |
| 14 | * @see https://github.com/php-fast-forward/dev-tools |
| 15 | * @see https://github.com/php-fast-forward |
| 16 | * @see https://datatracker.ietf.org/doc/html/rfc2119 |
| 17 | */ |
| 18 | |
| 19 | namespace FastForward\DevTools\License; |
| 20 | |
| 21 | /** |
| 22 | * Resolves license identifiers to their corresponding template filenames. |
| 23 | * |
| 24 | * This class maintains a mapping of supported open-source licenses to their |
| 25 | * template files and provides methods to check support and resolve licenses. |
| 26 | */ |
| 27 | class Resolver implements ResolverInterface |
| 28 | { |
| 29 | private const array SUPPORTED_LICENSES = [ |
| 30 | 'MIT' => 'mit.txt', |
| 31 | 'BSD-2-Clause' => 'bsd-2-clause.txt', |
| 32 | 'BSD-3-Clause' => 'bsd-3-clause.txt', |
| 33 | 'Apache-2.0' => 'apache-2.0.txt', |
| 34 | 'Apache-2' => 'apache-2.0.txt', |
| 35 | 'GPL-3.0-or-later' => 'gpl-3.0-or-later.txt', |
| 36 | 'GPL-3.0' => 'gpl-3.0-or-later.txt', |
| 37 | 'GPL-3+' => 'gpl-3.0-or-later.txt', |
| 38 | 'LGPL-3.0-or-later' => 'lgpl-3.0-or-later.txt', |
| 39 | 'LGPL-3.0' => 'lgpl-3.0-or-later.txt', |
| 40 | 'LGPL-3+' => 'lgpl-3.0-or-later.txt', |
| 41 | 'MPL-2.0' => 'mpl-2.0.txt', |
| 42 | 'ISC' => 'isc.txt', |
| 43 | 'Unlicense' => 'unlicense.txt', |
| 44 | ]; |
| 45 | |
| 46 | /** |
| 47 | * Checks whether the given license identifier is supported. |
| 48 | * |
| 49 | * The check is case-insensitive and handles common license variants. |
| 50 | * |
| 51 | * @param string $license The license identifier to check |
| 52 | * |
| 53 | * @return bool True if the license is supported, false otherwise |
| 54 | */ |
| 55 | public function isSupported(string $license): bool |
| 56 | { |
| 57 | return isset(self::SUPPORTED_LICENSES[$this->normalize($license)]); |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Resolves a license identifier to its template filename. |
| 62 | * |
| 63 | * @param string $license The license identifier to resolve |
| 64 | * |
| 65 | * @return string|null The template filename if supported, or null if not |
| 66 | */ |
| 67 | public function resolve(string $license): ?string |
| 68 | { |
| 69 | $normalized = $this->normalize($license); |
| 70 | |
| 71 | if (! isset(self::SUPPORTED_LICENSES[$normalized])) { |
| 72 | return null; |
| 73 | } |
| 74 | |
| 75 | return self::SUPPORTED_LICENSES[$normalized]; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Normalizes the license identifier for comparison. |
| 80 | * |
| 81 | * @param string $license The license identifier to normalize |
| 82 | * |
| 83 | * @return string The normalized license string |
| 84 | */ |
| 85 | private function normalize(string $license): string |
| 86 | { |
| 87 | return trim($license); |
| 88 | } |
| 89 | } |