Completed
Push — master ( 6a4e7a...d894f8 )
by Magnar Ovedal
04:34
created

MixedCase::apply()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\PasswordPolice\WordFormatter;
6
7
use Stadly\PasswordPolice\WordFormatter;
8
use Traversable;
9
10
final class MixedCase implements WordFormatter
11
{
12
    /**
13
     * {@inheritDoc}
14
     */
15 3
    public function apply(iterable $words): Traversable
16
    {
17 3
        foreach ($words as $word) {
18 3
            yield from $this->formatWord($word);
19
        }
20 3
    }
21
22
    /**
23
     * @param string $word Word to format.
24
     * @return Traversable<string> Formatted words. May contain duplicates.
25
     */
26 3
    private function formatWord(string $word): Traversable
27
    {
28 3
        if ($word === '') {
29 3
            yield '';
30 3
            return;
31
        }
32
33 3
        $char = mb_substr($word, 0, 1);
34
35 3
        $chars = [$char];
36 3
        if ($char !== mb_strtolower($char)) {
37 3
            $chars[] = mb_strtolower($char);
38
        }
39 3
        if ($char !== mb_strtoupper($char)) {
40 3
            $chars[] = mb_strtoupper($char);
41
        }
42
43 3
        foreach ($this->formatWord(mb_substr($word, 1)) as $suffix) {
44 3
            foreach ($chars as $formattedChar) {
45 3
                yield $formattedChar.$suffix;
46
            }
47
        }
48 3
    }
49
}
50