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