FieldMapper   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 162
Duplicated Lines 43.83 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 5
dl 71
loc 162
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
D __invoke() 60 96 9
B mapCollectionType() 11 37 3
A unrestrictChildClass() 0 13 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Psi\Bridge\ContentType\Doctrine\PhpcrOdm;
6
7
use Doctrine\ODM\PHPCR\Mapping\ClassMetadata;
8
use Psi\Component\ContentType\Field;
9
use Psi\Component\ContentType\FieldLoader;
10
use Psi\Component\ContentType\FieldOptions;
11
use Psi\Component\ContentType\Standard\Storage as Type;
12
13
/**
14
 * The FieldMapper maps the correct PHPCR-ODM field for the given content-type
15
 * field.
16
 */
17
class FieldMapper
18
{
19
    private $encoder;
20
    private $fieldLoader;
21
22
    public function __construct(
23
        PropertyEncoder $encoder,
24
        FieldLoader $fieldLoader
25
    ) {
26
        $this->encoder = $encoder;
27
        $this->fieldLoader = $fieldLoader;
28
    }
29
30
    public function __invoke($fieldName, Field $field, ClassMetadata $metadata, array $extraOptions = [])
31
    {
32
        $type = $field->getStorageType();
33
        $options = array_merge([
34
            'multivalue' => false,
35
        ], $extraOptions);
36
37
        if ($type === Type\ObjectType::class) {
38
            $options = $field->getStorageOptions();
39
            $this->unrestrictChildClass($options['class'], $metadata);
40
            $metadata->mapChild([
41
                'fieldName' => $fieldName,
42
                'nodeName' => $this->encoder->encode($fieldName),
43
                'nullable' => true,
44
            ]);
45
46
            return;
47
        }
48
49
        if ($type === Type\CollectionType::class) {
50
            $this->mapCollectionType($fieldName, $field, $metadata);
51
52
            return;
53
        }
54
55 View Code Duplication
        if ($type === Type\StringType::class) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
            $metadata->mapField([
57
                'fieldName' => $fieldName,
58
                'type' => 'string',
59
                'nullable' => true,
60
                'multivalue' => $options['multivalue'],
61
            ]);
62
63
            return;
64
        }
65
66 View Code Duplication
        if ($type === Type\IntegerType::class) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
            $metadata->mapField([
68
                'fieldName' => $fieldName,
69
                'type' => 'long',
70
                'nullable' => true,
71
                'multivalue' => $options['multivalue'],
72
            ]);
73
74
            return;
75
        }
76
77 View Code Duplication
        if ($type === Type\BooleanType::class) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
            $metadata->mapField([
79
                'fieldName' => $fieldName,
80
                'type' => 'boolean',
81
                'nullable' => true,
82
            ]);
83
84
            return;
85
        }
86
87 View Code Duplication
        if ($type === Type\DoubleType::class) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
88
            $metadata->mapField([
89
                'fieldName' => $fieldName,
90
                'type' => 'double',
91
                'nullable' => true,
92
            ]);
93
94
            return;
95
        }
96
97 View Code Duplication
        if ($type === Type\DateTimeType::class) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
            $metadata->mapField([
99
                'fieldName' => $fieldName,
100
                'type' => 'date',
101
                'nullable' => true,
102
                'multivalue' => $options['multivalue'],
103
            ]);
104
105
            return;
106
        }
107
108 View Code Duplication
        if ($type === Type\ReferenceType::class) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
109
            $metadata->mapManyToOne([
110
                'fieldName' => $fieldName,
111
                'strategy' => 'hard',
112
113
                'nullable' => true,
114
                'cascade' => ClassMetadata::CASCADE_ALL,
115
                'multivalue' => $options['multivalue'],
116
            ]);
117
118
            return;
119
        }
120
121
        throw new \RuntimeException(sprintf(
122
            'Do not know how to map field of type "%s"',
123
            $type
124
        ));
125
    }
126
127
    private function mapCollectionType($fieldName, Field $field, ClassMetadata $metadata)
128
    {
129
        $options = $field->getOptions();
130
        $collectionField = $this->fieldLoader->load($options['field_type'], FieldOptions::create($options['field_options']));
131
132
        if ($collectionField->getStorageType() === Type\ObjectType::class) {
133
            $options = $collectionField->getStorageOptions();
134
            $this->unrestrictChildClass($options['class'], $metadata);
135
136
            $metadata->mapChildren([
137
                'fieldName' => $fieldName,
138
                'fetchDepth' => 1,
139
                'filter' => $this->encoder->encode($fieldName) . '-*',
140
                'cascade' => ClassMetadata::CASCADE_ALL,
141
                'nullable' => true,
142
            ]);
143
144
            return;
145
        }
146
147 View Code Duplication
        if ($collectionField->getStorageType() === Type\ReferenceType::class) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
148
            $metadata->mapManyToMany([
149
                'fieldName' => $fieldName,
150
                'strategy' => 'hard',
151
152
                'nullable' => true,
153
                'cascade' => ClassMetadata::CASCADE_ALL,
154
            ]);
155
156
            return;
157
        }
158
159
        // assume that other types are scalars...
160
        $this->__invoke($fieldName, $collectionField, $metadata, [
161
            'multivalue' => true,
162
        ]);
163
    }
164
165
    private function unrestrictChildClass(string $class, ClassMetadata $metadata)
166
    {
167
        if (!$class) {
168
            return;
169
        }
170
171
        if (!$childClasses = $metadata->getChildClasses()) {
172
            return;
173
        }
174
175
        $childClasses[] = $class;
176
        $metadata->setChildClasses($childClasses);
177
    }
178
}
179