ipmiinfo::fans()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 17
Code Lines 12

Duplication

Lines 17
Ratio 100 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
nc 4
nop 0
dl 17
loc 17
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * ipmiinfo Plugin
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI_Plugin_ipmiinfo
9
 * @author    Mieczyslaw Nalewaj <[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.ipmiinfo.inc.php 661 2012-08-27 11:26:39Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
/**
16
 * ipmiinfo plugin, which displays all ipmi informations available
17
 *
18
 * @category  PHP
19
 * @package   PSI_Plugin_ipmiinfo
20
 * @author    Mieczyslaw Nalewaj <[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 ipmiinfo extends PSI_Plugin
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
    private $_lines;
29
30
    public function __construct($enc)
31
    {
32
        parent::__construct(__CLASS__, $enc);
33
34
        $this->_lines = array();
35
    }
36
37
    /**
38
     * get temperature information
39
     *
40
     * @return array temperatures in array with label
41
     */
42 View Code Duplication
    private function temperatures()
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...
43
    {
44
        $result = array();
45
        $i = 0;
46
        foreach ($this->_lines as $line) {
47
            $buffer = preg_split("/\s*\|\s*/", $line);
48
            if ($buffer[2] == "degrees C" && $buffer[3] != "na") {
49
                $result[$i]['label'] = $buffer[0];
50
                $result[$i]['value'] = $buffer[1];
51
                $result[$i]['state'] = $buffer[3];
52
                if ($buffer[8] != "na") $result[$i]['max'] = $buffer[8];
53
                $i++;
54
            }
55
        }
56
57
        return $result;
58
    }
59
60
    /**
61
     * get voltages information
62
     *
63
     * @return array voltage in array with label
64
     */
65
    private function voltages()
66
    {
67
        $result = array();
68
        $i = 0;
69
        foreach ($this->_lines as $line) {
70
            $buffer = preg_split("/\s*\|\s*/", $line);
71
            if ($buffer[2] == "Volts" && $buffer[3] != "na") {
72
                $result[$i]['label'] = $buffer[0];
73
                $result[$i]['value'] = $buffer[1];
74
                $result[$i]['state'] = $buffer[3];
75
                if ($buffer[5] != "na") $result[$i]['min'] = $buffer[5];
76
                if ($buffer[8] != "na") $result[$i]['max'] = $buffer[8];
77
                $i++;
78
            }
79
        }
80
81
        return $result;
82
    }
83
84
    /**
85
     * get fans information
86
     *
87
     * @return array fans in array with label
88
     */
89 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...
90
    {
91
        $result = array();
92
        $i = 0;
93
        foreach ($this->_lines as $line) {
94
            $buffer = preg_split("/\s*\|\s*/", $line);
95
            if ($buffer[2] == "RPM" && $buffer[3] != "na") {
96
                $result[$i]['label'] = $buffer[0];
97
                $result[$i]['value'] = $buffer[1];
98
                $result[$i]['state'] = $buffer[3];
99
                if ($buffer[8] != "na") $result[$i]['min'] = $buffer[8];
100
                $i++;
101
            }
102
        }
103
104
        return $result;
105
    }
106
107
    /**
108
     * get powers information
109
     *
110
     * @return array misc in array with label
111
     */
112 View Code Duplication
    private function powers()
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...
113
    {
114
        $result = array();
115
        $i = 0;
116
        foreach ($this->_lines as $line) {
117
            $buffer = preg_split("/\s*\|\s*/", $line);
118
            if ($buffer[2] == "Watts" && $buffer[3] != "na") {
119
                $result[$i]['label'] = $buffer[0];
120
                $result[$i]['value'] = $buffer[1];
121
                $result[$i]['state'] = $buffer[3];
122
                if ($buffer[8] != "na") $result[$i]['max'] = $buffer[8];
123
                $i++;
124
            }
125
        }
126
127
        return $result;
128
    }
129
130
    /**
131
     * get currents information
132
     *
133
     * @return array misc in array with label
134
     */
135 View Code Duplication
    private function currents()
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...
136
    {
137
        $result = array();
138
        $i = 0;
139
        foreach ($this->_lines as $line) {
140
            $buffer = preg_split("/\s*\|\s*/", $line);
141
            if ($buffer[2] == "Amps" && $buffer[3] != "na") {
142
                $result[$i]['label'] = $buffer[0];
143
                $result[$i]['value'] = $buffer[1];
144
                $result[$i]['state'] = $buffer[3];
145
                if ($buffer[8] != "na") $result[$i]['max'] = $buffer[8];
146
                $i++;
147
            }
148
        }
149
150
        return $result;
151
    }
152
153
    /**
154
     * get misc information
155
     *
156
     * @return array misc in array with label
157
     */
158
    private function misc()
159
    {
160
        $result = array();
161
        $i = 0;
162
        foreach ($this->_lines as $line) {
163
            $buffer = preg_split("/\s*\|\s*/", $line);
164
            if ($buffer[2] == "discrete" && $buffer[3] != "na") {
165
                $result[$i]['label'] = $buffer[0];
166
                $result[$i]['value'] = $buffer[1];
167
                $result[$i]['state'] = $buffer[3];
168
                $i++;
169
            }
170
        }
171
172
        return $result;
173
    }
174
175
    public function execute()
176
    {
177
        $this->_lines = array();
178
        switch (strtolower(PSI_PLUGIN_IPMIINFO_ACCESS)) {
179
            case 'command':
180
                $lines = "";
181
                if (CommonFunctions::executeProgram('ipmitool', 'sensor', $lines) && !empty($lines))
182
                    $this->_lines = preg_split("/\n/", $lines, -1, PREG_SPLIT_NO_EMPTY);
183
                break;
184
            case 'data':
185
                if (CommonFunctions::rfts(APP_ROOT."/data/ipmiinfo.txt", $lines) && !empty($lines))
186
                    $this->_lines = preg_split("/\n/", $lines, -1, PREG_SPLIT_NO_EMPTY);
187
                break;
188
            default:
189
                $this->error->addConfigError('__construct()', 'PSI_PLUGIN_IPMIINFO_ACCESS');
0 ignored issues
show
Bug introduced by
The property error does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
190
                break;
191
        }
192
    }
193
194
    public function xml()
195
    {
196
        if (empty($this->_lines))
197
        return $this->xml->getSimpleXmlElement();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->xml->getSimpleXmlElement(); (SimpleXMLElement) is incompatible with the return type declared by the interface PSI_Interface_Plugin::xml of type SimpleXMLObject.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
198
199
        $arrBuff = $this->temperatures();
200 View Code Duplication
        if (sizeof($arrBuff) > 0) {
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...
201
            $temp = $this->xml->addChild("Temperatures");
202
            foreach ($arrBuff as $arrValue) {
203
                $item = $temp->addChild('Item');
204
                $item->addAttribute('Label', $arrValue['label']);
205
                $item->addAttribute('Value', $arrValue['value']);
206
                $item->addAttribute('State', $arrValue['state']);
207
                if (isset($arrValue['Max'])) $item->addAttribute('Max', $arrValue['Max']);
208
            }
209
        }
210
        $arrBuff = $this->voltages();
211
        if (sizeof($arrBuff) > 0) {
212
            $volt = $this->xml->addChild('Voltages');
213
            foreach ($arrBuff as $arrValue) {
214
                $item = $volt->addChild('Item');
215
                $item->addAttribute('Label', $arrValue['label']);
216
                $item->addAttribute('Value', $arrValue['value']);
217
                $item->addAttribute('State', $arrValue['state']);
218
                if (isset($arrValue['Min'])) $item->addAttribute('Min', $arrValue['min']);
219
                if (isset($arrValue['Max'])) $item->addAttribute('Max', $arrValue['max']);
220
            }
221
        }
222
        $arrBuff = $this->fans();
223 View Code Duplication
        if (sizeof($arrBuff) > 0) {
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...
224
            $fan = $this->xml->addChild('Fans');
225
            foreach ($arrBuff as $arrValue) {
226
                $item = $fan->addChild('Item');
227
                $item->addAttribute('Label', $arrValue['label']);
228
                $item->addAttribute('Value', $arrValue['value']);
229
                $item->addAttribute('State', $arrValue['state']);
230
                if (isset($arrValue['Min'])) $item->addAttribute('Min', $arrValue['min']);
231
            }
232
        }
233
        $arrBuff = $this->powers();
234 View Code Duplication
        if (sizeof($arrBuff) > 0) {
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...
235
            $misc = $this->xml->addChild('Powers');
236
            foreach ($arrBuff as $arrValue) {
237
                $item = $misc->addChild('Item');
238
                $item->addAttribute('Label', $arrValue['label']);
239
                $item->addAttribute('Value', $arrValue['value']);
240
                $item->addAttribute('State', $arrValue['state']);
241
                if (isset($arrValue['Max'])) $item->addAttribute('Max', $arrValue['max']);
242
            }
243
        }
244
        $arrBuff = $this->currents();
245 View Code Duplication
        if (sizeof($arrBuff) > 0) {
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...
246
            $misc = $this->xml->addChild('Currents');
247
            foreach ($arrBuff as $arrValue) {
248
                $item = $misc->addChild('Item');
249
                $item->addAttribute('Label', $arrValue['label']);
250
                $item->addAttribute('Value', $arrValue['value']);
251
                $item->addAttribute('State', $arrValue['state']);
252
                if (isset($arrValue['Max'])) $item->addAttribute('Max', $arrValue['max']);
253
            }
254
        }
255
        $arrBuff = $this->misc();
256
        if (sizeof($arrBuff) > 0) {
257
            $misc = $this->xml->addChild('Misc');
258
            foreach ($arrBuff as $arrValue) {
259
                $item = $misc->addChild('Item');
260
                $item->addAttribute('Label', $arrValue['label']);
261
                $item->addAttribute('Value', $arrValue['value']);
262
                $item->addAttribute('State', $arrValue['state']);
263
            }
264
        }
265
266
        return $this->xml->getSimpleXmlElement();
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $this->xml->getSimpleXmlElement(); (SimpleXMLElement) is incompatible with the return type declared by the interface PSI_Interface_Plugin::xml of type SimpleXMLObject.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
267
    }
268
269
}
270