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 |
||
180 | private function recursiveSerialize($entity, $modelName, $level = 0, $context = []) |
||
181 | { |
||
182 | $classMetadata = $this->mapping->getClassMetadata($modelName); |
||
183 | |||
184 | if ($level > 0 && empty($context['serializeRelation'])) { |
||
185 | $idAttribute = $classMetadata->getIdentifierAttribute(); |
||
186 | $getter = 'get' . ucfirst($idAttribute->getAttributeName()); |
||
187 | $tmpId = $entity->{$getter}(); |
||
188 | if ($tmpId) { |
||
189 | return $tmpId; |
||
190 | } |
||
191 | } |
||
192 | |||
193 | $attributeList = $classMetadata->getAttributeList(); |
||
194 | |||
195 | $out = []; |
||
196 | if (!empty($attributeList)) { |
||
197 | foreach ($attributeList as $attribute) { |
||
198 | $method = 'get' . ucfirst($attribute->getAttributeName()); |
||
199 | |||
200 | if ($attribute->isIdentifier() && !$entity->$method()) { |
||
201 | continue; |
||
202 | } |
||
203 | $relation = $classMetadata->getRelation($attribute->getSerializedKey()); |
||
204 | |||
205 | $data = $entity->$method(); |
||
206 | |||
207 | if (null === $data && $relation && $relation->isManyToOne() && $level > 0) { |
||
208 | /* |
||
209 | We only serialize the root many-to-one relations to prevent, hopefully, |
||
210 | unlinked and/or duplicated content. For instance, a cart with cartItemList containing |
||
211 | null values for the cart [{ cart => null, ... }] may lead the creation of |
||
212 | CartItem entities explicitly bound to a null Cart instead of the created/updated Cart. |
||
213 | */ |
||
214 | continue; |
||
215 | } elseif ($data instanceof \DateTime) { |
||
216 | $data = $data->format('c'); |
||
217 | } elseif (is_object($data) && get_class($data) === "libphonenumber\PhoneNumber") { |
||
218 | $phoneNumberUtil = PhoneNumberUtil::getInstance(); |
||
219 | $data = $phoneNumberUtil->format( |
||
220 | $data, |
||
221 | PhoneNumberFormat::INTERNATIONAL |
||
222 | ); |
||
223 | } elseif (is_object($data) |
||
224 | && $relation |
||
225 | && $this->mapping->hasClassMetadata($relation->getTargetEntity()) |
||
226 | ) { |
||
227 | $idAttribute = $this->mapping |
||
228 | ->getClassMetadata($relation->getTargetEntity()) |
||
229 | ->getIdentifierAttribute() |
||
230 | ->getAttributeName(); |
||
231 | $idGetter = 'get' . ucfirst($idAttribute); |
||
232 | |||
233 | if (method_exists($data, $idGetter) && $data->{$idGetter}()) { |
||
234 | $data = $data->{$idGetter}(); |
||
235 | } elseif ($relation->isManyToOne()) { |
||
236 | if ($level > 0) { |
||
237 | continue; |
||
238 | } else { |
||
239 | throw new SdkException('Case not allowed for now'); |
||
240 | } |
||
241 | } |
||
242 | } elseif (is_array($data)) { |
||
243 | $newData = []; |
||
244 | foreach ($data as $key => $item) { |
||
245 | if ($item instanceof \DateTime) { |
||
246 | $newData[$key] = $item->format('c'); |
||
247 | } elseif (is_object($item) && |
||
248 | $relation && |
||
249 | $this->mapping->hasClassMetadata($relation->getTargetEntity()) |
||
250 | ) { |
||
251 | $serializeRelation = !empty($context['serializeRelations']) |
||
252 | && in_array($relation->getSerializedKey(), $context['serializeRelations']); |
||
253 | |||
254 | $newData[$key] = $this->recursiveSerialize( |
||
255 | $item, |
||
256 | $relation->getTargetEntity(), |
||
257 | $level + 1, |
||
258 | [ 'serializeRelation' => $serializeRelation ] |
||
259 | ); |
||
260 | } else { |
||
261 | $newData[$key] = $item; |
||
262 | } |
||
263 | } |
||
264 | $data = $newData; |
||
265 | } |
||
266 | |||
267 | $key = $attribute->getSerializedKey(); |
||
268 | |||
269 | $out[$key] = $data; |
||
270 | } |
||
271 | } |
||
272 | |||
273 | return $out; |
||
274 | } |
||
275 | |||
290 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.