ini.php ➔ IniGet()   C
last analyzed

Complexity

Conditions 14
Paths 41

Size

Total Lines 78

Duplication

Lines 13
Ratio 16.67 %

Importance

Changes 0
Metric Value
cc 14
nc 41
nop 3
dl 13
loc 78
rs 5.4932
c 0
b 0
f 0

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
 * Function about ini file read, write and modify.
4
 *
5
 * @package		fwolflib
6
 * @subpackage	func
7
 * @copyright	Copyright 2008-2010, Fwolf
8
 * @author		Fwolf <[email protected]>
9
 * @since		2008-04-23
10
 * @link		http://www.php.net/manual/en/function.parse-ini-file.php
11
 */
12
13
14
require_once(dirname(__FILE__) . '/../fwolflib.php');
15
16
17
/**
18
 * Read ini file
19
 *
20
 * To retrieve global value, set $section to ' ' instead of ''.
21
 * @param	string	$filepath
22
 * @param	string	$section	If obmit, return array while each index is a section name.
23
 * @param	string	$item		If obmit, return array include all items.
24
 * @return	mixed
25
 */
26
function IniGet($filepath, $section = '', $item = '')
27
{
28
	$ini = file($filepath);
29
	if (empty($ini))
30
		return '';
31
32
	/*
33
		Result array should like this:
34
		array(
35
			global,
36
			section => array(
37
				item => value
38
				)
39
			)
40
	*/
41
	$rs = array();
42
43
    $i = 0;
44
    $s_section = '';		// Current scaning section name
45
    foreach ($ini as $line)
46
    {
47
    	$line = trim($line);
48
        // Empyt line
49
        if (empty($line)) continue;
50
        // Comments
51
        if ((';' == $line{0}) || ('#' == $line{0}))
52
        	continue;
53
54
        // Section
55
        if ('[' == $line{0})
56
        {
57
        	$s_section = substr($line, 1, -1);
58
            $i++;
59
            continue;
60
        }
61
62
        // Key-value pair
63
        list($key, $value) = explode('=', $line, 2);
64
        $key = trim($key);
65
        $value = trim($value);
66
        if (0 == $i)
67
        {
68
            // Global value
69 View Code Duplication
            if ('[]' == substr($line, -1, 2))
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...
70
            	// Array value in ini file ?
71
                $rs[' '][$key][] = $value;
72
            else
73
                $rs[' '][$key] = $value;
74
        }
75 View Code Duplication
        else
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...
76
        {
77
            // Section value
78
            if ('[]' == substr($line, -1, 2))
79
            	$rs[$s_section][$key][] = $value;
80
            else
81
                $rs[$s_section][$key] = $value;
82
        }
83
    }
84
85
    // Return values by param
86
    if (empty($section))
87
    	return $rs;
88
    elseif (empty($item))
89
    {
90
    	if (isset($rs[$section]))
91
    		return $rs[$section];
92
    	else
93
    		return '';
94
    }
95
    else
96
    {
97
    	if (isset($rs[$section][$item]))
98
    		return $rs[$section][$item];
99
    	else
100
    		return '';
101
    }
102
103
} // end of func IniGet
104
105
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
106