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

ChainableFormatter::getNext()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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
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