FreeBSD   B
last analyzed

Complexity

Total Complexity 46

Size/Duplication

Total Lines 175
Duplicated Lines 46.29 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 81
loc 175
rs 8.3999
c 0
b 0
f 0
wmc 46
lcom 1
cbo 4

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 10 10 1
A _uptime() 0 6 1
C _network() 48 66 30
A _distroicon() 0 9 4
A _memoryadditional() 0 7 1
C _processes() 23 23 8
A build() 0 9 1

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 FreeBSD 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 FreeBSD, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * FreeBSD System Class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI FreeBSD OS class
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.FreeBSD.inc.php 696 2012-09-09 11:24:04Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * FreeBSD sysinfo class
17
 * get all the required information from FreeBSD system
18
 *
19
 * @category  PHP
20
 * @package   PSI FreeBSD OS class
21
 * @author    Michael Cramer <[email protected]>
22
 * @copyright 2009 phpSysInfo
23
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
24
 * @version   Release: 3.0
25
 * @link      http://phpsysinfo.sourceforge.net
26
 */
27
class FreeBSD extends BSDCommon
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...
28
{
29
    /**
30
     * define the regexp for log parser
31
     */
32 View Code Duplication
    public function __construct()
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...
33
    {
34
        parent::__construct();
35
        $this->setCPURegExp1("/CPU: (.*) \((.*)-MHz (.*)\)/");
36
        $this->setCPURegExp2("/(.*) ([0-9]+) ([0-9]+) ([0-9]+) ([0-9]+)/");
37
        $this->setSCSIRegExp1("/^(.*): <(.*)> .*SCSI.*device/");
38
        $this->setSCSIRegExp2("/^(da[0-9]+): (.*)MB /");
39
        $this->setPCIRegExp1("/(.*): <(.*)>(.*) pci[0-9]+$/");
40
        $this->setPCIRegExp2("/(.*): <(.*)>.* at [.0-9]+ irq/");
41
    }
42
43
    /**
44
     * UpTime
45
     * time the system is running
46
     *
47
     * @return void
48
     */
49
    private function _uptime()
50
    {
51
        $s = preg_split('/ /', $this->grabkey('kern.boottime'));
52
        $a = preg_replace('/,/', '', $s[3]);
53
        $this->sys->setUptime(time() - $a);
0 ignored issues
show
Documentation introduced by
time() - $a is of type integer|double, but the function expects a object<Interger>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
54
    }
55
56
    /**
57
     * get network information
58
     *
59
     * @return void
60
     */
61
    private function _network()
62
    {
63
        $dev = null;
0 ignored issues
show
Unused Code introduced by
$dev is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
64
        if (CommonFunctions::executeProgram('netstat', '-nibd', $netstat, PSI_DEBUG)) {
65
            $lines = preg_split("/\n/", $netstat, -1, PREG_SPLIT_NO_EMPTY);
66
            foreach ($lines as $line) {
67
                $ar_buf = preg_split("/\s+/", $line);
68
                if (!empty($ar_buf[0])) {
69
                    if (preg_match('/^<Link/i', $ar_buf[2])) {
70
                        $dev = new NetDevice();
71
                        $dev->setName($ar_buf[0]);
72
                        if (strlen($ar_buf[3]) < 17) { /* no Address */
73 View Code Duplication
                            if (isset($ar_buf[11]) && (trim($ar_buf[11]) != '')) { /* Idrop column exist*/
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
                              $dev->setTxBytes($ar_buf[9]);
75
                              $dev->setRxBytes($ar_buf[6]);
76
                              $dev->setErrors($ar_buf[4] + $ar_buf[8]);
77
                              $dev->setDrops($ar_buf[11] + $ar_buf[5]);
78
                            } else {
79
                              $dev->setTxBytes($ar_buf[8]);
80
                              $dev->setRxBytes($ar_buf[5]);
81
                              $dev->setErrors($ar_buf[4] + $ar_buf[7]);
82
                              $dev->setDrops($ar_buf[10]);
83
                            }
84 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...
85
                            if (isset($ar_buf[12]) && (trim($ar_buf[12]) != '')) { /* Idrop column exist*/
86
                              $dev->setTxBytes($ar_buf[10]);
87
                              $dev->setRxBytes($ar_buf[7]);
88
                              $dev->setErrors($ar_buf[5] + $ar_buf[9]);
89
                              $dev->setDrops($ar_buf[12] + $ar_buf[6]);
90
                            } else {
91
                              $dev->setTxBytes($ar_buf[9]);
92
                              $dev->setRxBytes($ar_buf[6]);
93
                              $dev->setErrors($ar_buf[5] + $ar_buf[8]);
94
                              $dev->setDrops($ar_buf[11]);
95
                            }
96
                        }
97 View Code Duplication
                        if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS) && (CommonFunctions::executeProgram('ifconfig', $ar_buf[0].' 2>/dev/null', $bufr2, PSI_DEBUG))) {
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...
98
                            $bufe2 = preg_split("/\n/", $bufr2, -1, PREG_SPLIT_NO_EMPTY);
99
                            foreach ($bufe2 as $buf2) {
100
                                if (preg_match('/^\s+ether\s+(\S+)/i', $buf2, $ar_buf2))
101
                                    $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').preg_replace('/:/', '-', strtoupper($ar_buf2[1])));
102
                                elseif (preg_match('/^\s+inet\s+(\S+)\s+netmask/i', $buf2, $ar_buf2))
103
                                    $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1]);
104
                                elseif ((preg_match('/^\s+inet6\s+([^\s%]+)\s+prefixlen/i', $buf2, $ar_buf2)
105
                                      || preg_match('/^\s+inet6\s+([^\s%]+)%\S+\s+prefixlen/i', $buf2, $ar_buf2))
106
                                      && ($ar_buf2[1]!="::") && !preg_match('/^fe80::/i', $ar_buf2[1]))
107
                                    $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').strtolower($ar_buf2[1]));
108
                                elseif (preg_match('/^\s+media:\s+/i', $buf2) && preg_match('/[\(\s](\d+)(G*)base/i', $buf2, $ar_buf2)) {
109
                                    if (isset($ar_buf2[2]) && strtoupper($ar_buf2[2])=="G") {
110
                                        $unit = "G";
111
                                    } else {
112
                                        $unit = "M";
113
                                    }
114
                                    if (preg_match('/[<\s]([^\s<]+)-duplex/i', $buf2, $ar_buf3))
115
                                        $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1].$unit.'b/s '.strtolower($ar_buf3[1]));
116
                                    else
117
                                        $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1].$unit.'b/s');
118
                                }
