| Conditions | 29 |
| Paths | 84 |
| Total Lines | 110 |
| Code Lines | 73 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 48 | public function __construct($enc) |
||
| 49 | { |
||
| 50 | parent::__construct(__CLASS__, $enc); |
||
| 51 | switch (strtolower(PSI_PLUGIN_PS_ACCESS)) { |
||
| 52 | case 'command': |
||
| 53 | if (PSI_OS == 'WINNT') { |
||
| 54 | try { |
||
| 55 | $objLocator = new COM("WbemScripting.SWbemLocator"); |
||
| 56 | $wmi = $objLocator->ConnectServer(); |
||
| 57 | $os_wmi = $wmi->InstancesOf('Win32_OperatingSystem'); |
||
| 58 | foreach ($os_wmi as $os) { |
||
| 59 | $memtotal = $os->TotalVisibleMemorySize * 1024; |
||
| 60 | } |
||
| 61 | $process_wmi = $wmi->InstancesOf('Win32_Process'); |
||
| 62 | foreach ($process_wmi as $process) { |
||
| 63 | if (strlen(trim($process->CommandLine)) > 0) { |
||
| 64 | $ps = trim($process->CommandLine); |
||
| 65 | } else { |
||
| 66 | $ps = trim($process->Caption); |
||
| 67 | } |
||
| 68 | if (trim($process->ProcessId) != 0) { |
||
| 69 | $memusage = round(trim($process->WorkingSetSize) * 100 / $memtotal, 1); |
||
| 70 | //ParentProcessId |
||
| 71 | //Unique identifier of the process that creates a process. Process identifier numbers are reused, so they |
||
| 72 | //only identify a process for the lifetime of that process. It is possible that the process identified by |
||
| 73 | //ParentProcessId is terminated, so ParentProcessId may not refer to a running process. It is also |
||
| 74 | //possible that ParentProcessId incorrectly refers to a process that reuses a process identifier. You can |
||
| 75 | //use the CreationDate property to determine whether the specified parent was created after the process |
||
| 76 | //represented by this Win32_Process instance was created. |
||
| 77 | //=> subtrees of processes may be missing (WHAT TODO?!?) |
||
| 78 | $this->_filecontent[] = trim($process->ProcessId)." ".trim($process->ParentProcessId)." ".$memusage." ".$ps; |
||
| 79 | } |
||
| 80 | } |
||
| 81 | } catch (Exception $e) { |
||
| 82 | } |
||
| 83 | } else { |
||
| 84 | CommonFunctions::executeProgram("ps", "axo pid,ppid,pmem,args", $buffer, PSI_DEBUG); |
||
| 85 | if (((PSI_OS == 'Linux') || (PSI_OS == 'Android')) && (!preg_match("/^[^\n]+\n\s*\d+\s+\d+\s+[\d\.]+\s+.+/", $buffer))) { //alternative method if no data |
||
| 86 | if (CommonFunctions::rfts('/proc/meminfo', $mbuf)) { |
||
| 87 | $bufe = preg_split("/\n/", $mbuf, -1, PREG_SPLIT_NO_EMPTY); |
||
| 88 | $totalmem = 0; |
||
| 89 | foreach ($bufe as $buf) { |
||
| 90 | if (preg_match('/^MemTotal:\s+(.*)\s*kB/i', $buf, $ar_buf)) { |
||
| 91 | $totalmem = $ar_buf[1]; |
||
| 92 | break; |
||
| 93 | } |
||
| 94 | } |
||
| 95 | $buffer = " PID PPID %MEM COMMAND\n"; |
||
| 96 | |||
| 97 | $processlist = glob('/proc/*/status', GLOB_NOSORT); |
||
| 98 | if (($total = count($processlist)) > 0) { |
||
| 99 | natsort($processlist); //first sort |
||
| 100 | $prosess = array(); |
||
| 101 | foreach ($processlist as $processitem) { //second sort |
||
| 102 | $process[] = $processitem; |
||
| 103 | } |
||
| 104 | |||
| 105 | $buf = ""; |
||
| 106 | for ($i = 0; $i < $total; $i++) { |
||
| 107 | if (CommonFunctions::rfts($process[$i], $buf, 0, 4096, false)) { |
||
| 108 | |||
| 109 | if (($totalmem != 0) && (preg_match('/^VmRSS:\s+(\d+)\s+kB/m', $buf, $tmppmem))) { |
||
| 110 | $pmem = round(100 * $tmppmem[1] / $totalmem, 1); |
||
| 111 | } else { |
||
| 112 | $pmem = 0; |
||
| 113 | } |
||
| 114 | |||
| 115 | $name = null; |
||
| 116 | if (CommonFunctions::rfts(substr($process[$i], 0, strlen($process[$i])-6)."cmdline", $namebuf, 0, 4096, false)) { |
||
| 117 | $name = str_replace(chr(0), ' ', trim($namebuf)); |
||
| 118 | } |
||
| 119 | if (preg_match('/^Pid:\s+(\d+)/m', $buf, $tmppid) && |
||
| 120 | preg_match('/^PPid:\s+(\d+)/m', $buf, $tmpppid) && |
||
| 121 | preg_match('/^Name:\s+(.+)/m', $buf, $tmpargs)) { |
||
| 122 | $pid = $tmppid[1]; |
||
| 123 | $ppid = $tmpppid[1]; |
||
| 124 | $args = $tmpargs[1]; |
||
| 125 | if ($name !== null) { |
||
| 126 | if ($name !== "") { |
||
| 127 | $args = $name; |
||
| 128 | } else { |
||
| 129 | $args = "[".$args."]"; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | $buffer .= $pid." ".$ppid." ".$pmem." ".$args."\n"; |
||
| 133 | } |
||
| 134 | |||
| 135 | } |
||
| 136 | } |
||
| 137 | } |
||
| 138 | } |
||
| 139 | } |
||
| 140 | } |
||
| 141 | break; |
||
| 142 | case 'data': |
||
| 143 | CommonFunctions::rfts(APP_ROOT."/data/ps.txt", $buffer); |
||
| 144 | break; |
||
| 145 | default: |
||
| 146 | $this->global_error->addConfigError("__construct()", "PSI_PLUGIN_PS_ACCESS"); |
||
| 147 | break; |
||
| 148 | } |
||
| 149 | if (PSI_OS != 'WINNT') { |
||
| 150 | if (trim($buffer) != "") { |
||
| 151 | $this->_filecontent = preg_split("/\n/", $buffer, -1, PREG_SPLIT_NO_EMPTY); |
||
| 152 | unset($this->_filecontent[0]); |
||
| 153 | } else { |
||
| 154 | $this->_filecontent = array(); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | } |
||
| 158 | /** |
||
| 256 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.