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

MixedCase   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 4 2
A formatWord() 0 20 6
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