This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
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
|
|||
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 Adding the ![]() |
|||
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 |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
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.