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

Series   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 9
dl 0
loc 37
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A formatWord() 0 9 2
A applyCurrent() 0 3 1
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