Completed
Push — master ( 7c9114...1cd453 )
by Márk
02:22
created

Parser   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 99
Duplicated Lines 14.14 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 92.86%

Importance

Changes 3
Bugs 1 Features 1
Metric Value
wmc 25
c 3
b 1
f 1
lcom 0
cbo 1
dl 14
loc 99
ccs 39
cts 42
cp 0.9286
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B parse() 0 26 5
C normalize() 14 54 20

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 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)) {
97 1
            $numericValue = $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 108 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_int($numericValue) && (int)$value === $numericValue)
102 1
                || (is_float($numericValue) && (float)$value === $numericValue && $numericValue > 0)
103 1
            ) {
104 1
                $value = $numericValue;
105 1
            }
106 1
        }
107
108 6
        return $value;
109
    }
110
}
111