DMRaid::__construct()   A
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 17

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 6
nop 1
dl 21
loc 21
rs 9.0534
c 0
b 0
f 0
1
<?php
2
/**
3
 * DMRaid Plugin
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI_Plugin_DMRaid
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.dmraid.inc.php 661 2012-08-27 11:26:39Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * dmraid Plugin, which displays software RAID status
17
 *
18
 * @category  PHP
19
 * @package   PSI_Plugin_DMRaid
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 DMRaid 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
    /**
29
     * variable, which holds the content of the command
30
     * @var array
31
     */
32
    private $_filecontent = "";
33
34
    /**
35
     * variable, which holds the result before the xml is generated out of this array
36
     * @var array
37
     */
38
    private $_result = array();
39
40
    /**
41
     * read the data into an internal array and also call the parent constructor
42
     *
43
     * @param String $enc encoding
44
     */
45 View Code Duplication
    public function __construct($enc)
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...
46
    {
47
        $buffer = "";
48
        parent::__construct(__CLASS__, $enc);
49
        switch (strtolower(PSI_PLUGIN_DMRAID_ACCESS)) {
50
        case 'command':
51
            CommonFunctions::executeProgram("dmraid", "-s -vv 2>&1", $buffer);
52
            break;
53
        case 'data':
54
            CommonFunctions::rfts(APP_ROOT."/data/dmraid.txt", $buffer);
55
            break;
56
        default:
57
            $this->global_error->addConfigError("__construct()", "PSI_PLUGIN_DMRAID_ACCESS");
58
            break;
59
        }
60
        if (trim($buffer) != "") {
61
            $this->_filecontent = preg_split("/(\r?\n\*\*\* )|(\r?\n--> )/", $buffer, -1, PREG_SPLIT_NO_EMPTY);
62
        } else {
63
            $this->_filecontent = array();
64
        }
65
    }
66
67
    /**
68
     * doing all tasks to get the required informations that the plugin needs
69
     * result is stored in an internal array<br>the array is build like a tree,
70
     * so that it is possible to get only a specific process with the childs
71
     *
72
     * @return void
73
     */
74
    public function execute()
75
    {
76
        if (empty($this->_filecontent)) {
77
            return;
78
        }
79
        $group = "";
80
        foreach ($this->_filecontent as $block) {
81
            if (preg_match('/^(NOTICE: )|(ERROR: )/m', $block)) {
82
                $group = "";
83
                $lines = preg_split("/\r?\n/", $block, -1, PREG_SPLIT_NO_EMPTY);
84
                foreach ($lines as $line) {
85
                    if (preg_match('/^NOTICE: added\s+\/dev\/(.+)\s+to RAID set\s+\"(.+)\"/', $line, $partition)) {
86
                        $this->_result['devices'][$partition[2]]['partitions'][$partition[1]]['status'] = "ok";
87
                    } elseif (preg_match('/^ERROR: .* device\s+\/dev\/(.+)\s+(.+)\s+in RAID set\s+\"(.+)\"/', $line, $partition)) {
88
                        if ($partition[2]=="broken") {
89
                            $this->_result['devices'][$partition[3]]['partitions'][$partition[1]]['status'] = 'F';
90 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...
91
                            $this->_result['devices'][$partition[3]]['partitions'][$partition[1]]['status'] = 'W';
92
                        }
93
                    }
94
                }
95
            } else {
96
                if (preg_match('/^Group superset\s+(.+)/m', $block, $arrname)) {
97
                    $group = trim($arrname[1]);
98
                }
99
                if (preg_match('/^name\s*:\s*(.*)/m', $block, $arrname)) {
100
                    if ($group=="") {
101
                        $group = trim($arrname[1]);
102
                    }
103
                    $this->_result['devices'][$group]['name'] = $arrname[1];
104 View Code Duplication
                    if (preg_match('/^size\s*:\s*(.*)/m', $block, $size)) {
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...
105
                        $this->_result['devices'][$group]['size'] = trim($size[1]);
106
                    }
107 View Code Duplication
                    if (preg_match('/^stride\s*:\s*(.*)/m', $block, $stride)) {
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...
108
                            $this->_result['devices'][$group]['stride'] = trim($stride[1]);
109
                    }
110 View Code Duplication
                    if (preg_match('/^type\s*:\s*(.*)/m', $block, $type)) {
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...
111
                        $this->_result['devices'][$group]['type'] = trim($type[1]);
112
                    }
113 View Code Duplication
                    if (preg_match('/^status\s*:\s*(.*)/m', $block, $status)) {
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...
114
                        $this->_result['devices'][$group]['status'] = trim($status[1]);
115
                    }
116 View Code Duplication
                    if (preg_match('/^subsets\s*:\s*(.*)/m', $block, $subsets)) {
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...
117
                        $this->_result['devices'][$group]['subsets'] = trim($subsets[1]);
118
                    }
119 View Code Duplication
                    if (preg_match('/^devs\s*:\s*(.*)/m', $block, $devs)) {
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...
120
                        $this->_result['devices'][$group]['devs'] = trim($devs[1]);
121
                    }
122 View Code Duplication
                    if (preg_match('/^spares\s*:\s*(.*)/m', $block, $spares)) {
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...
123
                            $this->_result['devices'][$group]['spares'] = trim($spares[1]);
124
                    }
125
                    $group = "";
126
                }
127
            }
128
        }
129
    }
