Passed
Push — feature/add-gacela-cli ( 2018fc )
by Chema
05:13 queued 15s
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 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 20
ccs 10
cts 10
cp 1
crap 3
rs 9.9
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