Issues (3)

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.

src/Async.php (3 issues)

Labels

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
 * @link https://github.com/vuongxuongminh/laravel-async
4
 *
5
 * @copyright (c) Vuong Xuong Minh
6
 * @license [MIT](https://opensource.org/licenses/MIT)
7
 */
8
9
namespace VXM\Async;
10
11
use Closure;
12
use Illuminate\Contracts\Events\Dispatcher as EventDispatcher;
13
use Illuminate\Support\Str;
14
use Spatie\Async\Process\Runnable;
15
use VXM\Async\Runtime\ParentRuntime;
16
17
/**
18
 * @author Vuong Minh <[email protected]>
19
 * @since  1.0.0
20
 */
21
class Async
22
{
23
    /**
24
     * A pool manage async processes.
25
     *
26
     * @var Pool
27
     */
28
    protected $pool;
29
30
    /**
31
     * Event dispatcher manage async events.
32
     *
33
     * @var EventDispatcher
34
     */
35
    protected $events;
36
37
    /**
38
     * Create a new Async instance.
39
     *
40
     * @param  \VXM\Async\Pool  $pool
41
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
42
     */
43
    public function __construct(Pool $pool, EventDispatcher $events)
44
    {
45
        $this->pool = $pool;
46
        $this->events = $events;
47
    }
48
49
    /**
50
     * Execute async job.
51
     *
52
     * @param  callable|string|object  $job  need to execute.
53
     * @param  array  $events  event. Have key is an event name, value is a callable triggered when event
54
     *                                       happen, have three events `error`, `success`, `timeout`.
55
     * @param  int|null  $outputLength
56
     * @return static
57
     */
58
    public function run($job, array $events = [], int $outputLength = null): self
59
    {
60
        $process = $this->pool->add($this->makeJob($job), $outputLength);
61
62
        $this->addProcessListeners($events, $process);
63
64
        $process->then($this->makeProcessListener('success', $process));
65
        $process->catch($this->makeProcessListener('error', $process));
66
        $process->timeout($this->makeProcessListener('timeout', $process));
67
68
        return $this;
69
    }
70
71
    /**
72
     * Batch execute async jobs.
73
     *
74
     * @param  array  $jobs
75
     * @return static
76
     * @see run()
77
     * @since 2.0.0
78
     */
79
    public function batchRun(...$jobs): self
80
    {
81
        foreach ($jobs as $job) {
82
            $events = [];
83
            $outputLength = null;
84
85
            if (is_array($job)) {
86
                if (count($job) === 2) {
87
                    [$job, $events] = $job;
88
                } else {
89
                    [$job, $events, $outputLength] = $job;
90
                }
91
            }
92
93
            $this->run($job, $events, $outputLength);
94
        }
95
96
        return $this;
97
    }
98
99
    /**
100
     * Wait until all async jobs done and return job results.
101
     *
102
     * @return array
103
     */
104
    public function wait()
105
    {
106
        $results = $this->pool->wait();
107
        $this->pool->flush();
108
109
        return $results;
110
    }
111
112
    /**
113
     * Make async job.
114
     *
115
     * @param $job
116
     *
117
     * @return mixed
118
     */
119
    protected function makeJob($job)
120
    {
121
        if (is_string($job)) {
122
            return $this->createClassJob($job);
123
        }
124
125
        return $job;
126
    }
127
128
    /**
129
     * Create class and method job.
130
     *
131
     * @param  string  $job
132
     *
133
     * @return Closure
134
     */
135
    protected function createClassJob(string $job): Closure
136
    {
137
        [$class, $method] = Str::parseCallback($job, 'handle');
0 ignored issues
show
The variable $class does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
The variable $method does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
138
139
        return function () use ($class, $method) {
140
            return app()->call($class.'@'.$method);
141
        };
142
    }
143
144
    /**
145
     * Listen events of process given.
146
     *
147
     * @param  array  $events
148
     * @param  Runnable  $process
149
     */
150
    protected function addProcessListeners(array $events, Runnable $process): void
151
    {
152
        foreach ($events as $event => $callable) {
153
            $this->events->listen("async.{$event}_{$process->getId()}", $callable);
154
        }
155
    }
156
157
    /**
158
     * Make a base listener for integration with [[EventDispatcher]].
159
     *
160
     * @param  string  $event
161
     * @param  Runnable  $process
162
     *
163
     * @return callable
164
     */
165
    protected function makeProcessListener(string $event, Runnable $process): callable
166
    {
167
        return function (...$args) use ($event, $process) {
168
            $event = "async.{$event}_{$process->getId()}";
0 ignored issues
show
Consider using a different name than the imported variable $event, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
169
            $this->events->dispatch($event, $args);
170
            $this->events->forget($event);
171
        };
172
    }
173
174
    /**
175
     * Create a new process for run a job.
176
     *
177
     * @param  callable  $job  need to execute.
178
     *
179
     * @return Runnable process.
180
     * @deprecated since 2.1.0
181
     */
182
    protected function createProcess($job): Runnable
183
    {
184
        return ParentRuntime::createProcess($job);
185
    }
186
187
    /**
188
     * Get current pool.
189
     *
190
     * @return Pool
191
     * @since 2.1.0
192
     */
193
    public function getPool(): Pool
194
    {
195
        return $this->pool;
196
    }
197
}
198