119
                            }
120
                        }
121
                        $this->sys->setNetDevices($dev);
122
                    }
123
                }
124
            }
125
        }
126
    }
127
128
    /**
129
     * get icon name and distro extended check
130
     *
131
     * @return void
132
     */
133
    private function _distroicon()
134
    {
135
        if (extension_loaded('pfSense') && CommonFunctions::rfts('/etc/version', $version, 1, 4096, false) && (trim($version) != '')) { // pfSense detection
136
            $this->sys->setDistribution('pfSense '. trim($version));
137
            $this->sys->setDistributionIcon('pfSense.png');
138
        } else {
139
            $this->sys->setDistributionIcon('FreeBSD.png');
140
        }
141
    }
142
143
    /**
144
     * extend the memory information with additional values
145
     *
146
     * @return void
147
     */
148
    private function _memoryadditional()
149
    {
150
        $pagesize = $this->grabkey("hw.pagesize");
151
        $this->sys->setMemCache($this->grabkey("vm.stats.vm.v_cache_count") * $pagesize);
152
        $this->sys->setMemApplication(($this->grabkey("vm.stats.vm.v_active_count") + $this->grabkey("vm.stats.vm.v_wire_count")) * $pagesize);
153
        $this->sys->setMemBuffer($this->sys->getMemUsed() - $this->sys->getMemApplication() - $this->sys->getMemCache());
154
    }
155
156
    /**
157
     * Processes
158
     *
159
     * @return void
160
     */
161 View Code Duplication
    protected function _processes()
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...
162
    {
163
        if (CommonFunctions::executeProgram('ps', 'aux', $bufr, PSI_DEBUG)) {
164
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
165
            $processes['*'] = 0;
0 ignored issues
show
Coding Style Comprehensibility introduced by
$processes was never initialized. Although not strictly required by PHP, it is generally a good practice to add $processes = 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...
166
            foreach ($lines as $line) {
167
                if (preg_match("/^\S+\s+\d+\s+\S+\s+\S+\s+\d+\s+\d+\s+\S+\s+(\w)/", $line, $ar_buf)) {
168
                    $processes['*']++;
169
                    $state = $ar_buf[1];
170
                    if ($state == 'L') $state = 'D'; //linux format
171
                    elseif ($state == 'I') $state = 'S';
172
                    if (isset($processes[$state])) {
173
                        $processes[$state]++;
174
                    } else {
175
                        $processes[$state] = 1;
176
                    }
177
                }
178
            }
179
            if ($processes['*'] > 0) {
180
                $this->sys->setProcesses($processes);
181
            }
182
        }
183
    }
184
185
    /**
186
     * get the information
187
     *
188
     * @see BSDCommon::build()
189
     *
190
     * @return Void
191
     */
192
    public function build()
193
    {
194
        parent::build();
195
        $this->_memoryadditional();
196
        $this->_distroicon();
197
        $this->_network();
198
        $this->_uptime();
199
        $this->_processes();
200
    }
201
}
202