QNX   B
last analyzed

Complexity

Total Complexity 42

Size/Duplication

Total Lines 211
Duplicated Lines 15.64 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 33
loc 211
rs 8.295
c 0
b 0
f 0
wmc 42
lcom 1
cbo 6

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B _cpuinfo() 0 17 6
A _kernel() 0 6 2
A _distro() 0 9 2
C _uptime() 0 25 7
A _users() 0 4 1
A _hostname() 13 13 4
A _memory() 0 9 3
A _filesystems() 0 7 2
C _network() 7 30 13
A build() 13 13 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 QNX 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 QNX, and based on these observations, apply Extract Interface, too.

1
<?php
2
/**
3
 * QNX System Class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI QNX OS class
9
 * @author    Mieczyslaw Nalewaj <[email protected]>
10
 * @copyright 2012 phpSysInfo
11
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
12
 * @version   SVN: $Id: class.QNX.inc.php 687 2012-09-06 20:54:49Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * QNX sysinfo class
17
 * get all the required information from QNX system
18
 *
19
 * @category  PHP
20
 * @package   PSI QNX OS class
21
 * @author    Mieczyslaw Nalewaj <[email protected]>
22
 * @copyright 2012 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 QNX extends OS
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
     * content of the syslog
31
     *
32
     * @var array
33
     */
34
    private $_dmesg = array();
0 ignored issues
show
Unused Code introduced by
The property $_dmesg is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
35
36
    /**
37
     * call parent constructor
38
     */
39
    public function __construct()
40
    {
41
        parent::__construct();
42
    }
43
44
    /**
45
     * get the cpu information
46
     *
47
     * @return array
0 ignored issues
show
Documentation introduced by
Should the return type not be array|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
48
     */
49
    protected function _cpuinfo()
50
    {
51
        if (CommonFunctions::executeProgram('pidin', 'info', $buf)
52
           && preg_match('/^Processor\d+: (.*)/m', $buf)) {
53
            $lines = preg_split("/\n/", $buf, -1, PREG_SPLIT_NO_EMPTY);
54
            foreach ($lines as $line) {
55
                if (preg_match('/^Processor\d+: (.+)/', $line, $proc)) {
56
                    $dev = new CpuDevice();
57
                    $dev->SetModel(trim($proc[1]));
58
                    if (preg_match('/(\d+)MHz/', $proc[1], $mhz)) {
59
                        $dev->setCpuSpeed($mhz[1]);
60
                    }
61
                    $this->sys->setCpus($dev);
0 ignored issues
show
Documentation introduced by
$dev is of type object<CpuDevice>, but the function expects a object<Cpu>.

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...
62
                }
63
            }
64
        }
65
    }
66
67
    /**
68
     * QNX Version
69
     *
70
     * @return void
71
     */
72
    private function _kernel()
73
    {
74
        if (CommonFunctions::executeProgram('uname', '-rvm', $ret)) {
75
            $this->sys->setKernel($ret);
76
        }
77
    }
78
79
    /**
80
     * Distribution
81
     *
82
     * @return void
83
     */
84
    protected function _distro()
85
    {
86
        if (CommonFunctions::executeProgram('uname', '-sr', $ret))
87
            $this->sys->setDistribution($ret);
88
        else
89
            $this->sys->setDistribution('QNX');
90
91
        $this->sys->setDistributionIcon('QNX.png');
92
    }
93
94
    /**
95
     * UpTime
96
     * time the system is running
97
     *
98
     * @return void
99
     */
100
    private function _uptime()
101
    {
102
103
        if (CommonFunctions::executeProgram('pidin', 'info', $buf)
104
           && preg_match('/^.* BootTime:(.*)/', $buf, $bstart)
105
           && CommonFunctions::executeProgram('date', '', $bstop)) {
106
            /* default error handler */
107
            if (function_exists('errorHandlerPsi')) {
108
                restore_error_handler();
109
            }
110
            /* fatal errors only */
111
            $old_err_rep = error_reporting();
112
            error_reporting(E_ERROR);
113
114
            $uptime = strtotime($bstop)-strtotime($bstart[1]);
115
            if ($uptime > 0) $this->sys->setUptime($uptime);
0 ignored issues
show
Documentation introduced by
$uptime is of type integer, 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...
116
117
            /* restore error level */
118
            error_reporting($old_err_rep);
119
            /* restore error handler */
120
            if (function_exists('errorHandlerPsi')) {
121
                set_error_handler('errorHandlerPsi');
122
            }
123
        }
124
    }
