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

ChainableFormatter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 8
dl 0
loc 41
ccs 10
cts 10
cp 1
rs 10
c 0
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getNext() 0 3 1
A setNext() 0 3 1
A apply() 0 6 2
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
abstract class ChainableFormatter implements WordFormatter
11
{
12
    /**
13
     * @var WordFormatter|null Next word formatter in the chain.
14
     */
15
    private $next;
16
17
    /**
18
     * {@inheritDoc}
19
     */
20 2
    public function setNext(?WordFormatter $next): void
21
    {
22 2
        $this->next = $next;
23 2
    }
24
25
    /**
26
     * {@inheritDoc}
27
     */
28 2
    public function getNext(): ?WordFormatter
29
    {
30 2
        return $this->next;
31
    }
32
33
    /**
34
     * @param iterable<string> $words Words to format.
35
     * @return Traversable<string> The words formatted by the word formatter chain. May contain duplicates.
36
     */
37 45
    public function apply(iterable $words): Traversable
38
    {
39 45
        if ($this->next === null) {
40 35
            yield from $this->applyCurrent($words);
41
        } else {
42 10
            yield from $this->next->apply($this->applyCurrent($words));
43
        }
44 45
    }
45
46
    /**
47
     * @param iterable<string> $words Words to format.
48
     * @return Traversable<string> The words formatted by this word formatter. May contain duplicates.
49
     */
50
    abstract protected function applyCurrent(iterable $words): Traversable;
51
}
52