Passed
Pull Request — master (#116)
by Arnaud
05:44 queued 02:35
created

MetadataHelper   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 29
dl 0
loc 62
rs 10
c 0
b 0
f 0
wmc 13

3 Methods

Rating   Name   Duplication   Size   Complexity  
B getFields() 0 37 9
A isJoinColumnNullable() 0 11 3
A __construct() 0 3 1
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
        $fieldNames = (array) $metadata->fieldNames;
0 ignored issues
show
Bug introduced by
Accessing fieldNames on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
25
        $fields = [];
26
27
        foreach ($fieldNames as $fieldName) {
28
            // Remove the primary key field if it's not managed manually
29
            if (!$metadata->isIdentifierNatural() && in_array($fieldName, $metadata->identifier)) {
0 ignored issues
show
Bug introduced by
The method isIdentifierNatural() does not exist on Doctrine\Common\Persistence\Mapping\ClassMetadata. Did you maybe mean isIdentifier()? ( Ignorable by Annotation )

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

29
            if (!$metadata->/** @scrutinizer ignore-call */ isIdentifierNatural() && in_array($fieldName, $metadata->identifier)) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
Accessing identifier on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
30
                continue;
31
            }
32
            $mapping = $metadata->getFieldMapping($fieldName);
33
            $formOptions = [];
34
35
            // When a field is defined as nullable in the Doctrine entity configuration, the associated form field
36
            // should not be required neither
37
            if (key_exists('nullable', $mapping) && true === $mapping['nullable']) {
38
                $formOptions['required'] = false;
39
            }
40
            $fields[$fieldName] = new FieldDefinition($metadata->getTypeOfField($fieldName), [], $formOptions);
41
        }
42
43
        foreach ($metadata->associationMappings as $fieldName => $relation) {
0 ignored issues
show
Bug introduced by
Accessing associationMappings on the interface Doctrine\Common\Persistence\Mapping\ClassMetadata suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
44
            $formOptions = [];
45
            $formType = 'choice';
46
47
            if (ClassMetadataInfo::MANY_TO_MANY === $relation['type']) {
48
                $formOptions['expanded'] = true;
49
                $formOptions['multiple'] = true;
50
            }
51
            if ($this->isJoinColumnNullable($relation)) {
52
                $formOptions['required'] = false;
53
            }
54
            $fields[$fieldName] = new FieldDefinition($formType, [], $formOptions);
55
        }
56
57
        return $fields;
58
    }
59
60
    private function isJoinColumnNullable(array $relation)
61
    {
62
        if (!key_exists('joinColumns', $relation)) {
63
            return false;
64
        }
65
66
        if (!key_exists('nullable', $relation['joinColumns'])) {
67
            return false;
68
        }
69
70
        return false === $relation['joinColumns']['nullable'];
71
    }
72
}
73