BoolType   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 75
Duplicated Lines 0 %

Test Coverage

Coverage 84%

Importance

Changes 0
Metric Value
eloc 24
c 0
b 0
f 0
dl 0
loc 75
ccs 21
cts 25
cp 0.84
rs 10
wmc 11

4 Methods

Rating   Name   Duplication   Size   Complexity  
A convertToPHP() 0 15 4
A __construct() 0 4 1
A get() 0 11 2
A convertToSQL() 0 15 4
1
<?php
2
3
namespace mindplay\sql\model\types;
4
5
use InvalidArgumentException;
6
use mindplay\sql\model\schema\Type;
7
8
class BoolType implements Type
9
{
10
    /**
11
     * @var mixed
12
     */
13
    private $true_value;
14
15
    /**
16
     * @var mixed
17
     */
18
    private $false_value;
19
20
    /**
21
     * @param mixed $true_value
22
     * @param mixed $false_value
23
     */
24 1
    protected function __construct($true_value = true, $false_value = false)
25
    {
26 1
        $this->true_value = $true_value;
27 1
        $this->false_value = $false_value;
28
    }
29
30
    /**
31
     * Flyweight factory method.
32
     *
33
     * @param int|string|bool $true_value  value to which boolean TRUE should be mapped
34
     * @param int|string|bool $false_value value to which boolean FALSE should be mapped
35
     *
36
     * @return self
37
     */
38 1
    public static function get($true_value = true, $false_value = false)
39
    {
40 1
        static $instances = [];
41
42 1
        $key = "{$true_value}_{$false_value}";
43
44 1
        if (!isset($instances[$key])) {
45 1
            $instances[$key] = new BoolType($true_value, $false_value);
46
        }
47
        
48 1
        return $instances[$key];
49
    }
50
51 1
    public function convertToSQL($value)
52
    {
53 1
        if ($value === null) {
54
            return null;
55
        }
56
57 1
        if ($value === true) {
58 1
            return $this->true_value;
59
        }
60
61 1
        if ($value === false) {
62 1
            return $this->false_value;
63
        }
64
65
        throw new InvalidArgumentException("unexpected value: " . print_r($value));
0 ignored issues
show
Bug introduced by
Are you sure print_r($value) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

65
        throw new InvalidArgumentException("unexpected value: " . /** @scrutinizer ignore-type */ print_r($value));
Loading history...
66
    }
67
68 1
    public function convertToPHP($value)
69
    {
70 1
        if ($value === null) {
71
            return null;
72
        }
73
74 1
        if ($value === $this->true_value) {
75 1
            return true;
76
        }
77
78 1
        if ($value === $this->false_value) {
79 1
            return false;
80
        }
81
82
        throw new InvalidArgumentException("unexpected value: " . print_r($value));
0 ignored issues
show
Bug introduced by
Are you sure print_r($value) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

82
        throw new InvalidArgumentException("unexpected value: " . /** @scrutinizer ignore-type */ print_r($value));
Loading history...
83
    }
84
}
85