Passed
Push — master ( a0b10b...5907c5 )
by Magnar Ovedal
03:17
created

Series::formatWord()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
ccs 6
cts 6
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 Series extends ChainableFormatter
11
{
12
    /**
13
     * @var WordFormatter[] Word formatters.
14
     */
15
    private $wordFormatters;
16
17
    /**
18
     * @param WordFormatter ...$wordFormatters Word formatters.
19
     */
20 3
    public function __construct(WordFormatter ...$wordFormatters)
21
    {
22 3
        $this->wordFormatters = $wordFormatters;
23 3
    }
24
25
    /**
26
     * {@inheritDoc}
27
     */
28 5
    protected function applyCurrent(iterable $words): Traversable
29
    {
30 5
        yield from $this->formatWord($words, $this->wordFormatters);
31 5
    }
32
33
    /**
34
     * @param iterable<string> $words Words to format.
35
     * @param WordFormatter[] $wordFormatters Word formatters.
36
     * @return Traversable<string> Formatted words. May contain duplicates.
37
     */
38 5
    private function formatWord(iterable $words, array $wordFormatters): Traversable
39
    {
40 5
        if ($wordFormatters === []) {
41 5
            yield from $words;
42 5
            return;
43
        }
44
45 5
        $wordFormatter = array_shift($wordFormatters);
46 5
        yield from $this->formatWord($wordFormatter->apply($words), $wordFormatters);
47 5
    }
48
}
49