Completed
Push — master ( f5e5fb...7d0a28 )
by Rasmus
02:24
created

BoolType   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 77
Duplicated Lines 41.56 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 84.62%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 11
c 2
b 0
f 0
lcom 1
cbo 0
dl 32
loc 77
ccs 22
cts 26
cp 0.8462
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A get() 0 12 2
A convertToSQL() 16 16 4
A convertToPHP() 16 16 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 1
    }
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 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...
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));
66
    }
67
68 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...
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));
83
    }
84
}
85