FilenameSanitizer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 46
rs 10
c 0
b 0
f 0
wmc 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getExpectedFilenames() 0 3 1
A sanitize() 0 21 3
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