FilenameSanitizer   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 4
eloc 23
c 1
b 1
f 0
dl 0
loc 48
rs 10

2 Methods

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