StringUtil   B
last analyzed

Complexity

Total Complexity 46

Size/Duplication

Total Lines 145
Duplicated Lines 44.14 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 94.29%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 46
c 1
b 0
f 1
lcom 0
cbo 2
dl 64
loc 145
ccs 66
cts 70
cp 0.9429
rs 8.3999

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getLength() 0 16 4
B parseString() 0 20 7
B parseInteger() 15 15 6
B parseFloat() 15 15 6
A getMaxWordLength() 17 17 3
A getMaxLineLength() 17 17 3
C parseBoolean() 0 32 16

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like StringUtil often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use StringUtil, and based on these observations, apply Extract Interface, too.

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