Completed
Push — feature/EVO-5751-mongodb-3.x ( e24a58...f7410f )
by Lucas
65:41
created

DocumentMap::__construct()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 20
ccs 13
cts 13
cp 1
rs 8.8571
c 1
b 0
f 0
cc 5
eloc 15
nc 9
nop 4
crap 5
1
<?php
2
/**
3
 * DocumentMap class file
4
 */
5
6
namespace Graviton\DocumentBundle\DependencyInjection\Compiler\Utils;
7
8
use Symfony\Component\Finder\Finder;
9
10
/**
11
 * Document map
12
 *
13
 * @author   List of contributors <https://github.com/libgraviton/graviton/graphs/contributors>
14
 * @license  http://opensource.org/licenses/gpl-license.php GNU Public License
15
 * @link     http://swisscom.ch
16
 */
17
class DocumentMap
18
{
19
    /**
20
     * @var array
21
     */
22
    private $mappings = [];
23
    /**
24
     * @var Document[]
25
     */
26
    private $documents = [];
27
28
    /**
29
     * Constructor
30
     *
31
     * @param Finder $doctrineFinder   Doctrine mapping finder
32
     * @param Finder $serializerFinder Serializer mapping finder
33
     * @param Finder $validationFinder Validation mapping finder
34
     * @param Finder $schemaFinder     Schema finder
35
     */
36 10
    public function __construct(
37
        Finder $doctrineFinder,
38
        Finder $serializerFinder,
39
        Finder $validationFinder,
40
        Finder $schemaFinder
41
    ) {
42 10
        $doctrineMap = $this->loadDoctrineClassMap($doctrineFinder);
43 10
        $serializerMap = $this->loadSerializerClassMap($serializerFinder);
44 10
        $validationMap = $this->loadValidationClassMap($validationFinder);
45 10
        $schemaMap = $this->loadSchemaClassMap($schemaFinder);
46
47 10
        foreach ($doctrineMap as $className => $doctrineMapping) {
48 10
            $this->mappings[$className] = [
49 10
                'doctrine'   => $doctrineMap[$className],
50 10
                'serializer' => isset($serializerMap[$className]) ? $serializerMap[$className] : null,
51 10
                'validation' => isset($validationMap[$className]) ? $validationMap[$className] : null,
52 10
                'schema' => isset($schemaMap[$className]) ? $schemaMap[$className] : null,
53
            ];
54 5
        }
55 10
    }
56
57
    /**
58
     * Get document
59
     *
60
     * @param string $className Document class
61
     * @return Document
62
     */
63 10
    public function getDocument($className)
64
    {
65 10
        if (isset($this->documents[$className])) {
66 10
            return $this->documents[$className];
67
        }
68 10
        if (!isset($this->mappings[$className])) {
69
            throw new \InvalidArgumentException(sprintf('No XML mapping found for document "%s"', $className));
70
        }
71
72 10
        return $this->documents[$className] = $this->processDocument(
73 5
            $className,
74 10
            $this->mappings[$className]['doctrine'],
75 10
            $this->mappings[$className]['serializer'],
76 10
            $this->mappings[$className]['validation'],
77 10
            $this->mappings[$className]['schema']
78 5
        );
79
    }
80
81
    /**
82
     * Get all documents
83
     *
84
     * @return Document[]
85
     */
86 6
    public function getDocuments()
87
    {
88 6
        return array_map([$this, 'getDocument'], array_keys($this->mappings));
89
    }
90
91
    /**
92
     * Process document
93
     *
94
     * @param string      $className         Class name
95
     * @param \DOMElement $doctrineMapping   Doctrine XML mapping
96
     * @param \DOMElement $serializerMapping Serializer XML mapping
0 ignored issues
show
Documentation introduced by
Should the type for parameter $serializerMapping not be null|\DOMElement?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
97
     * @param \DOMElement $validationMapping Validation XML mapping
0 ignored issues
show
Documentation introduced by
Should the type for parameter $validationMapping not be null|\DOMElement?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
98
     * @param array       $schemaMapping     Schema mapping
0 ignored issues
show
Documentation introduced by
Should the type for parameter $schemaMapping not be null|array? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
99
     *
100
     * @return Document
101
     */
102 10
    private function processDocument(
103
        $className,
104
        \DOMElement $doctrineMapping,
105
        \DOMElement $serializerMapping = null,
106
        \DOMElement $validationMapping = null,
107
        array $schemaMapping = null
108
    ) {
109 10
        if ($serializerMapping === null) {
110 2
            $serializerFields = [];
111 1
        } else {
112 10
            $serializerFields = array_reduce(
113 10
                $this->getSerializerFields($serializerMapping),
114
                function (array $fields, array $field) {
115 10
                    $fields[$field['fieldName']] = $field;
116 10
                    return $fields;
117 10
                },
118 10
                []
119 5
            );
120
        }
121
122 10
        if ($validationMapping === null) {
123 2
            $validationFields = [];
124 1
        } else {
125 10
            $validationFields = array_reduce(
126 10
                $this->getValidationFields($validationMapping),
127
                function (array $fields, array $field) {
128 4
                    $fields[$field['fieldName']] = $field;
129 4
                    return $fields;
130 10
                },
131 10
                []
132 5
            );
133
        }
134
135 10
        if ($schemaMapping === null) {
136 10
            $schemaFields = [];
137 5
        } else {
138 2
            $schemaFields = $schemaMapping;
139
        }
140
141 10
        $fields = [];
142 10
        foreach ($this->getDoctrineFields($doctrineMapping) as $doctrineField) {
143 10
            $serializerField = isset($serializerFields[$doctrineField['name']]) ?
144 10
                $serializerFields[$doctrineField['name']] :
145 10
                null;
146 10
            $validationField = isset($validationFields[$doctrineField['name']]) ?
147 7
                $validationFields[$doctrineField['name']] :
148 10
                null;
149 10
            $schemaField = isset($schemaFields[$doctrineField['name']]) ?
150 6
                $schemaFields[$doctrineField['name']] :
151 10
                null;
152
153 10
            if ($doctrineField['type'] === 'collection') {
154 2
                $fields[] = new ArrayField(
155 2
                    $serializerField === null ? 'array<string>' : $serializerField['fieldType'],
156 2
                    $doctrineField['name'],
157 2
                    $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'],
158 2
                    !isset($schemaField['readOnly']) ? false : $schemaField['readOnly'],
159 2
                    $validationField === null ? false : $validationField['required'],
160 2
                    $serializerField === null ? false : $serializerField['searchable'],
161 1
                    !isset($schemaField['recordOriginException']) ? false : $schemaField['recordOriginException']
0 ignored issues
show
Unused Code introduced by
The call to ArrayField::__construct() has too many arguments starting with !isset($schemaField['rec...recordOriginException'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
162 1
                );
163 10
            } else {
164 10
                $fields[] = new Field(
165 10
                    $doctrineField['type'],
166 10
                    $doctrineField['name'],
167 10
                    $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'],
168 10
                    !isset($schemaField['readOnly']) ? false : $schemaField['readOnly'],
169 10
                    $validationField === null ? false : $validationField['required'],
170 5
                    $serializerField === null ? false : $serializerField['searchable'],
171
                    !isset($schemaField['recordOriginException']) ? false : $schemaField['recordOriginException']
172 5
                );
173 10
            }
174 10
        }
175 10
        foreach ($this->getDoctrineEmbedOneFields($doctrineMapping) as $doctrineField) {
176 10
            $serializerField = isset($serializerFields[$doctrineField['name']]) ?
177 10
                $serializerFields[$doctrineField['name']] :
178 7
                null;
179 10
            $validationField = isset($validationFields[$doctrineField['name']]) ?
180 10
                $validationFields[$doctrineField['name']] :
181 6
                null;
182 10
            $schemaField = isset($schemaFields[$doctrineField['name']]) ?
183
                $schemaFields[$doctrineField['name']] :
184 10
                null;
185 10
186 10
            $fields[] = new EmbedOne(
187 10
                $this->getDocument($doctrineField['type']),
188 10
                $doctrineField['name'],
189 10
                $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'],
190 10
                !isset($schemaField['readOnly']) ? false : $schemaField['readOnly'],
191 5
                $validationField === null ? false : $validationField['required'],
192 5
                $serializerField === null ? false : $serializerField['searchable'],
193 10
                !isset($schemaField['recordOriginException']) ? false : $schemaField['recordOriginException']
0 ignored issues
show
Unused Code introduced by
The call to EmbedOne::__construct() has too many arguments starting with !isset($schemaField['rec...recordOriginException'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
194 10
            );
195 10
        }
196 10
        foreach ($this->getDoctrineEmbedManyFields($doctrineMapping) as $doctrineField) {
197 10
            $serializerField = isset($serializerFields[$doctrineField['name']]) ?
198 6
                $serializerFields[$doctrineField['name']] :
199 10
                null;
200
            $validationField = isset($validationFields[$doctrineField['name']]) ?
201 10
                $validationFields[$doctrineField['name']] :
202 10
                null;
203 10
204 10
            $fields[] = new EmbedMany(
205 10
                $this->getDocument($doctrineField['type']),
206 10
                $doctrineField['name'],
207 5
                $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'],
208 5
                !isset($schemaField['readOnly']) ? false : $schemaField['readOnly'],
209
                $validationField === null ? false : $validationField['required'],
210 10
                !isset($schemaField['recordOriginException']) ? false : $schemaField['recordOriginException']
211
            );
212
        }
213
214
        return new Document($className, $fields);
215
    }
216
217
    /**
218
     * Load doctrine class map
219 10
     *
220
     * @param Finder $finder Mapping finder
221 10
     * @return array
222 10
     */
223 10 View Code Duplication
    private function loadDoctrineClassMap(Finder $finder)
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...
224 10
    {
225
        $classMap = [];
226 10
        foreach ($finder as $file) {
227 10
            $document = new \DOMDocument();
228
            $document->load($file);
229 10
230 10
            $xpath = new \DOMXPath($document);
231
            $xpath->registerNamespace('doctrine', 'http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping');
232 10
233 10
            $classMap = array_reduce(
234 10
                iterator_to_array($xpath->query('//*[self::doctrine:document or self::doctrine:embedded-document]')),
235
                function (array $classMap, \DOMElement $element) {
236 5
                    $classMap[$element->getAttribute('name')] = $element;
237 5
                    return $classMap;
238
                },
239 10
                $classMap
240
            );
241
        }
242
243
        return $classMap;
244
    }
245
246
    /**
247
     * Load serializer class map
248 10
     *
249
     * @param Finder $finder Mapping finder
250 10
     * @return array
251 10
     */
252 10 View Code Duplication
    private function loadSerializerClassMap(Finder $finder)
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...
253 10
    {
254
        $classMap = [];
255 10
        foreach ($finder as $file) {
256
            $document = new \DOMDocument();
257 10
            $document->load($file);
258 10
259
            $xpath = new \DOMXPath($document);
260 10
261 10
            $classMap = array_reduce(
262 10
                iterator_to_array($xpath->query('//class')),
263
                function (array $classMap, \DOMElement $element) {
264 5
                    $classMap[$element->getAttribute('name')] = $element;
265 5
                    return $classMap;
266
                },
267 10
                $classMap
268
            );
269
        }
270
271
        return $classMap;
272
    }
273
274
    /**
275
     * Load schema class map
276 10
     *
277
     * @param Finder $finder Mapping finder
278 10
     * @return array
279 10
     */
280 10
    private function loadSchemaClassMap(Finder $finder)
281
    {
282 10
        $classMap = [];
283 10
        foreach ($finder as $file) {
284
            $schema = json_decode(file_get_contents($file), true);
285
286 2
            if (!isset($schema['x-documentClass'])) {
287 2
                continue;
288 1
            }
289 2
290 2 View Code Duplication
            foreach ($schema['required'] as $field) {
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...
291 1
                $classMap[$schema['x-documentClass']][$field]['required'] = true;
292 2
            }
293 2 View Code Duplication
            foreach ($schema['searchable'] as $field) {
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...
294 1
                $classMap[$schema['x-documentClass']][$field]['searchable'] = 1;
295 5
            }
296 View Code Duplication
            foreach ($schema['readOnlyFields'] as $field) {
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...
297 10
                $classMap[$schema['x-documentClass']][$field]['readOnly'] = true;
298
            }
299
300
            // flags from fields
301
            if (is_array($schema['properties'])) {
302
                foreach ($schema['properties'] as $fieldName => $field) {
303
                    if (isset($field['recordOriginException']) && $field['recordOriginException'] == true) {
304
                        $classMap[$schema['x-documentClass']][$fieldName]['recordOriginException'] = true;
305
                    }
306 10
                }
307
            }
308 10
        }
309 10
310 10
        return $classMap;
311 10
    }
312
313 10
    /**
314 10
     * Load validation class map
315
     *
316 10
     * @param Finder $finder Mapping finder
317 10
     * @return array
318
     */
319 10 View Code Duplication
    private function loadValidationClassMap(Finder $finder)
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...
320 10
    {
321 10
        $classMap = [];
322
        foreach ($finder as $file) {
323 5
            $document = new \DOMDocument();
324 5
            $document->load($file);
325
326 10
            $xpath = new \DOMXPath($document);
327
            $xpath->registerNamespace('constraint', 'http://symfony.com/schema/dic/constraint-mapping');
328
329
            $classMap = array_reduce(
330
                iterator_to_array($xpath->query('//constraint:class')),
331
                function (array $classMap, \DOMElement $element) {
332
                    $classMap[$element->getAttribute('name')] = $element;
333
                    return $classMap;
334
                },
335 10
                $classMap
336
            );
337 10
        }
338
339 10
        return $classMap;
340
    }
341
342 10
    /**
343 10
     * Get serializer fields
344 10
     *
345 10
     * @param \DOMElement $mapping Serializer XML mapping
346 10
     * @return array
347 5
     */
348 10
    private function getSerializerFields(\DOMElement $mapping)
349 10
    {
350 5
        $xpath = new \DOMXPath($mapping->ownerDocument);
351
352
        return array_map(
353
            function (\DOMElement $element) {
354
                return [
355
                    'fieldName'   => $element->getAttribute('name'),
356
                    'fieldType'   => $this->getSerializerFieldType($element),
357
                    'exposedName' => $element->getAttribute('serialized-name') ?: $element->getAttribute('name'),
358
                    'readOnly'    => $element->getAttribute('read-only') === 'true',
359 10
                    'searchable'  => (int) $element->getAttribute('searchable')
360
                ];
361 10
            },
362 10
            iterator_to_array($xpath->query('property', $mapping))
363
        );
364
    }
365 2
366
    /**
367 2
     * Get serializer field type
368 2
     *
369
     * @param \DOMElement $field Field node
370
     * @return string|null
371
     */
372
    private function getSerializerFieldType(\DOMElement $field)
373
    {
374
        if ($field->getAttribute('type')) {
375
            return $field->getAttribute('type');
376
        }
377 10
378
        $xpath = new \DOMXPath($field->ownerDocument);
379 10
380 10
        $type = $xpath->query('type', $field)->item(0);
381
        return $type === null ? null : $type->nodeValue;
382 10
    }
383
384 4
    /**
385
     * Get validation fields
386 4
     *
387 4
     * @param \DOMElement $mapping Validation XML mapping
388 2
     * @return array
389 10
     */
390 10
    private function getValidationFields(\DOMElement $mapping)
391 5
    {
392
        $xpath = new \DOMXPath($mapping->ownerDocument);
393
        $xpath->registerNamespace('constraint', 'http://symfony.com/schema/dic/constraint-mapping');
394
395
        return array_map(
396
            function (\DOMElement $element) use ($xpath) {
397
                $constraints = $xpath->query('constraint:constraint[@name="NotBlank" or @name="NotNull"]', $element);
398
                return [
399
                    'fieldName' => $element->getAttribute('name'),
400 10
                    'required'  => $constraints->length > 0,
401
                ];
402 10
            },
403 10
            iterator_to_array($xpath->query('constraint:property', $mapping))
404
        );
405 10
    }
406
407
    /**
408 10
     * Get doctrine document fields
409 10
     *
410 5
     * @param \DOMElement $mapping Doctrine XML mapping
411 10
     * @return array
412 10
     */
413 5 View Code Duplication
    private function getDoctrineFields(\DOMElement $mapping)
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...
414
    {
415
        $xpath = new \DOMXPath($mapping->ownerDocument);
416
        $xpath->registerNamespace('doctrine', 'http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping');
417
418
        return array_map(
419
            function (\DOMElement $element) {
420
                return [
421
                    'name' => $element->getAttribute('fieldName'),
422 10
                    'type' => $element->getAttribute('type'),
423
                ];
424 10
            },
425 10
            iterator_to_array($xpath->query('doctrine:field', $mapping))
426
        );
427 10
    }
428
429
    /**
430 10
     * Get doctrine document embed-one fields
431 10
     *
432 5
     * @param \DOMElement $mapping Doctrine XML mapping
433 10
     * @return array
434 10
     */
435 5 View Code Duplication
    private function getDoctrineEmbedOneFields(\DOMElement $mapping)
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...
436
    {
437
        $xpath = new \DOMXPath($mapping->ownerDocument);
438
        $xpath->registerNamespace('doctrine', 'http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping');
439
440
        return array_map(
441
            function (\DOMElement $element) {
442
                return [
443
                    'name' => $element->getAttribute('field'),
444 10
                    'type' => $element->getAttribute('target-document'),
445
                ];
446 10
            },
447 10
            iterator_to_array($xpath->query('*[self::doctrine:embed-one or self::doctrine:reference-one]', $mapping))
448
        );
449 10
    }
450 10
451
    /**
452 10
     * Get doctrine document embed-many fields
453 10
     *
454 5
     * @param \DOMElement $mapping Doctrine XML mapping
455 10
     * @return array
456 10
     */
457 5 View Code Duplication
    private function getDoctrineEmbedManyFields(\DOMElement $mapping)
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...
458
    {
459
        $xpath = new \DOMXPath($mapping->ownerDocument);
460
        $xpath->registerNamespace('doctrine', 'http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping');
461
462
        return array_map(
463
            function (\DOMElement $element) {
464
                return [
465
                    'name' => $element->getAttribute('field'),
466
                    'type' => $element->getAttribute('target-document'),
467
                ];
468
            },
469
            iterator_to_array($xpath->query('*[self::doctrine:embed-many or self::doctrine:reference-many]', $mapping))
470
        );
471
    }
472
473 4
    /**
474
     * Gets an array of all fields, flat with full internal name in dot notation as key and
475
     * the exposed field name as value. You can pass a callable to limit the fields return a subset of fields.
476
     * If the callback returns true, the field will be included in the output. You will get the field definition
477
     * passed to your callback.
478
     *
479 4
     * @param Document $document       The document
480 4
     * @param string   $documentPrefix Document field prefix
481 4
     * @param string   $exposedPrefix  Exposed field prefix
482 4
     * @param callable $callback       An optional callback where you can influence the number of fields returned
0 ignored issues
show
Documentation introduced by
Should the type for parameter $callback not be null|callable? Also, consider making the array more specific, something like array<String>, or String[].

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type array and suggests a stricter type like array<String>.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
483 2
     *
484
     * @return array
485 4
     */
486 2
    public function getFieldNamesFlat(
487 2
        Document $document,
488 2
        $documentPrefix = '',
489 1
        $exposedPrefix = '',
490 2
        callable $callback = null
491 4
    ) {
492 2
        $result = [];
493 4
        foreach ($document->getFields() as $field) {
494 4 View Code Duplication
            if ($this->getFlatFieldCheckCallback($field, $callback)) {
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...
495 4
                $result[$documentPrefix . $field->getFieldName()] = $exposedPrefix . $field->getExposedName();
496 4
            }
497
498 2
            if ($field instanceof ArrayField) {
499 2 View Code Duplication
                if ($this->getFlatFieldCheckCallback($field, $callback)) {
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...
500 2
                    $result[$documentPrefix . $field->getFieldName() . '.0'] =
501 4
                        $exposedPrefix . $field->getExposedName() . '.0';
502 4
                }
503 4
            } elseif ($field instanceof EmbedOne) {
504 2
                $result = array_merge(
505 4
                    $result,
506 2
                    $this->getFieldNamesFlat(
507 4
                        $field->getDocument(),
508 4
                        $documentPrefix.$field->getFieldName().'.',
509 4
                        $exposedPrefix.$field->getExposedName().'.',
510 4
                        $callback
511
                    )
512 2
                );
513 2
            } elseif ($field instanceof EmbedMany) {
514 2 View Code Duplication
                if ($this->getFlatFieldCheckCallback($field, $callback)) {
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...
515 2
                    $result[$documentPrefix . $field->getFieldName() . '.0'] =
516
                        $exposedPrefix . $field->getExposedName() . '.0';
517 4
                }
518
                $result = array_merge(
519
                    $result,
520
                    $this->getFieldNamesFlat(
521
                        $field->getDocument(),
522
                        $documentPrefix.$field->getFieldName().'.0.',
523
                        $exposedPrefix.$field->getExposedName().'.0.',
524
                        $callback
525
                    )
526
                );
527
            }
528
        }
529 4
530
        return $result;
531 4
    }
532 4
533
    /**
534
     * Simple function to check whether a given shall be returned in the output of getFieldNamesFlat
535 2
     * and the optional given callback there.
536
     *
537
     * @param AbstractField $field    field
538
     * @param callable|null $callback optional callback
539
     *
540
     * @return bool|mixed true if field should be returned, false otherwise
541
     */
542
    private function getFlatFieldCheckCallback($field, callable $callback = null)
543
    {
544
        if (!is_callable($callback)) {
545
            return true;
546
        }
547
548
        return call_user_func($callback, $field);
549
    }
550
}
551