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

Series   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 38
ccs 15
cts 15
cp 1
rs 10
c 0
b 0
f 0
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A apply() 0 3 1
A __construct() 0 7 2
A formatWord() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\PasswordPolice\WordFormatter;
6
7
use InvalidArgumentException;
8
use Stadly\PasswordPolice\WordFormatter;
9
use Traversable;
10
11
final class Series implements WordFormatter
12
{
13
    /**
14
     * @var WordFormatter[] Word formatters.
15
     */
16
    private $wordFormatters;
17
18
    /**
19
     * @param WordFormatter ...$wordFormatters Word formatters.
20
     */
21 3
    public function __construct(WordFormatter ...$wordFormatters)
22
    {
23 3
        if ($wordFormatters === []) {
24 1
            throw new InvalidArgumentException('At least one word formatter must be specified.');
25
        }
26
27 2
        $this->wordFormatters = $wordFormatters;
28 2
    }
29
30
    /**
31
     * {@inheritDoc}
32
     */
33 2
    public function apply(string $word): Traversable
34
    {
35 2
        yield from $this->formatWord($word, $this->wordFormatters);
36 2
    }
37
38 2
    private function formatWord(string $word, array $wordFormatters): Traversable
39
    {
40 2
        $wordFormatter = array_shift($wordFormatters);
41
42 2
        if ($wordFormatters === []) {
43 2
            yield from $wordFormatter->apply($word);
44 2
            return;
45
        }
46
47 1
        foreach ($wordFormatter->apply($word) as $formatted) {
48 1
            yield from $this->formatWord($formatted, $wordFormatters);
49
        }
50 1
    }
51
}
52