Conditions | 7 |
Paths | 7 |
Total Lines | 59 |
Code Lines | 37 |
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 |
||
97 | private function hydrateAssociation($field, $entity, $source) |
||
98 | { |
||
99 | $accessor = new PropertyAccessor(); |
||
100 | $targetClassName = $this->metadata->getAssociationTargetClass($field); |
||
101 | $mapping = $this->metadata->getAssociationMapping($field); |
||
102 | $targetPersister = $this->manager->getUnitOfWork()->getEntityPersister($targetClassName); |
||
103 | $targetMetadata = $this->manager->getClassMetadata($mapping['target']); |
||
104 | $apiField = $mapping['api_field']; |
||
105 | $field = $mapping['field']; |
||
106 | $oid = spl_object_hash($entity); |
||
107 | |||
108 | if ($this->metadata->isSingleValuedAssociation($field)) { |
||
109 | $identifiers = $this->metadata->getIdentifierValues($entity); |
||
110 | if ($mapping['isOwningSide']) { |
||
111 | try { |
||
112 | $value = $accessor->getValue($source, $apiField); |
||
113 | } catch (NoSuchPropertyException $exception) { |
||
114 | if ($mapping['nullable']) { |
||
115 | $this->manager->getUnitOfWork()->setOriginalEntityProperty($oid, $field, null); |
||
116 | |||
117 | return null; |
||
118 | } |
||
119 | |||
120 | throw new HydrationException( |
||
121 | sprintf('Api field %s for property %s does not present in response', $apiField, $field) |
||
122 | ); |
||
123 | } |
||
124 | |||
125 | if ($targetMetadata->isIdentifierComposite()) { |
||
126 | throw new HydrationException('Composite references not supported'); |
||
127 | } |
||
128 | |||
129 | $targetIdsNames = $targetMetadata->getIdentifierFieldNames(); |
||
130 | $targetIdName = array_shift($targetIdsNames); |
||
131 | $type = $this->manager |
||
132 | ->getConfiguration() |
||
133 | ->getTypeRegistry() |
||
134 | ->get($targetMetadata->getTypeOfField($targetIdName)); |
||
135 | |||
136 | $identifiers = [$targetIdName => $type->fromApiValue($value)]; |
||
137 | } |
||
138 | |||
139 | $newValue = $targetPersister->getToOneEntity($mapping, $entity, $identifiers); |
||
140 | |||
141 | $this->manager->getUnitOfWork()->setOriginalEntityProperty($oid, $field, $newValue); |
||
142 | |||
143 | return $newValue; |
||
144 | } |
||
145 | |||
146 | if ($this->metadata->isCollectionValuedAssociation($field)) { |
||
147 | $newValue = $targetPersister->getOneToManyCollection($mapping, $entity); |
||
148 | |||
149 | $this->manager->getUnitOfWork()->setOriginalEntityProperty($oid, $field, $newValue); |
||
150 | |||
151 | return $newValue; |
||
152 | } |
||
153 | |||
154 | throw new MappingException('Invalid metadata association type'); |
||
155 | } |
||
156 | } |
||
157 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.