| Conditions | 10 |
| Paths | 21 |
| Total Lines | 43 |
| Code Lines | 23 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 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 |
||
| 23 | public function getFields(string $entityClass): array |
||
| 24 | { |
||
| 25 | $metadata = $this->entityManager->getClassMetadata($entityClass); |
||
| 26 | |||
| 27 | // As the Doctrine ClassMetadata interface does not expose any properties, we should check the instance of the |
||
| 28 | // returned metadata class to respect the good practices |
||
| 29 | if (!$metadata instanceof ClassMetadataInfo) { |
||
| 30 | return []; |
||
| 31 | } |
||
| 32 | $fieldNames = (array) $metadata->fieldNames; |
||
| 33 | $fields = []; |
||
| 34 | |||
| 35 | foreach ($fieldNames as $fieldName) { |
||
| 36 | // Remove the primary key field if it's not managed manually |
||
| 37 | if (!$metadata->isIdentifierNatural() && in_array($fieldName, $metadata->identifier)) { |
||
| 38 | continue; |
||
| 39 | } |
||
| 40 | $mapping = $metadata->getFieldMapping($fieldName); |
||
| 41 | $formOptions = []; |
||
| 42 | |||
| 43 | // When a field is defined as nullable in the Doctrine entity configuration, the associated form field |
||
| 44 | // should not be required neither |
||
| 45 | if (key_exists('nullable', $mapping) && true === $mapping['nullable']) { |
||
| 46 | $formOptions['required'] = false; |
||
| 47 | } |
||
| 48 | $fields[$fieldName] = new FieldDefinition($metadata->getTypeOfField($fieldName), [], $formOptions); |
||
|
|
|||
| 49 | } |
||
| 50 | |||
| 51 | foreach ($metadata->associationMappings as $fieldName => $relation) { |
||
| 52 | $formOptions = []; |
||
| 53 | $formType = 'choice'; |
||
| 54 | |||
| 55 | if (ClassMetadataInfo::MANY_TO_MANY === $relation['type']) { |
||
| 56 | $formOptions['expanded'] = true; |
||
| 57 | $formOptions['multiple'] = true; |
||
| 58 | } |
||
| 59 | if ($this->isJoinColumnNullable($relation)) { |
||
| 60 | $formOptions['required'] = false; |
||
| 61 | } |
||
| 62 | $fields[$fieldName] = new FieldDefinition($formType, [], $formOptions); |
||
| 63 | } |
||
| 64 | |||
| 65 | return $fields; |
||
| 66 | } |
||
| 103 |