Conditions | 29 |
Paths | 7 |
Total Lines | 95 |
Code Lines | 62 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
186 | private function recursiveSerialize($entity, $modelName, $level = 0, $context = []) |
||
187 | { |
||
188 | $classMetadata = $this->mapping->getClassMetadata($modelName); |
||
189 | |||
190 | if ($level > 0 && empty($context['serializeRelation'])) { |
||
191 | $idAttribute = $classMetadata->getIdentifierAttribute(); |
||
192 | $getter = 'get' . ucfirst($idAttribute->getAttributeName()); |
||
193 | $tmpId = $entity->{$getter}(); |
||
194 | if ($tmpId) { |
||
195 | return $tmpId; |
||
196 | } |
||
197 | } |
||
198 | |||
199 | $attributeList = $classMetadata->getAttributeList(); |
||
200 | |||
201 | $out = []; |
||
202 | if (!empty($attributeList)) { |
||
203 | foreach ($attributeList as $attribute) { |
||
204 | $method = 'get' . ucfirst($attribute->getAttributeName()); |
||
205 | |||
206 | if ($attribute->isIdentifier() && !$entity->$method()) { |
||
207 | continue; |
||
208 | } |
||
209 | $relation = $classMetadata->getRelation($attribute->getSerializedKey()); |
||
210 | |||
211 | $data = $entity->$method(); |
||
212 | |||
213 | if (null === $data && $relation && $relation->isManyToOne() && $level > 0) { |
||
214 | /* |
||
215 | We only serialize the root many-to-one relations to prevent, hopefully, |
||
216 | unlinked and/or duplicated content. For instance, a cart with cartItemList containing |
||
217 | null values for the cart [{ cart => null, ... }] may lead the creation of |
||
218 | CartItem entities explicitly bound to a null Cart instead of the created/updated Cart. |
||
219 | */ |
||
220 | continue; |
||
221 | } elseif ($data instanceof \DateTime) { |
||
222 | $data = $data->format('c'); |
||
223 | } elseif (is_object($data) && get_class($data) === "libphonenumber\PhoneNumber") { |
||
224 | $phoneNumberUtil = PhoneNumberUtil::getInstance(); |
||
225 | $data = $phoneNumberUtil->format( |
||
226 | $data, |
||
227 | PhoneNumberFormat::INTERNATIONAL |
||
228 | ); |
||
229 | } elseif (is_object($data) |
||
230 | && $relation |
||
231 | && $this->mapping->hasClassMetadata($relation->getTargetEntity()) |
||
232 | ) { |
||
233 | $idAttribute = $this->mapping |
||
234 | ->getClassMetadata($relation->getTargetEntity()) |
||
235 | ->getIdentifierAttribute() |
||
236 | ->getAttributeName(); |
||
237 | $idGetter = 'get' . ucfirst($idAttribute); |
||
238 | |||
239 | if (method_exists($data, $idGetter) && $data->{$idGetter}()) { |
||
240 | $data = $data->{$idGetter}(); |
||
241 | } elseif ($relation->isManyToOne()) { |
||
242 | if ($level > 0) { |
||
243 | continue; |
||
244 | } else { |
||
245 | throw new SdkException('Case not allowed for now'); |
||
246 | } |
||
247 | } |
||
248 | } elseif (is_array($data)) { |
||
249 | $newData = []; |
||
250 | foreach ($data as $key => $item) { |
||
251 | if ($item instanceof \DateTime) { |
||
252 | $newData[$key] = $item->format('c'); |
||
253 | } elseif (is_object($item) && |
||
254 | $relation && |
||
255 | $this->mapping->hasClassMetadata($relation->getTargetEntity()) |
||
256 | ) { |
||
257 | $serializeRelation = !empty($context['serializeRelations']) |
||
258 | && in_array($relation->getSerializedKey(), $context['serializeRelations']); |
||
259 | |||
260 | $newData[$key] = $this->recursiveSerialize( |
||
261 | $item, |
||
262 | $relation->getTargetEntity(), |
||
263 | $level + 1, |
||
264 | [ 'serializeRelation' => $serializeRelation ] |
||
265 | ); |
||
266 | } else { |
||
267 | $newData[$key] = $item; |
||
268 | } |
||
269 | } |
||
270 | $data = $newData; |
||
271 | } |
||
272 | |||
273 | $key = $attribute->getSerializedKey(); |
||
274 | |||
275 | $out[$key] = $data; |
||
276 | } |
||
277 | } |
||
278 | |||
279 | return $out; |
||
280 | } |
||
281 | |||
302 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: