Completed
Push — master ( 9b80be...1d4d60 )
by Magnar Ovedal
08:44 queued 05:57
created

MixedCaseConverter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 37
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A formatWord() 0 20 6
A applyCurrent() 0 4 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\PasswordPolice\WordFormatter;
6
7
use Traversable;
8
9
final class MixedCaseConverter extends ChainableFormatter
10
{
11
    /**
12
     * @param iterable<string> $words Words to format.
13
     * @return Traversable<string> Variants of the words with all combinations of upper case and lower case characters.
14
     */
15 4
    protected function applyCurrent(iterable $words): Traversable
16
    {
17 4
        foreach ($words as $word) {
18 4
            yield from $this->formatWord($word);
19
        }
20 4
    }
21
22
    /**
23
     * @param string $word Word to format.
24
     * @return Traversable<string> Variants of the word with all combinations of upper case and lower case characters.
25
     */
26 4
    private function formatWord(string $word): Traversable
27
    {
28 4
        if ($word === '') {
29 4
            yield '';
30 4
            return;
31
        }
32
33 4
        $char = mb_substr($word, 0, 1);
34
35 4
        $chars = [$char];
36 4
        if ($char !== mb_strtolower($char)) {
37 4
            $chars[] = mb_strtolower($char);
38
        }
39 4
        if ($char !== mb_strtoupper($char)) {
40 4
            $chars[] = mb_strtoupper($char);
41
        }
42
43 4
        foreach ($this->formatWord(mb_substr($word, 1)) as $suffix) {
44 4
            foreach ($chars as $formattedChar) {
45 4
                yield $formattedChar.$suffix;
46
            }
47
        }
48 4
    }
49
}
50