Snapshot   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 1
Metric Value
wmc 18
c 3
b 0
f 1
lcom 1
cbo 0
dl 0
loc 118
ccs 77
cts 77
cp 1
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A readLoadAvg() 0 13 3
A loadavg() 0 4 1
B readMeminfo() 0 24 5
A meminfo() 0 4 1
A memTotal() 0 4 1
A memAvailable() 0 4 1
A htmlReport() 0 23 2
A memHtmlReport() 0 20 3
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 23 and the first side effect is on line 140.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
/*
3
Copyright 2016 Anton Petersson
4
5
This file is part of Sysinfo.
6
7
Sysinfo is free software: you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation, either version 3 of the License, or
10
(at your option) any later version.
11
12
Sysinfo is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
17
You should have received a copy of the GNU General Public License
18
along with Sysinfo.  If not, see <http://www.gnu.org/licenses/>.
19
*/
20
21
namespace Anpk12\Sysinfo;
22
23
class Snapshot
24
{
25
    private $procDir;
26 3
    function __construct($procDir='/proc')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
    {
28 3
        $this->procDir = $procDir;
29 3
        $this->readLoadAvg();
30 3
        $this->readMeminfo();
31 3
    }
32
33
    private $loadavgData = [];
34 3
    private function readLoadAvg()
35
    {
36 3
        $f = fopen($this->procDir . "/loadavg", "r");
37 3
        if ( !$f )
38 3
            throw new Exception("failed to read loadavg");
39 3
        if ( ($str = fgets($f)) != FALSE )
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $str = fgets($f) of type string to the boolean FALSE. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
40 3
        {
41 3
            $this->loadavgData = explode(" ", $str);
42 3
            unset($this->loadavgData[3]);
43 3
            unset($this->loadavgData[4]);
44 3
        }
45 3
        fclose($f);
46 3
    }
47
48 2
    public function loadavg()
49
    {
50 2
        return $this->loadavgData;
51
    }
52
53
    private $meminfoData = [];
54 3
    private function readMeminfo()
55
    {
56 3
        $f = fopen($this->procDir . "/meminfo", "r");
57 3
        $this->meminfoData = [];
58 3
        if ( !$f )
59 3
            throw new Exception("failed to read meminfo");
60 3
        while ( ($str = fgets($f)) != FALSE )
0 ignored issues
show
Bug Best Practice introduced by
It seems like you are loosely comparing $str = fgets($f) of type string to the boolean FALSE. If you are specifically checking for a non-empty string, consider using the more explicit !== '' instead.
Loading history...
61
        {
62 3
            $numMatches = preg_match_all(
63 3
            "/([[:alpha:]()_0-9]+):\s+([0-9]+)(?:\s([[:alpha:]]+)){0,1}/",
64 3
                $str, $entry, PREG_SPLIT_DELIM_CAPTURE);
65 3
            if ( $numMatches > 0 )
66 3
            {
67 3
                $key = $entry[0][1];
68 3
                $newEntry = array('value' => $entry[0][2]);
69 3
                if ( isset($entry[0][3]) )
70 3
                {
71 3
                    $newEntry['unit'] = $entry[0][3];
72 3
                }
73 3
                $this->meminfoData[$key] = $newEntry;
74 3
            }
75 3
        }
76 3
        fclose($f);
77 3
    }
78
79
    /**
80
    Return all info from /proc/meminfo
81
    */
82 2
    public function meminfo()
83
    {
84 2
        return $this->meminfoData;
85
    }
86
87 1
    public function memTotal()
88
    {
89 1
        return $this->meminfoData['MemTotal'];
90
    }
91
92 1
    public function memAvailable()
93
    {
94 1
        return  $this->meminfoData['MemAvailable'];
95
    }
96
97 1
    public function htmlReport($summary=false)
98
    {
99 1
        $report = "<h2>Host system stats</h2>";
100 1
        $report .= "<h3>Memory</h3>";
101 1
        if ( $summary === true )
102 1
        {
103 1
            $mema = $this->memAvailable();
104 1
            $memt = $this->memTotal();
105 1
            $report .= "<p><strong>".entryStr($mema)."</strong> available
106 1
                out of a total of <strong>".entryStr($memt)."</strong></p>";
107 1
        } else
108
        {
109 1
            $report .= $this->memHtmlReport();
110
        }
111
112 1
        $report .= "<h3>Load</h3>";
113 1
        $loadavg = $this->loadavg();
114
        $report .= "<p>Average number of jobs in run queue or 
115
            waiting for disk I/O the last 1, 5, and 15 minutes: <strong>"
116 1
            .$loadavg[0]."</strong>, <strong>".$loadavg[1]."</strong>, <strong>".$loadavg[2]."</strong></p>";
117
118 1
        return $report;
119
    }
120 1
    public function memHtmlReport()
121
    {
122 1
        $mem = $this->meminfo();
123
124 1
        $html = '<table>';
125 1
        foreach ( $mem as $key => $entry )
126
        {
127 1
            $html .= '<tr>';
128 1
            $html .= '<td class="sysinfo_key">'.$key.
129 1
                '</td><td class="sysinfo_value">'.$entry['value'] .'</td>';
130 1
            if ( isset($entry['unit']) )
131 1
            {
132 1
                $html .= '<td class="sysinfo_unit">'.$entry['unit'].'</td>';
133 1
            }
134 1
            $html .= '</tr>';
135 1
        }
136 1
        $html .= '</table>';
137
138 1
        return $html;
139
    }
140
};
141
142
function entryStr($entry)
143
{
144 1
    return $entry['value'].$entry['unit'];
145
}
146
147