FilenameSanitizer::getExpectedFilenames()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Console\Domain\FilenameSanitizer;
6
7
use RuntimeException;
8
9
use function count;
10
use function sprintf;
11
12
final class FilenameSanitizer implements FilenameSanitizerInterface
13
{
14
    public const FACADE = 'Facade';
15
16
    public const FACTORY = 'Factory';
17
18
    public const CONFIG = 'Config';
19
20
    public const PROVIDER = 'Provider';
21
22
    public const EXPECTED_FILENAMES = [
23
        self::FACADE,
24
        self::FACTORY,
25
        self::CONFIG,
26
        self::PROVIDER,
27
    ];
28
29
    /**
30
     * @return list<string>
31
     */
32
    public function getExpectedFilenames(): array
33
    {
34
        return self::EXPECTED_FILENAMES;
35
    }
36
37
    public function sanitize(string $filename): string
38
    {
39
        $percents = [];
40
41
        foreach (self::EXPECTED_FILENAMES as $expected) {
42
            $percents[$expected] = similar_text($expected, $filename);
43
        }
44
45
        $maxVal = max($percents);
46
        $maxValKeys = array_keys($percents, $maxVal, true);
47
48
        if (count($maxValKeys) > 1) {
49
            throw new RuntimeException(sprintf(
50
                'When using "%s", which filename do you mean [%s]?',
51
                $filename,
52
                implode(' or ', $maxValKeys),
53
            ));
54
        }
55
56
        /** @psalm-suppress RedundantCast */
57
        return (string)reset($maxValKeys);
58
    }
59
}
60