Passed
Push — master ( 12ec71...6a4e7a )
by Magnar Ovedal
02:59
created

MixedCase::apply()   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 Stadly\PasswordPolice\WordFormatter;
8
use Traversable;
9
10
final class MixedCase implements WordFormatter
11
{
12
    /**
13
     * {@inheritDoc}
14
     */
15 2
    public function apply(string $word): Traversable
16
    {
17 2
        if ($word === '') {
18 2
            yield '';
19 2
            return;
20
        }
21
22 2
        $char = mb_substr($word, 0, 1);
23
24 2
        $chars = [$char];
25 2
        if ($char !== mb_strtolower($char)) {
26 2
            $chars[] = mb_strtolower($char);
27
        }
28 2
        if ($char !== mb_strtoupper($char)) {
29 2
            $chars[] = mb_strtoupper($char);
30
        }
31
32 2
        foreach ($this->apply(mb_substr($word, 1)) as $suffix) {
33 2
            foreach ($chars as $formattedChar) {
34 2
                yield $formattedChar.$suffix;
35
            }
36
        }
37 2
    }
38
}
39