Issues (5)

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/Negotiation.php (2 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
namespace MarcusCampos\Dealer;
4
5
use MarcusCampos\Dealer\Parser;
6
use Illuminate\Database\Eloquent\Collection;
7
8
class Negotiation
9
{
10
11
    /**
12
     * @var Parser
13
     */
14
    private $parser;
15
16
    /**
17
     * @var array
18
     */
19
    private $stack;
20
21
    /**
22
     * @var \Illuminate\Database\Eloquent\Model
23
     */
24
    private $model;
25
26
    public function __construct()
27
    {
28
        $this->parser = new Parser();
29
    }
30
31
    /**
32
     * Negotiate
33
     *
34
     * @param string $query
35
     * @return Collection
36
     */
37
    public function negotiate(string $query)
38
    {
39
        $this->stack = $this->parser->parse($query);
40
41
        $namespace = config('dealer.models.namespace') ?? 'App\\';
42
        $this->model = app($namespace.$this->stack['model']);
43
44
        $this->model = $this->relations()
45
            ->filters()
46
            ->limit()
47
            ->groupBy()
48
            ->orderBy()
49
            ->get();
50
51
        return $this->only($this->model, $this->stack['fields']['only']);
52
    }
53
54
    /**
55
     * Return only selected elements
56
     *
57
     * @param Collection $collection
58
     * @param array $fields
59
     * @return Collection
60
     */
61
    private function only(Collection $collection, array $fields)
62
    {
63
        $fields = array_filter($fields);
64
        
65
        if(!$fields || $fields[0] == '*') {
66
            return $collection;
67
        }
68
69
        return $collection->map(function ($model) use($fields) {
70
            return $model->only($fields);
71
        });
72
    }
73
74
    /**
75
     * Get relations
76
     *
77
     * @return Negotiation
78
     */
79
    private function relations()
80
    {
81
        $relations = [];
82
83
        if (array_key_exists('extends', $this->stack['fields'])) {
84
            foreach ($this->stack['fields']['extends'] as $value) {
85
                $this->stack['fields']['only'][] = $value['name'];
86
87
                if (!$value['args'] || $value['args'][0] == '*') {
88
                    $relations[] = $value['name'];
89
                    continue;
90
                }
91
                $relations[] = $value['name'] . ':' . implode(',', $value['args']);
92
            }
93
        }
94
95
        $this->model = $this->model->with($relations);
96
97
        return $this;
98
    }
99
100
    /**
101
     * Set filters
102
     *
103
     * @return Negotiation
104
     */
105
    private function filters()
106
    {
107
        $relations = [];
108
109
        if (array_key_exists('filters', $this->stack)) {
110
            if (array_key_exists('extends', $this->stack['filters'])) {
111
                foreach ($this->stack['filters']['extends'] as $value) {
112
                    $methodName = $value['name'];
113
114
                    if (!$value['args']) {
115
                        $this->model = $this->model->$value['name']();
116
                        continue;
117
                    }
118
119
                    $this->model = $this->model->$methodName(...$value['args']);
120
                }
121
            }
122
        }
123
124
        return $this;
125
    }
126
127
    /**
128
     * Set the sort
129
     *
130
     * @return Negotiation
131
     */
132
    private function orderBy()
133
    {
134
        if (array_key_exists('orderBy', $this->stack)) {
135
            $this->model = $this->model->orderBy($this->stack['orderBy'][0],$this->stack['orderBy'][1]);
136
        }
137
138
        return $this;
139
    }
140
141
    /**
142
     * Set limit
143
     *
144
     * @return Negotiation
145
     */
146
    private function limit()
147
    {
148
        if (array_key_exists('limit', $this->stack)) {
149
            $this->model = $this->model->limit($this->stack['limit']);
150
        }
151
152
        return $this;
153
    }
154
155
    /**
156
     * Set groups
157
     *
158
     * @return Negotiation
159
     */
160 View Code Duplication
    private function groupBy()
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...
161
    {
162
        if (array_key_exists('groupBy', $this->stack)) {
163
            $this->model = $this->model->groupBy($this->stack['groupBy']);
164
        }
165
        return $this;
166
    }
167
168
    /**
169
     * Get data
170
     *
171
     * @return Collection
172
     */
173 View Code Duplication
    private function get()
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...
174
    {
175
        if (array_key_exists('paginate', $this->stack)) {
176
            return $this->model->paginate($this->stack['paginate']);
177
        }
178
179
        return $this->model->get();
180
    }
181
}