StringToBooleanTransformer   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 88
Duplicated Lines 5.68 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 5
loc 88
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 17 5
C transform() 5 31 7

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 TreeHouse\Feeder\Modifier\Data\Transformer;
4
5
use TreeHouse\Feeder\Exception\TransformationFailedException;
6
7
class StringToBooleanTransformer implements TransformerInterface
8
{
9
    /**
10
     * @var array
11
     */
12
    protected $defaultTruthyValues = [
13
        'true',
14
        'yes',
15
        'on',
16
    ];
17
18
    /**
19
     * @var array
20
     */
21
    protected $defaultFalsyValues = [
22
        'false',
23
        'no',
24
        'off',
25
    ];
26
27
    /**
28
     * @var array
29
     */
30
    protected $truthyValues;
31
32
    /**
33
     * @var array
34
     */
35
    protected $falsyValues;
36
37
    /**
38
     * @param array $truthy
39
     * @param array $falsy
40
     * @param bool  $merge
41
     */
42 36
    public function __construct(array $truthy = [], array $falsy = [], $merge = true)
43
    {
44 36
        if (empty($truthy)) {
45 36
            $truthy = $this->defaultTruthyValues;
46 36
        } elseif ($merge) {
47 2
            $truthy = array_merge($this->defaultTruthyValues, $truthy);
48 2
        }
49
50 36
        if (empty($falsy)) {
51 36
            $falsy = $this->defaultFalsyValues;
52 36
        } elseif ($merge) {
53 2
            $falsy = array_merge($this->defaultFalsyValues, $falsy);
54 2
        }
55
56 36
        $this->truthyValues = array_map('mb_strtolower', $truthy);
57 36
        $this->falsyValues = array_map('mb_strtolower', $falsy);
58 36
    }
59
60
    /**
61
     * @inheritdoc
62
     */
63 36
    public function transform($value)
64
    {
65
        // only transform when we have something to transform
66 36
        if (is_null($value)) {
67 2
            return $value;
68
        }
69
70 34
        if (is_bool($value)) {
71 4
            return $value;
72
        }
73
74 30 View Code Duplication
        if (!is_scalar($value)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
75 2
            throw new TransformationFailedException(
76 2
                sprintf('Expected a scalar value to transform, got %s instead.', var_export($value, true))
77 2
            );
78
        }
79
80 28
        if (trim($value) === '') {
81 2
            return null;
82
        }
83
84 26
        if (in_array(mb_strtolower($value), $this->truthyValues)) {
85 8
            return true;
86
        }
87
88 18
        if (in_array(mb_strtolower($value), $this->falsyValues)) {
89 8
            return false;
90
        }
91
92 10
        return (boolean) $value;
93
    }
94
}
95