Issues (1507)

Security Analysis    not enabled

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.

PHPDaemon/Traits/EventHandlers.php (6 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
namespace PHPDaemon\Traits;
3
4
use PHPDaemon\Core\CallbackWrapper;
5
6
/**
7
 * Event handlers trait
8
 * @package PHPDaemon\Traits
9
 * @author  Vasily Zorin <[email protected]>
10
 */
11
trait EventHandlers
12
{
13
    /**
14
     * @var array Event handlers
15
     */
16
    protected $eventHandlers = [];
17
18
    /**
19
     * @var boolean Unshift $this to arguments of callback?
20
     */
21
    protected $addThisToEvents = true;
22
23
    /**
24
     * @var string Last called event name
25
     */
26
    protected $lastEventName;
27
28
    /**
29
     * Propagate event
30
     * @param  string $name Event name
31
     * @param  mixed ...$args Arguments
32
     * @return self
33
     */
34 View Code Duplication
    public function event($name, ...$args)
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...
35
    {
36
        if ($this->addThisToEvents) {
37
            array_unshift($args, $this);
38
        }
39
        if (isset($this->eventHandlers[$name])) {
40
            $this->lastEventName = $name;
41
            foreach ($this->eventHandlers[$name] as $cb) {
42
                if ($cb(...$args) === true) {
43
                    return $this;
44
                }
45
            }
46
        }
47
        return $this;
48
    }
49
50
    /**
51
     * Propagate event
52
     * @param  string $name Event name
53
     * @param  mixed ...$args Arguments
54
     * @return self
55
     */
56 View Code Duplication
    public function trigger($name, ...$args)
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...
57
    {
58
        if ($this->addThisToEvents) {
59
            array_unshift($args, $this);
60
        }
61
        if (isset($this->eventHandlers[$name])) {
62
            $this->lastEventName = $name;
63
            foreach ($this->eventHandlers[$name] as $cb) {
64
                if ($cb(...$args) === true) {
65
                    return $this;
66
                }
67
            }
68
        }
69
        return $this;
70
    }
71
72
    /**
73
     * Propagate event
74
     * @param  string $name Event name
75
     * @param  mixed ...$args Arguments
76
     * @return integer
77
     */
78
    public function triggerAndCount($name, ...$args)
79
    {
80
        if ($this->addThisToEvents) {
81
            array_unshift($args, $this);
82
        }
83
        $cnt = 0;
84
        if (isset($this->eventHandlers[$name])) {
85
            $this->lastEventName = $name;
86
            foreach ($this->eventHandlers[$name] as $cb) {
87
                if ($cb(...$args) !== 0) {
88
                    ++$cnt;
89
                }
90
            }
91
        }
92
        return $cnt;
93
    }
94
95
    /**
96
     * Use it to define event name, when one callback was bind to more than one events
97
     * @return string
98
     */
99
    public function getLastEventName()
100
    {
101
        return $this->lastEventName;
102
    }
103
104
    /**
105
     * Bind event or events
106
     * @alias EventHandlers::bind
107
     * @param string|array $event Event name
108
     * @param callable $cb Callback
109
     * @return self
110
     */
111
    public function on($event, $cb)
112
    {
113
        return $this->bind($event, $cb);
114
    }
115
116
    /**
117
     * Bind event or events
118
     * @param string|array $event Event name
119
     * @param callable $cb Callback
120
     * @return self
121
     */
122
    public function bind($event, $cb)
123
    {
124
        if ($cb !== null) {
125
            $cb = CallbackWrapper::wrap($cb);
126
        }
127
        $event = (array) $event;
128
        foreach ($event as $e) {
129
            CallbackWrapper::addToArray($this->eventHandlers[$e], $cb);
130
        }
131
        return $this;
132
    }
133
134
    /**
135
     * Unbind event(s) or callback from event(s)
136
     * @alias EventHandlers::unbind
137
     * @param string|array $event Event name
138
     * @param callable $cb Callback, optional
0 ignored issues
show
Should the type for parameter $cb not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
139
     * @return self
140
     */
141
    public function off($event, $cb = null)
142
    {
143
        return $this->unbind($event, $cb);
144
    }
145
146
    /**
147
     * Unbind event(s) or callback from event(s)
148
     * @param string|array $event Event name
149
     * @param callable $cb Callback, optional
0 ignored issues
show
Should the type for parameter $cb not be callable|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
150
     * @return self
151
     */
152
    public function unbind($event, $cb = null)
153
    {
154
        if ($cb !== null) {
155
            $cb = CallbackWrapper::wrap($cb);
156
        }
157
        $event = (array) $event;
158
        $success = true;
0 ignored issues
show
$success is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
159
        foreach ($event as $e) {
160
            if (!isset($this->eventHandlers[$e])) {
161
                $success = false;
0 ignored issues
show
$success is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
162
                continue;
163
            }
164
            if ($cb === null) {
165
                unset($this->eventHandlers[$e]);
166
                continue;
167
            }
168
            CallbackWrapper::removeFromArray($this->eventHandlers[$e], $cb);
169
        }
170
        return $this;
171
    }
172
173
    /**
174
     * Clean up all events
175
     * @return void
176
     */
177
    protected function cleanupEventHandlers()
178
    {
179
        $this->eventHandlers = [];
180
        //Daemon::log('clean up event handlers '.get_class($this). ' -- '.$this->attrs->server['REQUEST_URI']. PHP_EOL .Debug::backtrace());
181
    }
182
}
183