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