Passed
Push — master ( bcf385...65a347 )
by Magnar Ovedal
05:19 queued 02:32
created

Chaining::apply()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Stadly\PasswordPolice\Formatter;
6
7
use Stadly\PasswordPolice\CharTree;
8
use Stadly\PasswordPolice\Formatter;
9
10
trait Chaining
11
{
12
    /**
13
     * @var Formatter|null Next character tree formatter in the chain.
14
     */
15
    private $next;
16
17
    /**
18
     * {@inheritDoc}
19
     */
20 2
    public function setNext(?Formatter $next): void
21
    {
22 2
        $this->next = $next;
23 2
    }
24
25
    /**
26
     * {@inheritDoc}
27
     */
28 2
    public function getNext(): ?Formatter
29
    {
30 2
        return $this->next;
31
    }
32
33
    /**
34
     * @param CharTree $charTree Character tree to format.
35
     * @return CharTree The character tree formatted by the formatter chain.
36
     */
37 1
    public function apply(CharTree $charTree): CharTree
38
    {
39 1
        if ($this->next === null) {
40
            return $this->applyCurrent($charTree);
41
        } else {
42 1
            return $this->next->apply($this->applyCurrent($charTree));
43
        }
44
    }
45
46
    /**
47
     * @param CharTree $charTree Character tree to format.
48
     * @return CharTree The character tree formatted by this formatter.
49
     */
50
    abstract protected function applyCurrent(CharTree $charTree): CharTree;
51
}
52