Completed
Push — master ( f707f5...a08928 )
by Rasmus
02:27
created

BoolType::convertToSQL()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 16
Code Lines 8

Duplication

Lines 16
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 4.25

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 16
loc 16
ccs 6
cts 8
cp 0.75
rs 9.2
cc 4
eloc 8
nc 4
nop 1
crap 4.25
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
    public function __construct($true_value = true, $false_value = false)
25
    {
26 1
        $this->true_value = $true_value;
27 1
        $this->false_value = $false_value;
28 1
    }
29
30
    /**
31
     * @return BoolType
32
     */
33 1
    public static function asInt()
34
    {
35 1
        return new BoolType(1, 0);
36
    }
37
38
    /**
39
     * @param string $true_value
40
     * @param string $false_value
41
     *
42
     * @return BoolType
43
     */
44 1
    public static function asEnum($true_value, $false_value)
45
    {
46 1
        return new BoolType($true_value, $false_value);
47
    }
48
49 1 View Code Duplication
    public function convertToSQL($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
50
    {
51 1
        if ($value === null) {
52
            return null;
53
        }
54
55 1
        if ($value === true) {
56 1
            return $this->true_value;
57
        }
58
59 1
        if ($value === false) {
60 1
            return $this->false_value;
61
        }
62
63
        throw new InvalidArgumentException("unexpected value: " . print_r($value));
64
    }
65
66 1 View Code Duplication
    public function convertToPHP($value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
    {
68 1
        if ($value === null) {
69
            return null;
70
        }
71
72 1
        if ($value === $this->true_value) {
73 1
            return true;
74
        }
75
76 1
        if ($value === $this->false_value) {
77 1
            return false;
78
        }
79
80
        throw new InvalidArgumentException("unexpected value: " . print_r($value));
81
    }
82
}
83