flipbox /
spark
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\ModelNotFoundException; |
||
| 12 | use flipbox\spark\exceptions\RecordNotFoundException; |
||
| 13 | use flipbox\spark\models\Model as BaseModel; |
||
| 14 | use flipbox\spark\models\ModelWithId; |
||
| 15 | use flipbox\spark\records\Record; |
||
| 16 | use flipbox\spark\records\RecordWithId; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @author Flipbox Factory <[email protected]> |
||
| 20 | * @since 2.0.0 |
||
| 21 | * |
||
| 22 | * @method RecordWithId|null findRecordByCondition($condition, string $toScenario = null) |
||
| 23 | */ |
||
| 24 | trait ModelById |
||
| 25 | { |
||
| 26 | |||
| 27 | use Model; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var ModelWithId[] |
||
| 31 | */ |
||
| 32 | protected $cacheById = []; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @param Record $record |
||
| 36 | * @param string|null $toScenario |
||
| 37 | * @return BaseModel|ModelWithId |
||
| 38 | */ |
||
| 39 | abstract protected function findByRecord(Record $record, string $toScenario = null): BaseModel; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param array $config |
||
| 43 | * @param string|null $toScenario |
||
| 44 | * @return BaseModel|ModelWithId |
||
| 45 | */ |
||
| 46 | abstract public function create($config = [], string $toScenario = null): BaseModel; |
||
| 47 | |||
| 48 | /******************************************* |
||
| 49 | * FIND/GET BY ID |
||
| 50 | *******************************************/ |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param int $id |
||
| 54 | * @param string|null $toScenario |
||
| 55 | * @return ModelWithId|null |
||
| 56 | */ |
||
| 57 | public function findById(int $id, string $toScenario = null) |
||
| 58 | { |
||
| 59 | |||
| 60 | // Check cache |
||
| 61 | if (!$model = $this->findCacheById($id)) { |
||
|
0 ignored issues
–
show
|
|||
| 62 | // Find record in db |
||
| 63 | if ($record = $this->findRecordById($id)) { |
||
| 64 | $model = $this->findByRecord($record, $toScenario); |
||
| 65 | } else { |
||
| 66 | $this->cacheById[$id] = null; |
||
| 67 | |||
| 68 | return null; |
||
| 69 | } |
||
| 70 | } |
||
| 71 | |||
| 72 | return $model; |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @param int $id |
||
| 77 | * @param string|null $toScenario |
||
| 78 | * @return ModelWithId|null |
||
| 79 | * @throws ModelNotFoundException |
||
| 80 | */ |
||
| 81 | public function getById(int $id, string $toScenario = null): ModelWithId |
||
| 82 | { |
||
| 83 | |||
| 84 | if (!$model = $this->findById($id, $toScenario)) { |
||
| 85 | $this->notFoundByIdException($id); |
||
| 86 | } |
||
| 87 | |||
| 88 | return $model; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @param int $id |
||
| 93 | * @param string|null $toScenario |
||
| 94 | * @return ModelWithId|null |
||
| 95 | */ |
||
| 96 | public function freshFindById(int $id, string $toScenario = null) |
||
| 97 | { |
||
| 98 | |||
| 99 | // Find record in db |
||
| 100 | if (!$record = $this->findRecordById($id)) { |
||
| 101 | return null; |
||
| 102 | } |
||
| 103 | |||
| 104 | $model = $this->create($record, $toScenario); |
||
|
0 ignored issues
–
show
$record is of type object<flipbox\spark\records\RecordWithId>, but the function expects a array.
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
| 105 | |||
| 106 | return $model; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @param int $id |
||
| 111 | * @param string|null $toScenario |
||
| 112 | * @return ModelWithId |
||
| 113 | * @throws ModelNotFoundException |
||
| 114 | */ |
||
| 115 | public function freshGetById(int $id, string $toScenario = null): ModelWithId |
||
| 116 | { |
||
| 117 | |||
| 118 | if (!$model = $this->freshFindById($id, $toScenario)) { |
||
| 119 | $this->notFoundByIdException($id); |
||
| 120 | } |
||
| 121 | |||
| 122 | return $model; |
||
| 123 | } |
||
| 124 | |||
| 125 | /******************************************* |
||
| 126 | * CACHE |
||
| 127 | *******************************************/ |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Find an existing cache by id |
||
| 131 | * |
||
| 132 | * @param int $id |
||
| 133 | * @return null |
||
| 134 | */ |
||
| 135 | public function findCacheById(int $id) |
||
| 136 | { |
||
| 137 | |||
| 138 | // Check if already in cache |
||
| 139 | if (!$this->isCachedById($id)) { |
||
| 140 | return null; |
||
| 141 | } |
||
| 142 | |||
| 143 | return $this->cacheById[$id]; |
||
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * Identify whether in cache by id |
||
| 148 | * |
||
| 149 | * @param int $id |
||
| 150 | * @return bool |
||
| 151 | */ |
||
| 152 | private function isCachedById(int $id): bool |
||
| 153 | { |
||
| 154 | return array_key_exists($id, $this->cacheById); |
||
| 155 | } |
||
| 156 | |||
| 157 | |||
| 158 | /** |
||
| 159 | * @param ModelWithId $model |
||
| 160 | * @return static |
||
| 161 | */ |
||
| 162 | protected function cacheById(ModelWithId $model) |
||
| 163 | { |
||
| 164 | |||
| 165 | if (null === $model->getId()) { |
||
| 166 | return $this; |
||
| 167 | } |
||
| 168 | |||
| 169 | // Check if already in cache |
||
| 170 | if (!$this->isCachedById($model->getId())) { |
||
| 171 | // Cache it |
||
| 172 | $this->cacheById[$model->getId()] = $model; |
||
| 173 | } |
||
| 174 | |||
| 175 | return $this; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @param RecordWithId $record |
||
| 180 | * @return ModelWithId|null |
||
| 181 | */ |
||
| 182 | protected function findCacheByRecordById(RecordWithId $record) |
||
| 183 | { |
||
| 184 | |||
| 185 | $value = $record->id; |
||
| 186 | |||
| 187 | if ($value === null) { |
||
| 188 | return null; |
||
| 189 | } |
||
| 190 | |||
| 191 | return $this->findCacheById($value); |
||
| 192 | } |
||
| 193 | |||
| 194 | /******************************************* |
||
| 195 | * RECORD BY ID |
||
| 196 | *******************************************/ |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @param int $id |
||
| 200 | * @param string|null $toScenario |
||
| 201 | * @return RecordWithId|null |
||
| 202 | */ |
||
| 203 | public function findRecordById(int $id, string $toScenario = null) |
||
| 204 | { |
||
| 205 | |||
| 206 | return $this->findRecordByCondition( |
||
| 207 | [ |
||
| 208 | 'id' => $id |
||
| 209 | ], |
||
| 210 | $toScenario |
||
| 211 | ); |
||
| 212 | } |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param int $id |
||
| 216 | * @param string|null $toScenario |
||
| 217 | * @return Record|RecordWithId|null |
||
| 218 | */ |
||
| 219 | public function getRecordById(int $id, string $toScenario = null): Record |
||
| 220 | { |
||
| 221 | |||
| 222 | if (!$record = $this->findRecordById($id, $toScenario)) { |
||
| 223 | $this->notFoundRecordByIdException($id); |
||
| 224 | } |
||
| 225 | |||
| 226 | return $record; |
||
| 227 | } |
||
| 228 | |||
| 229 | |||
| 230 | /******************************************* |
||
| 231 | * EXCEPTIONS |
||
| 232 | *******************************************/ |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param int|null $id |
||
| 236 | * @throws ModelNotFoundException |
||
| 237 | */ |
||
| 238 | protected function notFoundByIdException(int $id = null) |
||
| 239 | { |
||
| 240 | |||
| 241 | throw new ModelNotFoundException( |
||
| 242 | sprintf( |
||
| 243 | 'Model does not exist with the id "%s".', |
||
| 244 | (string)$id |
||
| 245 | ) |
||
| 246 | ); |
||
| 247 | } |
||
| 248 | |||
| 249 | /** |
||
| 250 | * @param int|null $id |
||
| 251 | * @throws RecordNotFoundException |
||
| 252 | */ |
||
| 253 | protected function notFoundRecordByIdException(int $id = null) |
||
| 254 | { |
||
| 255 | |||
| 256 | throw new RecordNotFoundException( |
||
| 257 | sprintf( |
||
| 258 | 'Record does not exist with the id "%s".', |
||
| 259 | (string)$id |
||
| 260 | ) |
||
| 261 | ); |
||
| 262 | } |
||
| 263 | } |
||
| 264 |
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.