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

MixedCaseConverter::formatWord()   A

Complexity

Conditions 6
Paths 13

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 6

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 13
nop 1
dl 0
loc 20
ccs 13
cts 13
cp 1
crap 6
rs 9.2222
c 0
b 0
f 0
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