| Conditions | 16 |
| Paths | 36 |
| Total Lines | 68 |
| Code Lines | 38 |
| 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 |
||
| 78 | public function deserialize(array $data, $className) |
||
| 79 | { |
||
| 80 | $className = $this->resolveRealClassName($data, $className); |
||
| 81 | |||
| 82 | $classMetadata = $this->mapping->getClassMetadata($className); |
||
| 83 | $identifierAttribute = $classMetadata->getIdentifierAttribute(); |
||
| 84 | $identifierAttrKey = $identifierAttribute ? $identifierAttribute->getSerializedKey() : null; |
||
| 85 | |||
| 86 | $attributeList = $classMetadata->getAttributeList(); |
||
| 87 | |||
| 88 | $instance = new $className(); |
||
| 89 | |||
| 90 | foreach ($attributeList as $attribute) { |
||
| 91 | $key = $attribute->getSerializedKey(); |
||
| 92 | |||
| 93 | if (!ArrayHelper::arrayHas($data, $key)) { |
||
| 94 | continue; |
||
| 95 | } |
||
| 96 | |||
| 97 | $value = ArrayHelper::arrayGet($data, $key); |
||
| 98 | |||
| 99 | |||
| 100 | $setter = 'set' . ucfirst($attribute->getAttributeName()); |
||
| 101 | |||
| 102 | if (method_exists($instance, $setter)) { |
||
| 103 | $relation = $classMetadata->getRelation($key); |
||
| 104 | if ($relation) { |
||
| 105 | if (is_string($value)) { |
||
| 106 | $value = $this->sdk->createProxy($value); |
||
| 107 | } elseif (is_array($value)) { |
||
| 108 | if (isset($value[$identifierAttrKey])) { |
||
| 109 | $key = $this->mapping->getKeyFromId($value[$identifierAttrKey]); |
||
|
|
|||
| 110 | $subClassMetadata = $this->getClassMetadataFromId($value[$identifierAttrKey]); |
||
| 111 | $value = $this->deserialize($value, $subClassMetadata->getModelName()); |
||
| 112 | } else { |
||
| 113 | $list = []; |
||
| 114 | foreach ($value as $item) { |
||
| 115 | if (is_string($item)) { |
||
| 116 | $list[] = $this->sdk->createProxy($item); |
||
| 117 | } elseif (is_array($item) && isset($item[$identifierAttrKey])) { |
||
| 118 | // not tested for now |
||
| 119 | // /the $identifierAttrKey is not the real identifier, as it is |
||
| 120 | // the main object identifier, but we do not have the metadada for now |
||
| 121 | // the thing we assume now is that every entity "may" have the same key |
||
| 122 | // as identifier |
||
| 123 | $key = $this->mapping->getKeyFromId($item[$identifierAttrKey]); |
||
| 124 | $subClassMetadata = $this->getClassMetadataFromId($item[$identifierAttrKey]); |
||
| 125 | $list[] = $this->deserialize($item, $subClassMetadata->getModelName()); |
||
| 126 | } |
||
| 127 | } |
||
| 128 | |||
| 129 | $value = $list; |
||
| 130 | } |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | if (isset($value)) { |
||
| 135 | if ($attribute && $attribute->getType() === 'datetime') { |
||
| 136 | $value = new \DateTime($value); |
||
| 137 | } |
||
| 138 | |||
| 139 | $instance->$setter($value); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | } |
||
| 143 | |||
| 144 | return $instance; |
||
| 145 | } |
||
| 146 | |||
| 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.