Parser::df()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 210
Code Lines 2

Duplication

Lines 80
Ratio 38.1 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 80
loc 210
rs 8.2857
c 0
b 0
f 0

How to fix   Long Method   

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
 * parser Class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI
9
 * @author    Michael Cramer <[email protected]>
10
 * @copyright 2009 phpSysInfo
11
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
12
 * @version   SVN: $Id: class.Parser.inc.php 604 2012-07-10 07:31:34Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * parser class with common used parsing metods
17
 *
18
 * @category  PHP
19
 * @package   PSI
20
 * @author    Michael Cramer <[email protected]>
21
 * @copyright 2009 phpSysInfo
22
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
23
 * @version   Release: 3.0
24
 * @link      http://phpsysinfo.sourceforge.net
25
 */
26
class Parser
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
27
{
28
    /**
29
     * parsing the output of lspci command
30
     *
31
     * @return Array
32
     */
33
    public static function lspci($debug = PSI_DEBUG)
34
    {
35
        $arrResults = array();
36
        if (CommonFunctions::executeProgram("lspci", "", $strBuf, $debug)) {
37
            $arrLines = preg_split("/\n/", $strBuf, -1, PREG_SPLIT_NO_EMPTY);
38 View Code Duplication
            foreach ($arrLines as $strLine) {
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...
39
                $arrParams = preg_split('/ /', trim($strLine), 2);
40
                if (count($arrParams) == 2)
41
                   $strName = $arrParams[1];
42
                else
43
                   $strName = "unknown";
44
                $strName = preg_replace('/\(.*\)/', '', $strName);
45
                $dev = new HWDevice();
46
                $dev->setName($strName);
47
                $arrResults[] = $dev;
48
            }
49
        }
50
51
        return $arrResults;
52
    }
53
54
    /**
55
     * parsing the output of df command
56
     *
57
     * @param string $df_param additional parameter for df command
58
     *
59
     * @return array
60
     */
61
    public static function df($df_param = "")
62
    {
63
        $arrResult = array();
64
        if (CommonFunctions::executeProgram('mount', '', $mount, PSI_DEBUG)) {
65
            $mount = preg_split("/\n/", $mount, -1, PREG_SPLIT_NO_EMPTY);
66
            foreach ($mount as $mount_line) {
67
                if (preg_match("/(\S+) on ([\S ]+) type (.*) \((.*)\)/", $mount_line, $mount_buf)) {
68
                    $parm = array();
69
                    $parm['mountpoint'] = trim($mount_buf[2]);
70
                    $parm['fstype'] = $mount_buf[3];
71
                    $parm['name'] = $mount_buf[1];
72
                    if (PSI_SHOW_MOUNT_OPTION) $parm['options'] = $mount_buf[4];
73
                    $mount_parm[] = $parm;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$mount_parm was never initialized. Although not strictly required by PHP, it is generally a good practice to add $mount_parm = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
74
                } elseif (preg_match("/(\S+) is (.*) mounted on (\S+) \(type (.*)\)/", $mount_line, $mount_buf)) {
75
                    $parm = array();
76
                    $parm['mountpoint'] = trim($mount_buf[3]);
77
                    $parm['fstype'] = $mount_buf[4];
78
                    $parm['name'] = $mount_buf[1];
79
                    if (PSI_SHOW_MOUNT_OPTION) $parm['options'] = $mount_buf[2];
80
                    $mount_parm[] = $parm;
0 ignored issues
show
Bug introduced by
The variable $mount_parm does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
81
                } elseif (preg_match("/(\S+) (.*) on (\S+) \((.*)\)/", $mount_line, $mount_buf)) {
82
                    $parm = array();
83
                    $parm['mountpoint'] = trim($mount_buf[3]);
84
                    $parm['fstype'] = $mount_buf[2];
85
                    $parm['name'] = $mount_buf[1];
86
                    if (PSI_SHOW_MOUNT_OPTION) $parm['options'] = $mount_buf[4];
87
                    $mount_parm[] = $parm;
88 View Code Duplication
                } elseif (preg_match("/(\S+) on ([\S ]+) \((\S+)(,\s(.*))?\)/", $mount_line, $mount_buf)) {
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...
89
                    $parm = array();
90
                    $parm['mountpoint'] = trim($mount_buf[2]);
91
                    $parm['fstype'] = $mount_buf[3];
92
                    $parm['name'] = $mount_buf[1];
93
                    if (PSI_SHOW_MOUNT_OPTION) $parm['options'] = isset($mount_buf[5]) ? $mount_buf[5] : '';
94
                    $mount_parm[] = $parm;
95
                }
96
            }
97
        } elseif (CommonFunctions::rfts("/etc/mtab", $mount)) {
98
            $mount = preg_split("/\n/", $mount, -1, PREG_SPLIT_NO_EMPTY);
99
            foreach ($mount as $mount_line) {
100 View Code Duplication
                if (preg_match("/(\S+) (\S+) (\S+) (\S+) ([0-9]+) ([0-9]+)/", $mount_line, $mount_buf)) {
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...
101
                    $parm = array();
102
                    $mount_point = preg_replace("/\\\\040/i", ' ', $mount_buf[2]); //space as \040
103
                    $parm['mountpoint'] = $mount_point;
104
                    $parm['fstype'] = $mount_buf[3];
105
                    $parm['name'] = $mount_buf[1];
106
                    if (PSI_SHOW_MOUNT_OPTION) $parm['options'] = $mount_buf[4];
107
                    $mount_parm[] = $parm;
108
                }
109
            }
110
        }
111
        if (CommonFunctions::executeProgram('df', '-k '.$df_param, $df, PSI_DEBUG) && ($df!=="")) {
112
            $df = preg_split("/\n/", $df, -1, PREG_SPLIT_NO_EMPTY);
113
            if (PSI_SHOW_INODES) {
114
                if (CommonFunctions::executeProgram('df', '-i '.$df_param, $df2, PSI_DEBUG)) {
115
                    $df2 = preg_split("/\n/", $df2, -1, PREG_SPLIT_NO_EMPTY);
116
                    // Store inode use% in an associative array (df_inodes) for later use
117
                    foreach ($df2 as $df2_line) {
118
                        if (preg_match("/^(\S+).*\s([0-9]+)%/", $df2_line, $inode_buf)) {
119
                            $df_inodes[$inode_buf[1]] = $inode_buf[2];
0 ignored issues
show
Coding Style Comprehensibility introduced by
$df_inodes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $df_inodes = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
120
                        }
121
                    }
122
                }
123
            }
124
            foreach ($df as $df_line) {
125
                $df_buf1 = preg_split("/(\%\s)/", $df_line, 3);
126
                if (count($df_buf1) < 2) {
127
                    continue;
128
                }
129
                if (preg_match("/(.*)(\s+)(([0-9]+)(\s+)([0-9]+)(\s+)([\-0-9]+)(\s+)([0-9]+)$)/", $df_buf1[0], $df_buf2)) {
130
                    if (count($df_buf1) == 3) {
131
                        $df_buf = array($df_buf2[1], $df_buf2[4], $df_buf2[6], $df_buf2[8], $df_buf2[10], $df_buf1[2]);
132
                    } else {
133
                        $df_buf = array($df_buf2[1], $df_buf2[4], $df_buf2[6], $df_buf2[8], $df_buf2[10], $df_buf1[1]);
134
                    }
135
                    if (count($df_buf) == 6) {
136
                        $df_buf[5] = trim($df_buf[5]);
137
                        $dev = new DiskDevice();
138
                        $dev->setName(trim($df_buf[0]));
139
                        if ($df_buf[2] < 0) {
140
                            $dev->setTotal($df_buf[3] * 1024);
141
                            $dev->setUsed($df_buf[3] * 1024);
142
                        } else {
143
                            $dev->setTotal($df_buf[1] * 1024);
144
                            $dev->setUsed($df_buf[2] * 1024);
145
                            if ($df_buf[3]>0) {
146
                                $dev->setFree($df_buf[3] * 1024);
147
                            }
148
                        }
149
                        if (PSI_SHOW_MOUNT_POINT) $dev->setMountPoint($df_buf[5]);
150
151
                        $notwas = true;
152
                        if (isset($mount_parm)) {
153
                            foreach ($mount_parm as $mount_param) { //name and mountpoint find
154
                                if (($mount_param['name']===trim($df_buf[0])) && ($mount_param['mountpoint']===$df_buf[5])) {
155
                                    $dev->setFsType($mount_param['fstype']);
156 View Code Duplication
                                    if (PSI_SHOW_MOUNT_OPTION) {
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...
157
                                        if (PSI_SHOW_MOUNT_CREDENTIALS) {
158
                                            $dev->setOptions($mount_param['options']);
159
                                        } else {
160
                                            $mpo=$mount_param['options'];
161
162
                                            $mpo=preg_replace('/(^guest,)|(^guest$)|(,guest$)/i', '', $mpo);
163
                                            $mpo=preg_replace('/,guest,/i', ',', $mpo);
164
165
                                            $mpo=preg_replace('/(^user=[^,]*,)|(^user=[^,]*$)|(,user=[^,]*$)/i', '', $mpo);
166
                                            $mpo=preg_replace('/,user=[^,]*,/i', ',', $mpo);
167
168
                                            $mpo=preg_replace('/(^username=[^,]*,)|(^username=[^,]*$)|(,username=[^,]*$)/i', '', $mpo);
169
                                            $mpo=preg_replace('/,username=[^,]*,/i', ',', $mpo);
170
171
                                            $mpo=preg_replace('/(^password=[^,]*,)|(^password=[^,]*$)|(,password=[^,]*$)/i', '', $mpo);
172
                                            $mpo=preg_replace('/,password=[^,]*,/i', ',', $mpo);
173
174
                                            $dev->setOptions($mpo);
175
                                        }
176
                                    }
177
                                    $notwas = false;
178
                                    break;
179
                                }
180
                            }
181
                            if ($notwas) foreach ($mount_parm as $mount_param) { //mountpoint find
182
                                if ($mount_param['mountpoint']===$df_buf[5]) {
183
                                    $dev->setFsType($mount_param['fstype']);
184 View Code Duplication
                                    if (PSI_SHOW_MOUNT_OPTION) {
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...
185
                                        if (PSI_SHOW_MOUNT_CREDENTIALS) {
186
                                            $dev->setOptions($mount_param['options']);
187
                                        } else {
188
                                            $mpo=$mount_param['options'];
189
190
                                            $mpo=preg_replace('/(^guest,)|(^guest$)|(,guest$)/i', '', $mpo);
191
                                            $mpo=preg_replace('/,guest,/i', ',', $mpo);
192
193
                                            $mpo=preg_replace('/(^user=[^,]*,)|(^user=[^,]*$)|(,user=[^,]*$)/i', '', $mpo);
194
                                            $mpo=preg_replace('/,user=[^,]*,/i', ',', $mpo);
195
196
                                            $mpo=preg_replace('/(^username=[^,]*,)|(^username=[^,]*$)|(,username=[^,]*$)/i', '', $mpo);
197
                                            $mpo=preg_replace('/,username=[^,]*,/i', ',', $mpo);
198
199
                                            $mpo=preg_replace('/(^password=[^,]*,)|(^password=[^,]*$)|(,password=[^,]*$)/i', '', $mpo);
200
                                            $mpo=preg_replace('/,password=[^,]*,/i', ',', $mpo);
201
202
                                            $dev->setOptions($mpo);
203
                                        }
204
                                    }
205
                                    $notwas = false;
206
                                    break;
207
                                }
208
                            }
209
                        }
210
211
                        if ($notwas) {
212
                            $dev->setFsType('unknown');
213
                        }
214
215
                        if (PSI_SHOW_INODES && isset($df_inodes[trim($df_buf[0])])) {
216
                            $dev->setPercentInodesUsed($df_inodes[trim($df_buf[0])]);
217
                        }
218
                        $arrResult[] = $dev;
219
                    }
220
                }
221
            }
222
        } else {
223
            if (isset($mount_parm)) {
224
                foreach ($mount_parm as $mount_param) {
225
                    $total = disk_total_space($mount_param['mountpoint']);
226
                    if (($mount_param['fstype'] != 'none') && ($total > 0)) {
227
                        $dev = new DiskDevice();
228
                        $dev->setName($mount_param['name']);
229
                        $dev->setFsType($mount_param['fstype']);
230
231
                        if (PSI_SHOW_MOUNT_POINT) $dev->setMountPoint($mount_param['mountpoint']);
232
233
                        $dev->setTotal($total);
234
                        $free = disk_free_space($mount_param['mountpoint']);
235
                        if ($free > 0) {
236
                            $dev->setFree($free);
237
                        } else {
238
                            $free = 0;
239
                        }
240
                        if ($total > $free) $dev->setUsed($total - $free);
241
242 View Code Duplication
                        if (PSI_SHOW_MOUNT_OPTION) {
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...
243
                            if (PSI_SHOW_MOUNT_CREDENTIALS) {
244
                                $dev->setOptions($mount_param['options']);
245
                            } else {
246
                                $mpo=$mount_param['options'];
247
248
                                $mpo=preg_replace('/(^guest,)|(^guest$)|(,guest$)/i', '', $mpo);
249
                                $mpo=preg_replace('/,guest,/i', ',', $mpo);
250
251
                                $mpo=preg_replace('/(^user=[^,]*,)|(^user=[^,]*$)|(,user=[^,]*$)/i', '', $mpo);
252
                                $mpo=preg_replace('/,user=[^,]*,/i', ',', $mpo);
253
254
                                $mpo=preg_replace('/(^username=[^,]*,)|(^username=[^,]*$)|(,username=[^,]*$)/i', '', $mpo);
255
                                $mpo=preg_replace('/,username=[^,]*,/i', ',', $mpo);
256
257
                                $mpo=preg_replace('/(^password=[^,]*,)|(^password=[^,]*$)|(,password=[^,]*$)/i', '', $mpo);
258
                                $mpo=preg_replace('/,password=[^,]*,/i', ',', $mpo);
259
260
                                $dev->setOptions($mpo);
261
                            }
262
                        }
263
                        $arrResult[] = $dev;
264
                    }
265
                }
266
            }
267
        }
268
269
        return $arrResult;
270
    }
271
}
272