130
131
    /**
132
     * generates the XML content for the plugin
133
     *
134
     * @return SimpleXMLObject entire XML content for the plugin
0 ignored issues
show
Documentation introduced by
Should the return type not be SimpleXMLElement?

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...
135
     */
136
    public function xml()
137
    {
138
        if (empty($this->_result)) {
139
            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...
140
        }
141
        $hideRaids = array();
142 View Code Duplication
        if (defined('PSI_PLUGIN_DMRAID_HIDE_RAID_DEVICES') && is_string(PSI_PLUGIN_DMRAID_HIDE_RAID_DEVICES)) {
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...
143
            if (preg_match(ARRAY_EXP, PSI_PLUGIN_DMRAID_HIDE_RAID_DEVICES)) {
144
                $hideRaids = eval(PSI_PLUGIN_DMRAID_HIDE_RAID_DEVICES);
0 ignored issues
show
Coding Style introduced by
It is generally not recommended to use eval unless absolutely required.

On one hand, eval might be exploited by malicious users if they somehow manage to inject dynamic content. On the other hand, with the emergence of faster PHP runtimes like the HHVM, eval prevents some optimization that they perform.

Loading history...
145
            } else {
146
                $hideRaids = array(PSI_PLUGIN_DMRAID_HIDE_RAID_DEVICES);
147
            }
148
        }
149
        foreach ($this->_result['devices'] as $key=>$device) {
150
            if (!in_array($key, $hideRaids, true)) {
151
                $dev = $this->xml->addChild("Raid");
152
                $dev->addAttribute("Device_Name", $key);
153
                $dev->addAttribute("Type", $device["type"]);
154
                $dev->addAttribute("Disk_Status", $device["status"]);
155
                $dev->addAttribute("Name", $device["name"]);
156
                $dev->addAttribute("Size", $device["size"]);
157
                $dev->addAttribute("Stride", $device["stride"]);
158
                $dev->addAttribute("Subsets", $device["subsets"]);
159
                $dev->addAttribute("Devs", $device["devs"]);
160
                $dev->addAttribute("Spares", $device["spares"]);
161
                $disks = $dev->addChild("Disks");
162 View Code Duplication
                if (isset($device['partitions']) && sizeof($device['partitions']>0)) foreach ($device['partitions'] as $diskkey=>$disk) {
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...
163
                    $disktemp = $disks->addChild("Disk");
164
                    $disktemp->addAttribute("Name", $diskkey);
165
                    if ($device["status"]=='ok') {
166
                        $disktemp->addAttribute("Status", $disk['status']);
167
                    } else {
168
                        $disktemp->addAttribute("Status", 'W');
169
                    }
170
                }
171
            }
172
        }
173
174
        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...
175
    }
176
}
177