Completed
Push — master ( 04d7f3...ec52e4 )
by Márk
02:01
created

Parser::normalize()   C

Complexity

Conditions 18
Paths 9

Size

Total Lines 52
Code Lines 24

Duplication

Lines 14
Ratio 26.92 %

Code Coverage

Tests 26
CRAP Score 18.0164

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 14
loc 52
ccs 26
cts 27
cp 0.963
rs 5.8949
cc 18
eloc 24
nc 9
nop 2
crap 18.0164

How to fix   Long Method    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 Indigo\Ini;
4
5
use Indigo\Ini\Exception\ParserException;
6
7
/**
8
 * Parses an INI string.
9
 *
10
 * @author Márk Sági-Kazár <[email protected]>
11
 */
12
class Parser
13
{
14
    /**
15
     * Parses an INI string.
16
     *
17
     * @param string $ini
18
     *
19
     * @return array
20
     */
21 11
    public function parse($ini)
22
    {
23 11
        if (!is_string($ini)) {
24 1
            throw new ParserException('Cannot parse non-string INI data');
25
        }
26
27 10
        $scannerMode = defined('INI_SCANNER_TYPED') ? INI_SCANNER_TYPED : INI_SCANNER_NORMAL;
28
29 10
        $parsedIni = @parse_ini_string($ini, true, $scannerMode);
30
31 10
        if (false === $parsedIni) {
32
            $e = error_get_last();
33
            throw new ParserException('Error during parsing INI: '.$e['message']);
34
        }
35
36
        // Prior to 5.6.1 we have to do some internal parsing as well
37 10
        if (false === defined('INI_SCANNER_TYPED')) {
38
            // We cannot use INI_SCANNER_RAW by default because it is buggy under PHP 5.3.14 and 5.4.4
39
            // http://3v4l.org/m24cT
40 10
            $rawIni = @parse_ini_string($ini, true, INI_SCANNER_RAW);
41
42 10
            $parsedIni = $this->normalize($parsedIni, $rawIni);
43 10
        }
44
45 10
        return $parsedIni;
46
    }
47
48
    /**
49
     * Normalizes INI and array values.
50
     *
51
     * @param $value
52
     * @param $rawValue
53
     *
54
     * @return bool|int|null|string|array
55
     */
56 10
    protected function normalize($value, $rawValue)
57
    {
58
        // Normalize array values
59 10
        if (is_array($value)) {
60 10
            foreach ($value as $i => &$subValue) {
61 10
                $subValue = $this->normalize($subValue, $rawValue[$i]);
62 10
            }
63
64 10
            return $value;
65
        }
66
67
        // Don't normalize non-string value
68 10
        if (!is_string($value)) {
69
            return $value;
70
        }
71
72
        // Normalize true boolean value
73 View Code Duplication
        if ($value === '1'
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...
74 10
            && (strcasecmp($rawValue, 'true') === 0
75 5
                || strcasecmp($rawValue, 'yes') === 0
76 4
                || strcasecmp($rawValue, 'on') === 0)
77 10
        ) {
78 4
            return true;
79
        }
80
81
        // Normalize false boolean value
82 View Code Duplication
        if ($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...
83 10
            && (strcasecmp($rawValue, 'false') === 0
84 5
                || strcasecmp($rawValue, 'no') === 0
85 5
                || strcasecmp($rawValue, 'off') === 0)
86 10
        ) {
87 4
            return false;
88
        }
89
90
        // Normalize null value
91 7
        if ($value === '' && strcasecmp($rawValue, 'null') === 0) {
92 2
            return;
93
        }
94
95
        // Normalize numeric value
96 6
        if (is_numeric($value) && ((string) ($value + 0) === $value)) {
97 1
            $value = $value + 0;
0 ignored issues
show
Bug Compatibility introduced by
The expression $value + 0; of type integer|double adds the type double to the return on line 106 which is incompatible with the return type documented by Indigo\Ini\Parser::normalize of type boolean|integer|null|string|array.
Loading history...
98
99
            // Typed ini parsing does not support negative doubles
100
            // https://3v4l.org/ujDo1
101 1
            if (is_float($value) and $value < 0) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
102 1
                $value = (string) $value;
103 1
            }
104 1
        }
105
106 6
        return $value;
107
    }
108
}
109