Conditions | 29 |
Paths | 53 |
Total Lines | 94 |
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 |
||
152 | private function readExtendedMetadata(ORM\Mapping\ClassMetadata $metadata, array $config) : array |
||
153 | { |
||
154 | $class = $metadata->getReflectionClass(); |
||
155 | |||
156 | // Create doctrine annotation reader |
||
157 | $reader = $this->getDefaultAnnotationReader(); |
||
158 | |||
159 | // Property annotations |
||
160 | foreach ($class->getProperties() as $property) { |
||
161 | if ($metadata->isMappedSuperclass && $property->isPrivate() === FALSE || |
||
162 | $metadata->isInheritedField($property->getName()) || |
||
163 | isset($metadata->associationMappings[$property->getName()]['inherited']) |
||
164 | ) { |
||
165 | continue; |
||
166 | } |
||
167 | |||
168 | /** @var Mapping\Annotation\Blameable $blameable */ |
||
169 | if ($blameable = $reader->getPropertyAnnotation($property, self::EXTENSION_ANNOTATION)) { |
||
170 | $field = $property->getName(); |
||
171 | |||
172 | // No map field nor association |
||
173 | if ($metadata->hasField($field) === FALSE && $metadata->hasAssociation($field) === FALSE && $this->configuration->useLazyAssociation() === FALSE) { |
||
174 | if ($this->configuration->autoMapField) { |
||
175 | if ($this->configuration->automapWithAssociation()) { |
||
176 | $entityMap = [ |
||
177 | 'targetEntity' => $this->configuration->userEntity, |
||
178 | 'fieldName' => $field, |
||
179 | 'joinColumns' => [ |
||
180 | [ |
||
181 | 'onDelete' => 'SET NULL', |
||
182 | ], |
||
183 | ], |
||
184 | ]; |
||
185 | |||
186 | if (isset($blameable->association['column']) && $blameable->association['column'] !== NULL) { |
||
187 | $entityMap['joinColumns'][0]['name'] = $blameable->columnName; |
||
188 | } |
||
189 | |||
190 | if (isset($blameable->association['referencedColumn']) && $blameable->association['referencedColumn'] !== NULL) { |
||
191 | $entityMap['joinColumns'][0]['referencedColumnName'] = $blameable->referencedColumnName; |
||
192 | } |
||
193 | |||
194 | $metadata->mapManyToOne($entityMap); |
||
195 | |||
196 | } elseif ($this->configuration->automapWithField()) { |
||
197 | $metadata->mapField([ |
||
198 | 'fieldName' => $field, |
||
199 | 'type' => 'string', |
||
200 | 'nullable' => TRUE, |
||
201 | ]); |
||
202 | |||
203 | } else { |
||
204 | throw new Exceptions\InvalidMappingException("Unable to find blameable [{$field}] as mapped property in entity - {$metadata->getName()}"); |
||
205 | } |
||
206 | |||
207 | } else { |
||
208 | throw new Exceptions\InvalidMappingException("Unable to find blameable [{$field}] as mapped property in entity - {$metadata->getName()}"); |
||
209 | } |
||
210 | } |
||
211 | |||
212 | if ($metadata->hasField($field) && $this->isValidField($metadata, $field) === FALSE && $this->configuration->useLazyAssociation() === FALSE) { |
||
213 | throw new Exceptions\InvalidMappingException("Field - [{$field}] type is not valid and must be 'string' or a one-to-many relation in class - {$metadata->getName()}"); |
||
214 | |||
215 | } elseif ($metadata->hasAssociation($field) && $metadata->isSingleValuedAssociation($field) === FALSE && $this->configuration->useLazyAssociation() === FALSE) { |
||
216 | throw new Exceptions\InvalidMappingException("Association - [{$field}] is not valid, it must be a one-to-many relation or a string field - {$metadata->getName()}"); |
||
217 | } |
||
218 | |||
219 | // Check for valid events |
||
220 | if (!in_array($blameable->on, ['update', 'create', 'change', 'delete'])) { |
||
221 | throw new Exceptions\InvalidMappingException("Field - [{$field}] trigger 'on' is not one of [update, create, change] in class - {$metadata->getName()}"); |
||
222 | } |
||
223 | |||
224 | if ($blameable->on === 'change') { |
||
225 | if (!isset($blameable->field)) { |
||
226 | throw new Exceptions\InvalidMappingException("Missing parameters on property - {$field}, field must be set on [change] trigger in class - {$metadata->getName()}"); |
||
227 | } |
||
228 | |||
229 | if (is_array($blameable->field) && isset($blameable->value)) { |
||
230 | throw new Exceptions\InvalidMappingException("Blameable extension does not support multiple value changeset detection yet."); |
||
231 | } |
||
232 | |||
233 | $field = [ |
||
234 | 'field' => $field, |
||
235 | 'trackedField' => $blameable->field, |
||
236 | 'value' => is_array($blameable->value) ? $blameable->value : [$blameable->value], |
||
237 | ]; |
||
238 | } |
||
239 | |||
240 | $config[$blameable->on][] = $field; |
||
241 | } |
||
242 | } |
||
243 | |||
244 | return $config; |
||
245 | } |
||
246 | |||
351 |