OHM   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 120
Duplicated Lines 56.67 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 68
loc 120
rs 10
c 0
b 0
f 0
wmc 21
lcom 1
cbo 4

6 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 24 24 4
A _temperature() 11 11 4
A _voltage() 11 11 4
A _fans() 11 11 4
A _power() 11 11 4
A build() 0 7 1

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
 * Open Hardware Monitor sensor class
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI_Sensor
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.ohm.inc.php 661 2012-08-27 11:26:39Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * getting information from Open Hardware Monitor
17
 *
18
 * @category  PHP
19
 * @package   PSI_Sensor
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 OHM extends Sensors
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
     * holds the COM object that we pull all the WMI data from
30
     *
31
     * @var Object
32
     */
33
    private $_buf = array();
34
35
    /**
36
     * fill the private content var
37
     */
38 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...
39
    {
40
        parent::__construct();
41
        $_wmi = null;
42
        // don't set this params for local connection, it will not work
43
        $strHostname = '';
44
        $strUser = '';
45
        $strPassword = '';
46
        try {
47
            // initialize the wmi object
48
            $objLocator = new COM('WbemScripting.SWbemLocator');
0 ignored issues
show
Unused Code introduced by
The call to com::__construct() has too many arguments starting with 'WbemScripting.SWbemLocator'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
49
            if ($strHostname == "") {
50
                $_wmi = $objLocator->ConnectServer($strHostname, 'root\OpenHardwareMonitor');
51
52
            } else {
53
                $_wmi = $objLocator->ConnectServer($strHostname, 'root\OpenHardwareMonitor', $strHostname.'\\'.$strUser, $strPassword);
54
            }
55
        } catch (Exception $e) {
56
            $this->error->addError("WMI connect error", "PhpSysInfo can not connect to the WMI interface for OpenHardwareMonitor data.");
57
        }
58
        if ($_wmi) {
59
            $this->_buf = CommonFunctions::getWMI($_wmi, 'Sensor', array('Parent', 'Name', 'SensorType', 'Value'));
0 ignored issues
show
Documentation Bug introduced by
It seems like \CommonFunctions::getWMI...'SensorType', 'Value')) of type array is incompatible with the declared type object of property $_buf.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
60
        }
61
    }
62
63
    /**
64
     * get temperature information
65
     *
66
     * @return void
67
     */
68 View Code Duplication
    private function _temperature()
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...
69
    {
70
        if ($this->_buf) foreach ($this->_buf as $buffer) {
71
            if ($buffer['SensorType'] == "Temperature") {
72
                $dev = new SensorDevice();
73
                $dev->setName($buffer['Parent'].' '.$buffer['Name']);
74
                $dev->setValue($buffer['Value']);
75
                $this->mbinfo->setMbTemp($dev);
0 ignored issues
show
Documentation introduced by
$dev is of type object<SensorDevice>, but the function expects a object<Sensor>.

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...
76
            }
77
        }
78
    }
79
80
    /**
81
     * get voltage information
82
     *
83
     * @return void
84
     */
85 View Code Duplication
    private function _voltage()
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...
86
    {
87
        if ($this->_buf) foreach ($this->_buf as $buffer) {
88
            if ($buffer['SensorType'] == "Voltage") {
89
                $dev = new SensorDevice();
90
                $dev->setName($buffer['Parent'].' '.$buffer['Name']);
91
                $dev->setValue($buffer['Value']);
92
                $this->mbinfo->setMbVolt($dev);
0 ignored issues
show
Documentation introduced by
$dev is of type object<SensorDevice>, but the function expects a object<Sensor>.

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...
93
            }
94
        }
95
    }
96
97
    /**
98
     * get fan information
99
     *
100
     * @return void
101
     */
102 View Code Duplication
    private function _fans()
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...
103
    {
104
        if ($this->_buf) foreach ($this->_buf as $buffer) {
105
            if ($buffer['SensorType'] == "Fan") {
106
                $dev = new SensorDevice();
107
                $dev->setName($buffer['Parent'].' '.$buffer['Name']);
108
                $dev->setValue($buffer['Value']);
109
                $this->mbinfo->setMbFan($dev);
110
            }
111
        }
112
    }
113
114
    /**
115
     * get power information
116
     *
117
     * @return void
118
     */
119 View Code Duplication
    private function _power()
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...
120
    {
121
        if ($this->_buf) foreach ($this->_buf as $buffer) {
122
            if ($buffer['SensorType'] == "Power") {
123
                $dev = new SensorDevice();
124
                $dev->setName($buffer['Parent'].' '.$buffer['Name']);
125
                $dev->setValue($buffer['Value']);
126
                $this->mbinfo->setMbPower($dev);
0 ignored issues
show
Documentation introduced by
$dev is of type object<SensorDevice>, but the function expects a object<Sensor>.

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...
127
            }
128
        }
129
    }
130
131
    /**
132
     * get the information
133
     *
134
     * @see PSI_Interface_Sensor::build()
135
     *
136
     * @return Void
137
     */
138
    public function build()
139
    {
140
      $this->_temperature();
141
      $this->_voltage();
142
      $this->_fans();
143
      $this->_power();
144
    }
145
}
146