Issues (10)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

Gauge/ProcessesGauge.php (5 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: Petrica
5
 * Date: 5/28/2016
6
 * Time: 23:53
7
 */
8
namespace Petrica\StatsdSystem\Gauge;
9
10
use Petrica\StatsdSystem\Collection\ValuesCollection;
11
use Petrica\StatsdSystem\Model\Process\Process;
12
use Petrica\StatsdSystem\Model\Process\TopProcessParser;
13
use Petrica\StatsdSystem\Model\TopCommand;
14
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
15
16
class ProcessesGauge implements GaugeInterface
17
{
18
    protected $cpuAbove;
19
20
    protected $memoryAbove;
21
22
    /**
23
     * @var TopCommand
24
     */
25
    protected $command;
26
27
    /**
28
     * This is the cache namespace
29
     *
30
     * @var string
31
     */
32
    protected $namespace;
33
34
    /**
35
     * ProcessesGauge constructor.
36
     *
37
     * Track CPU and memory of processes
38
     *
39
     * @param float $cpuAbove
40
     * @param float $memoryAbove
41
     */
42 3
    public function __construct($cpuAbove = 5.0, $memoryAbove = 1.0)
43
    {
44 3
        $this->cpuAbove = $cpuAbove;
45 3
        $this->memoryAbove = $memoryAbove;
46
47 3
        $this->command = new TopCommand();
48
49 3
        $this->namespace = 'statsd.localhost';
50 3
    }
51
52
    /**
53
     * {@inheritdoc}
54
     */
55
    public function getSamplingPeriod()
56
    {
57
        return 10;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 1
    public function getCollection()
64
    {
65 1
        $processes = $this->getProcesses();
66
67 1
        $cpu = $this->aggregateCpu($processes);
68 1
        $cpu = $this->filterAbove($cpu, $this->cpuAbove);
69
70 1
        $memory = $this->aggregateMemory($processes);
71 1
        $memory = $this->filterAbove($memory, $this->memoryAbove);
72
73
        // Get previous values saved
74 1
        $collection = $this->retrieveCollection();
75 1
        $this->resetValues($collection);
0 ignored issues
show
The call to the method Petrica\StatsdSystem\Gau...sesGauge::resetValues() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
76
77 1
        foreach ($cpu as $name => $value) {
78 1
            $collection->add($name . '.cpu.value', $value);
79 1
        }
80
81 1
        foreach ($memory as $name => $value) {
82 1
            $collection->add($name . '.memory.value', $value);
83 1
        }
84
85
        // Persist collection to a temporary storage
86 1
        $copyCollection = new ValuesCollection($collection->getValues());
87 1
        $this->removeEmpty($copyCollection);
0 ignored issues
show
The call to the method Petrica\StatsdSystem\Gau...sesGauge::removeEmpty() seems un-needed as the method has no side-effects.

PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.

Let’s take a look at an example:

class User
{
    private $email;

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

If we look at the getEmail() method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:

$user = new User();
$user->getEmail(); // This line could safely be removed as it has no effect.

On the hand, if we look at the setEmail(), this method _has_ side-effects. In the following case, we could not remove the method call:

$user = new User();
$user->setEmail('email@domain'); // This line has a side-effect (it changes an
                                 // instance variable).
Loading history...
88 1
        $this->persistCollection($copyCollection);
89
90 1
        return $collection;
91
    }
92
93
    /**
94
     * Return parsed data for processes
95
     */
96 1
    protected function getProcesses()
97
    {
98 1
        $data = $this->getCommand()->run();
99
100 1
        $parser = new TopProcessParser($data);
101 1
        $parser->parse();
102
103 1
        return $parser->getProcesses();
104
    }
105
106
    /**
107
     * Return only those processes above certail value
108
     *
109
     * @param $processes Process[]
110
     * @return array
111
     */
112
    protected function filterAbove($processes, $gate)
113
    {
114 1
        return array_filter($processes, function ($item) use ($gate) {
115 1
            return $item >= $gate;
116 1
        });
117
    }
118
119
    /**
120
     * Aggregate CPU for processes with the same name
121
     *
122
     * @param $processes Process[]
123
     */
124 1 View Code Duplication
    protected function aggregateCpu($processes)
0 ignored issues
show
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...
125
    {
126 1
        $cpus = array();
127
128 1
        foreach ($processes as $process) {
129 1
            if (isset($cpus[$process->getName()])) {
130 1
                $cpus[$process->getName()] += $process->getCpu();
131 1
            }
132
            else {
133 1
                $cpus[$process->getName()] = $process->getCpu();
134
            }
135 1
        }
136
137 1
        return $cpus;
138
    }
139
140
    /**
141
     * Aggregate memory for processes with the same name
142
     *
143
     * @param $processes Process[]
144
     * @return array
145
     */
146 1 View Code Duplication
    protected function aggregateMemory($processes)
0 ignored issues
show
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...
147
    {
148 1
        $memory = array();
149
150 1
        foreach ($processes as $process) {
151 1
            if (isset($memory[$process->getName()])) {
152 1
                $memory[$process->getName()] += $process->getMemory();
153 1
            }
154
            else {
155 1
                $memory[$process->getName()] = $process->getMemory();
156
            }
157 1
        }
158
159 1
        return $memory;
160
    }
161
162
    /**
163
     * Return command object for top utility
164
     *
165
     * @return TopCommand
166
     */
167
    protected function getCommand()
168
    {
169
        return $this->command;
170
    }
171
172
    /**
173
     * Persist collection values to a temporary storage
174
     * key is the command string value
175
     *
176
     * @param $collection
177
     */
178 3
    protected function persistCollection($collection)
179
    {
180 3
        $cache = new FilesystemAdapter($this->namespace);
181 3
        $item = $cache->getItem('collection');
182 3
        $item->set($collection);
183 3
        $cache->save($item);
184 3
    }
185
186
    /**\
187
     * Retrieve a new collection of a previous collection
188
     *
189
     * @return mixed|ValuesCollection
190
     */
191 3
    protected function retrieveCollection()
192
    {
193 3
        $cache = new FilesystemAdapter($this->namespace);
194 3
        $item = $cache->getItem('collection');
195
196 3
        $collection = null;
197 3
        if ($item->isHit()) {
198 3
            $collection = $item->get('collection');
0 ignored issues
show
The call to CacheItemInterface::get() has too many arguments starting with 'collection'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
199 3
        }
200
201 3
        if (empty($collection) || !$collection instanceof ValuesCollection) {
202
            $collection = new ValuesCollection();
203
        }
204
205 3
        return $collection;
206
    }
207
208
    /**
209
     * Reset collection values to 0
210
     *
211
     * These are in fact previous collection values that needs to be sent to statsd as 0
212
     *
213
     * @param $collection
214
     */
215 1
    protected function resetValues($collection) {
216 1
        foreach ($collection as $key => $value) {
217 1
            $collection[$key] = 0;
218 1
        }
219
220 1
        return $collection;
221
    }
222
223
    /**
224
     * Remove empty values from collection
225
     *
226
     * @param $collection
227
     */
228 1
    protected function removeEmpty($collection) {
229 1
        foreach ($collection as $key => $value) {
230 1
            if (empty($value)) {
231 1
                unset($collection[$key]);
232 1
            }
233 1
        }
234
235 1
        return $collection;
236
    }
237
}