Swap::pass()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
ccs 0
cts 6
cp 0
crap 2
1
<?php
2
declare(strict_types=1);
3
4
namespace SlayerBirden\DataFlow\Pipe;
5
6
use SlayerBirden\DataFlow\DataBagInterface;
7
use SlayerBirden\DataFlow\IdentificationTrait;
8
use SlayerBirden\DataFlow\PipeInterface;
9
10
class Swap implements PipeInterface
11
{
12
    use IdentificationTrait;
13
    /**
14
     * @var string
15
     */
16
    private $id;
17
    /**
18
     * @var string
19
     */
20
    private $first;
21
    /**
22
     * @var string
23
     */
24
    private $second;
25
26
    public function __construct(string $id, string $first, string $second)
27
    {
28
        $this->id = $id;
29
        $this->first = $first;
30
        $this->second = $second;
31
    }
32
33
    /**
34
     * Swap one key with another.
35
     *
36
     * @param DataBagInterface $dataBag
37
     * @return DataBagInterface
38
     */
39
    public function pass(DataBagInterface $dataBag): DataBagInterface
40
    {
41
        $first = $dataBag[$this->first] ?? null;
42
        $second = $dataBag[$this->second] ?? null;
43
        $dataBag[$this->first] = $second;
44
        $dataBag[$this->second] = $first;
45
46
        return $dataBag;
47
    }
48
}
49