Passed
Push — feature/add-config-files ( 837ed6...2054b6 )
by Jesús
11:11 queued 06:36
created

FilenameSanitizer   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 42
ccs 12
cts 12
cp 1
rs 10
wmc 4

2 Methods

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