BooleanModule::makeBoolean()   B
last analyzed

Complexity

Conditions 11
Paths 8

Size

Total Lines 32
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 12.5763

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 17
c 1
b 0
f 0
dl 0
loc 32
ccs 13
cts 17
cp 0.7647
rs 7.3166
cc 11
nc 8
nop 1
crap 12.5763

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace PhpWinTools\Support;
4
5
class BooleanModule
6
{
7 1
    public static function makeBoolean($value): bool
8
    {
9 1
        if ($value === true || $value === false) {
10 1
            return $value;
11
        }
12
13 1
        if (is_array($value)) {
14
            return !empty($value);
15
        }
16
17 1
        if ($value === 1 || $value === '1') {
18 1
            return true;
19
        }
20
21 1
        if ($value === 0 || $value === '0') {
22 1
            return false;
23
        }
24
25 1
        $value = strtolower($value);
26
27 1
        if ($value === 'true') {
28 1
            return true;
29
        }
30
31 1
        if ($value === 'false') {
32 1
            return false;
33
        }
34
35
        if (trim($value) !== '') {
36
            return true;
37
        } else {
38
            return false;
39
        }
40
    }
41
}
42