Passed
Push — feature/create-console-bootstr... ( abaeda )
by Chema
04:15
created

FilenameSanitizer::sanitize()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 20
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 11
nc 4
nop 1
dl 0
loc 20
rs 9.9
c 0
b 0
f 0
ccs 10
cts 10
cp 1
crap 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
11
final class FilenameSanitizer implements FilenameSanitizerInterface
12
{
13
    public const FACADE = 'Facade';
14
    public const FACTORY = 'Factory';
15
    public const CONFIG = 'Config';
16
    public const DEPENDENCY_PROVIDER = 'DependencyProvider';
17
18
    public const EXPECTED_FILENAMES = [
19
        self::FACADE,
20
        self::FACTORY,
21
        self::CONFIG,
22
        self::DEPENDENCY_PROVIDER,
23
    ];
24
25
    /**
26
     * @return list<string>
27
     */
28 1
    public function getExpectedFilenames(): array
29
    {
30 1
        return self::EXPECTED_FILENAMES;
31
    }
32
33 24
    public function sanitize(string $filename): string
34
    {
35 24
        $percents = [];
36
37 24
        foreach (self::EXPECTED_FILENAMES as $expected) {
38 24
            $percents[$expected] = similar_text($expected, $filename, $percent);
39
        }
40
41 24
        $maxVal = max($percents);
42 24
        $maxValKeys = array_keys($percents, $maxVal, true);
43
44 24
        if (count($maxValKeys) > 1) {
45 1
            throw new RuntimeException(sprintf(
46
                'When using "%s", which filename do you mean [%s]?',
47
                $filename,
48 1
                implode(' or ', $maxValKeys)
49
            ));
50
        }
51
52 23
        return reset($maxValKeys);
53
    }
54
}
55