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 | declare(strict_types=1); |
||
4 | |||
5 | namespace Chubbyphp\Model\Collection; |
||
6 | |||
7 | use Chubbyphp\Model\ModelInterface; |
||
8 | use Chubbyphp\Model\ResolverInterface; |
||
9 | use Chubbyphp\Model\ModelSortTrait; |
||
10 | |||
11 | final class LazyModelCollection implements ModelCollectionInterface |
||
12 | { |
||
13 | use ModelSortTrait; |
||
14 | |||
15 | /** |
||
16 | * @var ResolverInterface |
||
17 | */ |
||
18 | private $resolver; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $modelClass; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $foreignField; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $foreignId; |
||
34 | |||
35 | /** |
||
36 | * @var array|null |
||
37 | */ |
||
38 | private $orderBy; |
||
39 | |||
40 | /** |
||
41 | * @var bool |
||
42 | */ |
||
43 | private $resolved = false; |
||
44 | |||
45 | /** |
||
46 | * @var ModelInterface[]|array |
||
47 | */ |
||
48 | private $initialModels; |
||
49 | |||
50 | /** |
||
51 | * @var ModelInterface[]|array |
||
52 | */ |
||
53 | private $models; |
||
54 | |||
55 | /** |
||
56 | * @var \ReflectionProperty |
||
57 | */ |
||
58 | private $propertyReflection; |
||
59 | |||
60 | /** |
||
61 | * @param ResolverInterface $resolver |
||
62 | * @param string $modelClass |
||
63 | * @param string $foreignField |
||
64 | * @param string $foreignId |
||
65 | * @param array|null $orderBy |
||
66 | */ |
||
67 | 13 | View Code Duplication | public function __construct( |
0 ignored issues
–
show
|
|||
68 | ResolverInterface $resolver, |
||
69 | string $modelClass, |
||
70 | string $foreignField, |
||
71 | string $foreignId, |
||
72 | array $orderBy = null |
||
73 | ) { |
||
74 | 13 | $this->resolver = $resolver; |
|
75 | 13 | $this->modelClass = $modelClass; |
|
76 | 13 | $this->foreignField = $foreignField; |
|
77 | 13 | $this->foreignId = $foreignId; |
|
78 | 13 | $this->orderBy = $orderBy; |
|
79 | |||
80 | 13 | $this->propertyReflection = new \ReflectionProperty($this->modelClass, $this->foreignField); |
|
81 | 13 | $this->propertyReflection->setAccessible(true); |
|
82 | 13 | } |
|
83 | |||
84 | 10 | private function resolveModels() |
|
85 | { |
||
86 | 10 | if ($this->resolved) { |
|
87 | 10 | return; |
|
88 | } |
||
89 | |||
90 | 10 | $this->resolved = true; |
|
91 | |||
92 | 10 | $criteria = [$this->foreignField => $this->foreignId]; |
|
93 | |||
94 | 10 | $models = []; |
|
95 | 10 | foreach ($this->resolver->findBy($this->modelClass, $criteria, $this->orderBy) as $model) { |
|
96 | 5 | $models[$model->getId()] = $model; |
|
97 | } |
||
98 | |||
99 | 10 | $this->initialModels = $models; |
|
100 | 10 | $this->models = $models; |
|
101 | 10 | } |
|
102 | |||
103 | /** |
||
104 | * @param ModelInterface $model |
||
105 | * |
||
106 | * @return ModelCollectionInterface |
||
107 | */ |
||
108 | 1 | public function addModel(ModelInterface $model): ModelCollectionInterface |
|
109 | { |
||
110 | 1 | $this->resolveModels(); |
|
111 | |||
112 | 1 | $this->propertyReflection->setValue($model, $this->foreignId); |
|
113 | |||
114 | 1 | $this->models[$model->getId()] = $model; |
|
115 | |||
116 | 1 | return $this; |
|
117 | } |
||
118 | |||
119 | /** |
||
120 | * @param ModelInterface $model |
||
121 | * |
||
122 | * @return ModelCollectionInterface |
||
123 | */ |
||
124 | 1 | View Code Duplication | public function removeModel(ModelInterface $model): ModelCollectionInterface |
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...
|
|||
125 | { |
||
126 | 1 | $this->resolveModels(); |
|
127 | |||
128 | 1 | if (isset($this->models[$model->getId()])) { |
|
129 | 1 | $this->propertyReflection->setValue($model, null); |
|
130 | |||
131 | 1 | unset($this->models[$model->getId()]); |
|
132 | } |
||
133 | |||
134 | 1 | return $this; |
|
135 | } |
||
136 | |||
137 | /** |
||
138 | * @param ModelInterface[]|array $models |
||
139 | * |
||
140 | * @return ModelCollectionInterface |
||
141 | */ |
||
142 | 1 | View Code Duplication | public function setModels(array $models): ModelCollectionInterface |
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...
|
|||
143 | { |
||
144 | 1 | $this->resolveModels(); |
|
145 | |||
146 | 1 | foreach ($this->models as $model) { |
|
147 | 1 | $this->removeModel($model); |
|
148 | } |
||
149 | 1 | foreach ($models as $model) { |
|
150 | 1 | $this->addModel($model); |
|
151 | } |
||
152 | |||
153 | 1 | return $this; |
|
154 | } |
||
155 | |||
156 | /** |
||
157 | * @return ModelInterface[]|array |
||
158 | */ |
||
159 | 3 | public function getModels(): array |
|
160 | { |
||
161 | 3 | $this->resolveModels(); |
|
162 | |||
163 | 3 | return $this->sort($this->modelClass, array_values($this->models), $this->orderBy); |
|
164 | } |
||
165 | |||
166 | /** |
||
167 | * @return ModelInterface[]|array |
||
168 | */ |
||
169 | 1 | public function getInitialModels(): array |
|
170 | { |
||
171 | 1 | $this->resolveModels(); |
|
172 | |||
173 | 1 | return array_values($this->initialModels); |
|
174 | } |
||
175 | |||
176 | /** |
||
177 | * @return \ArrayIterator |
||
178 | */ |
||
179 | 1 | public function getIterator() |
|
180 | { |
||
181 | 1 | $this->resolveModels(); |
|
182 | |||
183 | 1 | return new \ArrayIterator($this->getModels()); |
|
184 | } |
||
185 | |||
186 | /** |
||
187 | * @return int |
||
188 | */ |
||
189 | 1 | public function count() |
|
190 | { |
||
191 | 1 | $this->resolveModels(); |
|
192 | |||
193 | 1 | return count($this->getModels()); |
|
194 | } |
||
195 | |||
196 | /** |
||
197 | * @return array |
||
198 | */ |
||
199 | 2 | View Code Duplication | public function jsonSerialize(): array |
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...
|
|||
200 | { |
||
201 | 2 | $this->jsonSerializableOrException(); |
|
202 | |||
203 | 1 | $this->resolveModels(); |
|
204 | |||
205 | 1 | $serializedModels = []; |
|
206 | 1 | foreach ($this->getModels() as $model) { |
|
207 | 1 | $serializedModels[] = $model->jsonSerialize(); |
|
208 | } |
||
209 | |||
210 | 1 | return $serializedModels; |
|
211 | } |
||
212 | |||
213 | /** |
||
214 | * @throws \LogicException |
||
215 | */ |
||
216 | 2 | View Code Duplication | private function jsonSerializableOrException() |
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...
|
|||
217 | { |
||
218 | 2 | $reflectionClass = new \ReflectionClass($this->modelClass); |
|
219 | 2 | if (!$reflectionClass->implementsInterface(\JsonSerializable::class)) { |
|
220 | 1 | throw new \LogicException( |
|
221 | 1 | sprintf('Model %s does not implement %s', $this->modelClass, \JsonSerializable::class) |
|
222 | ); |
||
223 | } |
||
224 | 1 | } |
|
225 | |||
226 | /** |
||
227 | * @return string |
||
228 | */ |
||
229 | 1 | public function getForeignField(): string |
|
230 | { |
||
231 | 1 | return $this->foreignField; |
|
232 | } |
||
233 | |||
234 | /** |
||
235 | * @return string |
||
236 | */ |
||
237 | 1 | public function getForeignId(): string |
|
238 | { |
||
239 | 1 | return $this->foreignId; |
|
240 | } |
||
241 | } |
||
242 |
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.