StringUtil::parseBoolean()   C
last analyzed

Complexity

Conditions 16
Paths 13

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 16

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 32
ccs 20
cts 20
cp 1
rs 5.0151
cc 16
eloc 21
nc 13
nop 2
crap 16

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
/*
4
 * This file is part of the webmozart/console package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Webmozart\Console\Util;
13
14
use Webmozart\Console\Api\Args\Format\InvalidValueException;
15
use Webmozart\Console\Api\Formatter\Formatter;
16
17
/**
18
 * @since  1.0
19
 *
20
 * @author Bernhard Schussek <[email protected]>
21
 */
22
class StringUtil
0 ignored issues
show
Coding Style introduced by
Since you have declared the constructor as private, maybe you should also declare the class as final.
Loading history...
23
{
24 277
    public static function parseString($value, $nullable = true)
25
    {
26 277
        if ($nullable && (null === $value || 'null' === $value)) {
27 10
            return null;
28
        }
29
30 267
        if (null === $value) {
31 2
            return 'null';
32
        }
33
34 265
        if (true === $value) {
35 1
            return 'true';
36
        }
37
38 264
        if (false === $value) {
39 1
            return 'false';
40
        }
41
42 263
        return (string) $value;
43
    }
44
45 28
    public static function parseBoolean($value, $nullable = true)
46
    {
47 28
        if ($nullable && (null === $value || 'null' === $value)) {
48 4
            return null;
49
        }
50
51 24
        if (is_bool($value)) {
52 2
            return $value;
53
        }
54
55 22
        if (is_string($value) || is_int($value)) {
56 20
            switch ((string) $value) {
57 20
                case '':
58 19
                case 'false':
59 16
                case '0':
60 14
                case 'no':
61 13
                case 'off':
62 8
                    return false;
63
64 12
                case 'true':
65 9
                case '1':
66 7
                case 'yes':
67 6
                case 'on':
68 7
                    return true;
69
            }
70
        }
71
72 7
        throw new InvalidValueException(sprintf(
73 7
            'The value "%s" cannot be parsed as boolean.',
74
            $value
75
        ));
76
    }
77
78 36 View Code Duplication
    public static function parseInteger($value, $nullable = true)
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...
79
    {
80 36
        if ($nullable && (null === $value || 'null' === $value)) {
81 4
            return null;
82
        }
83
84 32
        if (is_numeric($value) || is_bool($value)) {
85 20
            return (int) $value;
86
        }
87
88 12
        throw new InvalidValueException(sprintf(
89 12
            'The value "%s" cannot be parsed as integer.',
90
            $value
91
        ));
92
    }
93
94 30 View Code Duplication
    public static function parseFloat($value, $nullable = true)
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...
95
    {
96 30
        if ($nullable && (null === $value || 'null' === $value)) {
97 4
            return null;
98
        }
99
100 26
        if (is_numeric($value) || is_bool($value)) {
101 14
            return (float) $value;
102
        }
103
104 12
        throw new InvalidValueException(sprintf(
105 12
            'The value "%s" cannot be parsed as float.',
106
            $value
107
        ));
108
    }
109
110 21
    public static function getLength($string, Formatter $formatter = null)
111
    {
112 21
        if ($formatter) {
113 21
            $string = $formatter->removeFormat($string);
114
        }
115
116 21
        if (!function_exists('mb_strwidth')) {
117
            return strlen($string);
118
        }
119
120 21
        if (false === $encoding = mb_detect_encoding($string)) {
121
            return strlen($string);
122
        }
123
124 21
        return mb_strwidth($string, $encoding);
125
    }
126
127 14 View Code Duplication
    public static function getMaxWordLength($string, Formatter $formatter = null)
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...
128
    {
129 14
        if ($formatter) {
130 14
            $string = $formatter->removeFormat($string);
131
        }
132
133 14
        $maxLength = 0;
134 14
        $words = preg_split('/\s+/', $string);
135
136 14
        foreach ($words as $word) {
137
            // No need to pass the formatter because the tags are already
138
            // removed
139 14
            $maxLength = max($maxLength, self::getLength($word));
140
        }
141
142 14
        return $maxLength;
143
    }
144
145 14 View Code Duplication
    public static function getMaxLineLength($string, Formatter $formatter = null)
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...
146
    {
147 14
        if ($formatter) {
148 14
            $string = $formatter->removeFormat($string);
149
        }
150
151 14
        $maxLength = 0;
152 14
        $lines = explode("\n", $string);
153
154 14
        foreach ($lines as $word) {
155
            // No need to pass the formatter because the tags are already
156
            // removed
157 14
            $maxLength = max($maxLength, self::getLength($word));
158
        }
159
160 14
        return $maxLength;
161
    }
162
163
    private function __construct()
164
    {
165
    }
166
}
167