Conditional::ifThenElse()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 3
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Marcosh\Effector;
6
7
final class Conditional
8
{
9
    /**
10
     * @var callable
11
     */
12
    private $if;
13
14
    /**
15
     * @var callable
16
     */
17
    private $then;
18
19
    /**
20
     * @var callable
21
     */
22
    private $else;
23
24
    private function __construct(callable $if, callable $then, callable $else)
25
    {
26
        $this->if = $if;
27
        $this->then = $then;
28
        $this->else = $else;
29
    }
30
31
    public static function ifThenElse(callable $if, callable $then, callable $else)
32
    {
33
        return new self($if, $then, $else);
34
    }
35
36
    public function __invoke(... $args)
37
    {
38
        if (($this->if)(... $args)) {
39
            return ($this->then)(... $args);
40
        } else {
41
            return ($this->else)(... $args);
42
        }
43
    }
44
}
45