Completed
Push — master ( 1edd1f...794991 )
by Arnaud
22s queued 10s
created

MetadataHelper::getFields()   B

Complexity

Conditions 10
Paths 21

Size

Total Lines 43
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 23
nc 21
nop 1
dl 0
loc 43
rs 7.6666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

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:

1
<?php
2
3
namespace LAG\AdminBundle\Bridge\Doctrine\ORM\Metadata;
4
5
use Doctrine\ORM\EntityManagerInterface;
6
use Doctrine\ORM\Mapping\ClassMetadataInfo;
7
use LAG\AdminBundle\Field\Definition\FieldDefinition;
8
9
class MetadataHelper implements MetadataHelperInterface
10
{
11
    /**
12
     * @var EntityManagerInterface
13
     */
14
    private $entityManager;
15
16
    public function __construct(EntityManagerInterface $entityManager)
17
    {
18
        $this->entityManager = $entityManager;
19
    }
20
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);
0 ignored issues
show
Bug introduced by
It seems like $metadata->getTypeOfField($fieldName) can also be of type null; however, parameter $type of LAG\AdminBundle\Field\De...finition::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

46
            $fields[$fieldName] = new FieldDefinition(/** @scrutinizer ignore-type */ $metadata->getTypeOfField($fieldName), [], $formOptions);
Loading history...
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
    }
65
66
    private function isJoinColumnNullable(array $relation)
67
    {
68
        if (!key_exists('joinColumns', $relation)) {
69
            return false;
70
        }
71
72
        if (!key_exists('nullable', $relation['joinColumns'])) {
73
            return false;
74
        }
75
76
        return false === $relation['joinColumns']['nullable'];
77
    }
78
}
79