Issues (38)

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.

src/services/traits/ObjectByString.php (2 issues)

Labels
Severity

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
/**
4
 * @copyright  Copyright (c) Flipbox Digital Limited
5
 * @license    https://github.com/flipbox/spark/blob/master/LICENSE
6
 * @link       https://github.com/flipbox/spark
7
 */
8
9
namespace flipbox\spark\services\traits;
10
11
use flipbox\spark\exceptions\ObjectNotFoundException;
12
use flipbox\spark\exceptions\RecordNotFoundException;
13
use flipbox\spark\records\Record;
14
use yii\base\Object as BaseObject;
15
16
/**
17
 * @author Flipbox Factory <[email protected]>
18
 * @since 2.0.0
19
 */
20
trait ObjectByString
21
{
22
23
    use Object;
24
25
    /**
26
     * @var BaseObject[]
27
     */
28
    protected $cacheByString = [];
29
30
    /**
31
     * @return string
32
     */
33
    abstract protected function stringProperty(): string;
34
35
    /**
36
     * @param Record $record
37
     * @param string|null $toScenario
38
     * @return BaseObject
39
     */
40
    abstract protected function findByRecord(Record $record, string $toScenario = null): BaseObject;
41
42
    /**
43
     * @return string
44
     */
45
    protected function recordStringProperty(): string
46
    {
47
        return $this->stringProperty();
48
    }
49
50
    /**
51
     * @param BaseObject $object
52
     * @return string
53
     */
54
    protected function stringValue(BaseObject $object)
55
    {
56
57
        $property = $this->stringProperty();
58
59
        return $object->{$property};
60
    }
61
62
    /*******************************************
63
     * FIND/GET BY STRING
64
     *******************************************/
65
66
    /**
67
     * @param string $string
68
     * @param string|null $toScenario
69
     * @return BaseObject|null
70
     */
71
    public function findByString(string $string, string $toScenario = null)
72
    {
73
74
        // Check cache
75
        if (!$model = $this->findCacheByString($string)) {
0 ignored issues
show
Are you sure the assignment to $model is correct as $this->findCacheByString($string) (which targets flipbox\spark\services\t...ng::findCacheByString()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
76
            // Find record in db
77
            if ($record = $this->findRecordByString($string)) {
78
                $model = $this->findByRecord($record, $toScenario);
79
            } else {
80
                $this->cacheByString[$string] = null;
81
82
                return null;
83
            }
84
        }
85
86
        return $model;
87
    }
88
89
    /**
90
     * @param string $string
91
     * @param string|null $toScenario
92
     * @return BaseObject|null
93
     * @throws ObjectNotFoundException
94
     */
95
    public function getByString(string $string, string $toScenario = null): BaseObject
96
    {
97
98
        if (!$model = $this->findByString($string, $toScenario)) {
99
            $this->notFoundByStringException($string);
100
        }
101
102
        return $model;
103
    }
104
105
    /**
106
     * @param string $string
107
     * @param string|null $toScenario
108
     * @return BaseObject|null
109
     */
110
    public function freshFindByString(string $string, string $toScenario = null)
111
    {
112
113
        // Find record in db
114
        if (!$record = $this->findRecordByString($string)) {
115
            return null;
116
        }
117
118
        return $this->createFromRecord($record, $toScenario);
0 ignored issues
show
It seems like createFromRecord() must be provided by classes using this trait. How about adding it as abstract method to this trait?

This check looks for methods that are used by a trait but not required by it.

To illustrate, let’s look at the following code example

trait Idable {
    public function equalIds(Idable $other) {
        return $this->getId() === $other->getId();
    }
}

The trait Idable provides a method equalsId that in turn relies on the method getId(). If this method does not exist on a class mixing in this trait, the method will fail.

Adding the getId() as an abstract method to the trait will make sure it is available.

Loading history...
119
    }
120
121
    /**
122
     * @param string $string
123
     * @param string|null $toScenario
124
     * @return BaseObject
125
     * @throws ObjectNotFoundException
126
     */
127
    public function freshGetByString(string $string, string $toScenario = null): BaseObject
128
    {
129
130
        if (!$model = $this->freshFindByString($string, $toScenario)) {
131
            $this->notFoundByStringException($string);
132
        }
133
134
        return $model;
135
    }
136
137
    /*******************************************
138
     * CACHE
139
     *******************************************/
140
141
    /**
142
     * Find an existing cache by string
143
     *
144
     * @param string $string
145
     * @return null
146
     */
147
    public function findCacheByString(string $string)
148
    {
149
150
        // Check if already in cache
151
        if (!$this->isCachedByString($string)) {
152
            return null;
153
        }
154
155
        return $this->cacheByString[$string];
156
    }
157
158
    /**
159
     * Identify whether in cache by string
160
     *
161
     * @param string $string
162
     * @return bool
163
     */
164
    private function isCachedByString(string $string): bool
165
    {
166
        return array_key_exists($string, $this->cacheByString);
167
    }
168
169
170
    /**
171
     * @param BaseObject $model
172
     * @return static
173
     */
174
    protected function cacheByString(BaseObject $model)
175
    {
176
177
        $stringValue = $this->stringValue($model);
178
179
        if (null === $stringValue) {
180
            return $this;
181
        }
182
183
        // Check if already in cache
184
        if (!$this->isCachedByString($stringValue)) {
185
            // Cache it
186
            $this->cacheByString[$stringValue] = $model;
187
        }
188
189
        return $this;
190
    }
191
192
    /**
193
     * @param Record $record
194
     * @return BaseObject|null
195
     */
196
    protected function findCacheByRecordByString(Record $record)
197
    {
198
199
        $property = $this->recordStringProperty();
200
201
        $stringValue = $record->{$property};
202
203
        if ($stringValue === null) {
204
            return null;
205
        }
206
207
        return $this->findCacheByString($stringValue);
208
    }
209
210
    /*******************************************
211
     * RECORD BY STRING
212
     *******************************************/
213
214
    /**
215
     * @param string $string
216
     * @param string|null $toScenario
217
     * @return Record|null
218
     */
219
    public function findRecordByString(string $string, string $toScenario = null)
220
    {
221
222
        return $this->findRecordByCondition(
223
            [
224
                $this->recordStringProperty() => $string
225
            ],
226
            $toScenario
227
        );
228
    }
229
230
    /**
231
     * @param string $string
232
     * @param string|null $toScenario
233
     * @throws RecordNotFoundException
234
     * @return Record|null
235
     */
236
    public function getRecordByString(string $string, string $toScenario = null)
237
    {
238
239
        if (!$record = $this->findRecordByString($string, $toScenario)) {
240
            $this->notFoundRecordByStringException($string);
241
        }
242
243
        return $record;
244
    }
245
246
    /*******************************************
247
     * EXCEPTIONS
248
     *******************************************/
249
250
    /**
251
     * @param string|null $string
252
     * @throws ObjectNotFoundException
253
     */
254
    protected function notFoundByStringException(string $string = null)
255
    {
256
257
        throw new ObjectNotFoundException(
258
            sprintf(
259
                'Object does not exist with the string "%s".',
260
                (string)$string
261
            )
262
        );
263
    }
264
265
    /**
266
     * @param string|null $string
267
     * @throws RecordNotFoundException
268
     */
269
    protected function notFoundRecordByStringException(string $string = null)
270
    {
271
272
        throw new RecordNotFoundException(
273
            sprintf(
274
                'Record does not exist with the string "%s".',
275
                (string)$string
276
            )
277
        );
278
    }
279
}
280