125
126
    /**
127
     * Number of Users
128
     *
129
     * @return void
130
     */
131
    protected function _users()
132
    {
133
        $this->sys->setUsers(1);
134
    }
135
136
    /**
137
     * Virtual Host Name
138
     *
139
     * @return void
140
     */
141 View Code Duplication
    private function _hostname()
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...
142
    {
143
        if (PSI_USE_VHOST === true) {
144
            $this->sys->setHostname(getenv('SERVER_NAME'));
145
        } else {
146
            if (CommonFunctions::executeProgram('uname', '-n', $result, PSI_DEBUG)) {
147
                $ip = gethostbyname($result);
148
                if ($ip != $result) {
149
                    $this->sys->setHostname(gethostbyaddr($ip));
150
                }
151
            }
152
        }
153
    }
154
155
    /**
156
     *  Physical memory information and Swap Space information
157
     *
158
     *  @return void
159
     */
160
    private function _memory()
161
    {
162
        if (CommonFunctions::executeProgram('pidin', 'info', $buf)
163
           && preg_match('/^.* FreeMem:(\S+)Mb\/(\S+)Mb/', $buf, $memm)) {
164
            $this->sys->setMemTotal(1024*1024*$memm[2]);
165
            $this->sys->setMemFree(1024*1024*$memm[1]);
166
            $this->sys->setMemUsed(1024*1024*($memm[2]-$memm[1]));
167
        }
168
    }
169
170
    /**
171
     * filesystem information
172
     *
173
     * @return void
174
     */
175
    private function _filesystems()
176
    {
177
        $arrResult = Parser::df("-P 2>/dev/null");
178
        foreach ($arrResult as $dev) {
179
            $this->sys->setDiskDevices($dev);
180
        }
181
    }
182
183
    /**
184
     * network information
185
     *
186
     * @return void
187
     */
188
    private function _network()
189
    {
190
        if (CommonFunctions::executeProgram('ifconfig', '', $bufr, PSI_DEBUG)) {
191
            $lines = preg_split("/\n/", $bufr, -1, PREG_SPLIT_NO_EMPTY);
192
            $was = false;
193
            foreach ($lines as $line) {
194
                if (preg_match("/^([^\s:]+)/", $line, $ar_buf)) {
195
                    if ($was) {
196
                        $this->sys->setNetDevices($dev);
0 ignored issues
show
Bug introduced by
The variable $dev 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...
197
                    }
198
                    $dev = new NetDevice();
199
                    $dev->setName($ar_buf[1]);
200
                    $was = true;
201
                } else {
202
                    if ($was) {
203 View Code Duplication
                        if (defined('PSI_SHOW_NETWORK_INFOS') && (PSI_SHOW_NETWORK_INFOS)) {
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...
204
                            if (preg_match('/^\s+address:\s*(\S+)/i', $line, $ar_buf2)) {
205
                                    $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').preg_replace('/:/', '-', strtoupper($ar_buf2[1])));
206
                            } elseif (preg_match('/^\s+inet\s+(\S+)\s+netmask/i', $line, $ar_buf2))
207
                                $dev->setInfo(($dev->getInfo()?$dev->getInfo().';':'').$ar_buf2[1]);
208
209
                        }
210
                    }
211
                }
212
            }
213
            if ($was) {
214
                $this->sys->setNetDevices($dev);
215
            }
216
        }
217
    }
218
219
    /**
220
     * get the information
221
     *
222
     * @return Void
223
     */
224 View Code Duplication
    public function build()
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...
225
    {
226
        $this->error->addError("WARN", "The QNX version of phpSysInfo is a work in progress, some things currently don't work");
227
        $this->_distro();
228
        $this->_hostname();
229
        $this->_kernel();
230
        $this->_uptime();
231
        $this->_users();
232
        $this->_cpuinfo();
233
        $this->_memory();
234
        $this->_filesystems();
235
        $this->_network();
236
    }
237
}
238