Completed
Push — feature/wrong-form-translatabl... ( 311579...42bacf )
by Narcotic
08:56
created

DocumentFormFieldsCompilerPass   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 155
Duplicated Lines 9.03 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 17
c 1
b 0
f 1
lcom 1
cbo 7
dl 14
loc 155
rs 10
ccs 81
cts 81
cp 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A process() 8 8 2
C getFormFields() 6 70 7
C resolveFieldParams() 0 24 7

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
 * compiler pass for building a listing of fields for compiler
4
 */
5
6
namespace Graviton\DocumentBundle\DependencyInjection\Compiler;
7
8
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\Document;
9
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\DocumentMap;
10
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\ArrayField;
11
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\EmbedMany;
12
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\EmbedOne;
13
use Graviton\DocumentBundle\DependencyInjection\Compiler\Utils\Field;
14
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
17
/**
18
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
19
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
20
 * @link     http://swisscom.ch
21
 */
22
class DocumentFormFieldsCompilerPass implements CompilerPassInterface
23
{
24
    /**
25
     * @var DocumentMap
26
     */
27
    private $documentMap;
28
    /**
29
     * @var array
30
     */
31
    private $typeMap = [
32
        'string'  => 'text',
33
        'extref'  => 'extref',
34
        'int'     => 'integer',
35
        'float'   => 'number',
36
        'boolean' => 'strictboolean',
37
        'date'    => 'datetime',
38
    ];
39
40
    /**
41
     * Constructor
42
     *
43
     * @param DocumentMap $documentMap Document map
44
     */
45 2
    public function __construct(DocumentMap $documentMap)
46
    {
47 2
        $this->documentMap = $documentMap;
48 2
    }
49
50
51
    /**
52
     * load services
53
     *
54
     * @param ContainerBuilder $container container builder
55
     *
56
     * @return void
57
     */
58 2 View Code Duplication
    public function process(ContainerBuilder $container)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
59
    {
60 2
        $map = ['stdclass' => []];
61 2
        foreach ($this->documentMap->getDocuments() as $document) {
62 2
            $map[$document->getClass()] = $this->getFormFields($document);
63 2
        }
64 2
        $container->setParameter('graviton.document.form.type.document.field_map', $map);
65 2
    }
66
67
    /**
68
     * Get document fields
69
     *
70
     * @param Document $document Document
71
     * @return array
72
     */
73 2
    private function getFormFields(Document $document)
74
    {
75 2
        $reflection = new \ReflectionClass($document->getClass());
76 2 View Code Duplication
        if ($reflection->implementsInterface('Graviton\I18nBundle\Document\TranslatableDocumentInterface')) {
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...
77 2
            $instance = $reflection->newInstanceWithoutConstructor();
78 2
            $translatableFields = $instance->getTranslatableFields();
79 2
        } else {
80 1
            $translatableFields = [];
81
        }
82
83 2
        $result = [];
84 2
        foreach ($document->getFields() as $field) {
85 2
            if ($field instanceof Field) {
86 2
                list($type, $options) = $this->resolveFieldParams(
87 2
                    $translatableFields,
88 2
                    $field->getFieldName(),
89 2
                    $field->getType()
90 2
                );
91
92 2
                $result[] = [
93 2
                    $field->getFormName(),
94 2
                    $type,
95 2
                    array_replace(
96
                        [
97 2
                            'property_path' => $field->getFieldName(),
98 1
                            'required' => $field->isRequired()
99 1
                        ],
100 1
                        $options
101 1
                    ),
102 1
                ];
103
            } elseif ($field instanceof ArrayField) {
104 1
                list($type, $options) = $this->resolveFieldParams(
105 1
                    $translatableFields,
106 1
                    $field->getFieldName(),
107
                    $field->getItemType()
108 1
                );
109 1
110 1
                $result[] = [
111 1
                    $field->getFormName(),
112
                    'collection',
113 2
                    [
114 2
                        'property_path' => $field->getFieldName(),
115 2
                        'type' => $type,
116 2
                        'options' => $options,
117
                    ],
118 2
                ];
119 2
            } elseif ($field instanceof EmbedOne) {
120 2
                $result[] = [
121 2
                    $field->getFormName(),
122
                    'form',
123 2
                    [
124 2
                        'property_path' => $field->getFieldName(),
125 2
                        'data_class' => $field->getDocument()->getClass(),
126 2
                        'required' => $field->isRequired(),
127
                    ],
128 2
                ];
129 2
            } elseif ($field instanceof EmbedMany) {
130 2
                $result[] = [
131 2
                    $field->getFormName(),
132
                    'collection',
133 2
                    [
134 2
                        'property_path' => $field->getFieldName(),
135 2
                        'type' => 'form',
136
                        'options' => ['data_class' => $field->getDocument()->getClass()],
137
                    ],
138
                ];
139
            }
140
        }
141
        return $result;
142
    }
143
144
    /**
145
     * Resolve simple field type
146 2
     *
147
     * @param array  $translatable Translatable fields
148 2
     * @param string $fieldName    Field name
149 2
     * @param string $fieldType    Field type
150 2
     * @return array Form type and options
151 2
     */
152 2
    private function resolveFieldParams(array $translatable, $fieldName, $fieldType)
153 2
    {
154 2
        if (in_array($fieldName, $translatable, true) || in_array($fieldName.'[]', $translatable, true)) {
155 1
            $type = 'translatable';
156 1
            $options = [];
157 2
        } elseif ($fieldType === 'hash') {
158 1
            $type = 'freeform';
159 1
            $options = [];
160 2
        } elseif ($fieldType === 'hasharray') {
161 2
            $type = 'collection';
162 2
            $options = ['type' => 'freeform'];
163 2
        } elseif ($fieldType === 'datearray') {
164 1
            $type = 'datearray';
165 1
            $options = [];
166
        } elseif (isset($this->typeMap[$fieldType])) {
167
            $type = $this->typeMap[$fieldType];
168 2
            $options = [];
169
        } else {
170
            $type = 'text';
171
            $options = [];
172
        }
173
174
        return [$type, $options];
175
    }
176
}
177