Quotas::execute()   B
last analyzed

Complexity

Conditions 5
Paths 6

Size

Total Lines 30
Code Lines 24

Duplication

Lines 10
Ratio 33.33 %

Importance

Changes 0
Metric Value
cc 5
eloc 24
nc 6
nop 0
dl 10
loc 30
rs 8.439
c 0
b 0
f 0
1
<?php
2
/**
3
 * Quotas Plugin
4
 *
5
 * PHP version 5
6
 *
7
 * @category  PHP
8
 * @package   PSI_Plugin_Quotas
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.quotas.inc.php 661 2012-08-27 11:26:39Z namiltd $
13
 * @link      http://phpsysinfo.sourceforge.net
14
 */
15
 /**
16
 * Quotas Plugin, which displays all quotas on the machine
17
 * display all quotas in a sortable table with the current values which are determined by
18
 * calling the "repquota" command line utility, another way is to provide
19
 * a file with the output of the repquota utility, so there is no need to run a execute by the
20
 * webserver, the format of the command is written down in the phpsysinfo.ini file, where also
21
 * the method of getting the information is configured
22
 *
23
 * @category  PHP
24
 * @package   PSI_Plugin_Quotas
25
 * @author    Michael Cramer <[email protected]>
26
 * @copyright 2009 phpSysInfo
27
 * @license   http://opensource.org/licenses/gpl-2.0.php GNU General Public License
28
 * @version   Release: 3.0
29
 * @link      http://phpsysinfo.sourceforge.net
30
 */
31
class Quotas 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...
32
{
33
    /**
34
     * variable, which holds the content of the command
35
     * @var array
36
     */
37
    private $_filecontent = array();
38
39
    /**
40
     * variable, which holds the result before the xml is generated out of this array
41
     * @var array
42
     */
43
    private $_result = array();
44
45
    /**
46
     * read the data into an internal array and also call the parent constructor
47
     *
48
     * @param String $enc target encoding
49
     */
50 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...
51
    {
52
        parent::__construct(__CLASS__, $enc);
53
        switch (strtolower(PSI_PLUGIN_QUOTAS_ACCESS)) {
54
        case 'command':
55
            CommonFunctions::executeProgram("repquota", "-au", $buffer, PSI_DEBUG);
56
            break;
57
        case 'data':
58
            CommonFunctions::rfts(APP_ROOT."/data/quotas.txt", $buffer);
59
            break;
60
        default:
61
            $this->global_error->addConfigError("__construct()", "PSI_PLUGIN_QUOTAS_ACCESS");
62
            break;
63
        }
64
        if (trim($buffer) != "") {
65
            $this->_filecontent = preg_split("/\n/", $buffer, -1, PREG_SPLIT_NO_EMPTY);
0 ignored issues
show
Bug introduced by
The variable $buffer 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...
66
            unset($this->_filecontent[0]);
67
        } else {
68
            $this->_filecontent = array();
69
        }
70
    }
71
72
    /**
73
     * doing all tasks to get the required informations that the plugin needs
74
     * result is stored in an internal array<br>the array is build like a tree,
75
     * so that it is possible to get only a specific process with the childs
76
     *
77
     * @return void
78
     */
79
    public function execute()
80
    {
81
        $i = 0;
82
        $quotas = array();
83
        foreach ($this->_filecontent as $thisline) {
84
            $thisline = preg_replace("/([\s]--)/", "", $thisline);
85
            $thisline = preg_split("/(\s)/e", $thisline, -1, PREG_SPLIT_NO_EMPTY);
86
            if (count($thisline) == 7) {
87
                $quotas[$i]['user'] = str_replace("--", "", $thisline[0]);
88
                $quotas[$i]['byte_used'] = $thisline[1] * 1024;
89
                $quotas[$i]['byte_soft'] = $thisline[2] * 1024;
90
                $quotas[$i]['byte_hard'] = $thisline[3] * 1024;
91 View Code Duplication
                if ($thisline[3] != 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...
92
                    $quotas[$i]['byte_percent_used'] = round((($quotas[$i]['byte_used'] / $quotas[$i]['byte_hard']) * 100), 1);
93
                } else {
94
                    $quotas[$i]['byte_percent_used'] = 0;
95
                }
96
                $quotas[$i]['file_used'] = $thisline[4];
97
                $quotas[$i]['file_soft'] = $thisline[5];
98
                $quotas[$i]['file_hard'] = $thisline[6];
99 View Code Duplication
                if ($thisline[6] != 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...
100
                    $quotas[$i]['file_percent_used'] = round((($quotas[$i]['file_used'] / $quotas[$i]['file_hard']) * 100), 1);
101
                } else {
102
                    $quotas[$i]['file_percent_used'] = 0;
103
                }
104
                $i++;
105
            }
106
        }
107
        $this->_result = $quotas;
108
    }
109
110
    /**
111
     * generates the XML content for the plugin
112
     *
113
     * @return SimpleXMLElement entire XML content for the plugin
114
     */
115
    public function xml()
116
    {
117
        foreach ($this->_result as $quota) {
118
            $quotaChild = $this->xml->addChild("Quota");
119
            $quotaChild->addAttribute("User", $quota['user']);
120
            $quotaChild->addAttribute("ByteUsed", $quota['byte_used']);
121
            $quotaChild->addAttribute("ByteSoft", $quota['byte_soft']);
122
            $quotaChild->addAttribute("ByteHard", $quota['byte_hard']);
123
            $quotaChild->addAttribute("BytePercentUsed", $quota['byte_percent_used']);
124
            $quotaChild->addAttribute("FileUsed", $quota['file_used']);
125
            $quotaChild->addAttribute("FileSoft", $quota['file_soft']);
126
            $quotaChild->addAttribute("FileHard", $quota['file_hard']);
127
            $quotaChild->addAttribute("FilePercentUsed", $quota['file_percent_used']);
128
        }
129
130
        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...
131
    }
132
}
133