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