Completed
Push — feature/EVO-6307-record-origin... ( 2c1fcb...797eb1 )
by Narcotic
97:17 queued 32:45
created

DocumentMap::loadSchemaClassMap()   D

Complexity

Conditions 10
Paths 34

Size

Total Lines 32
Code Lines 17

Duplication

Lines 9
Ratio 28.13 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 9
loc 32
rs 4.8196
cc 10
eloc 17
nc 34
nop 1

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
 * 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
    public function __construct(
37
        Finder $doctrineFinder,
38
        Finder $serializerFinder,
39
        Finder $validationFinder,
40
        Finder $schemaFinder
41
    ) {
42
        $doctrineMap = $this->loadDoctrineClassMap($doctrineFinder);
43
        $serializerMap = $this->loadSerializerClassMap($serializerFinder);
44
        $validationMap = $this->loadValidationClassMap($validationFinder);
45
        $schemaMap = $this->loadSchemaClassMap($schemaFinder);
46
47
        foreach ($doctrineMap as $className => $doctrineMapping) {
48
            $this->mappings[$className] = [
49
                'doctrine'   => $doctrineMap[$className],
50
                'serializer' => isset($serializerMap[$className]) ? $serializerMap[$className] : null,
51
                'validation' => isset($validationMap[$className]) ? $validationMap[$className] : null,
52
                'schema' => isset($schemaMap[$className]) ? $schemaMap[$className] : null,
53
            ];
54
        }
55
    }
56
57
    /**
58
     * Get document
59
     *
60
     * @param string $className Document class
61
     * @return Document
62
     */
63
    public function getDocument($className)
64
    {
65
        if (isset($this->documents[$className])) {
66
            return $this->documents[$className];
67
        }
68
        if (!isset($this->mappings[$className])) {
69
            throw new \InvalidArgumentException(sprintf('No XML mapping found for document "%s"', $className));
70
        }
71
72
        return $this->documents[$className] = $this->processDocument(
73
            $className,
74
            $this->mappings[$className]['doctrine'],
75
            $this->mappings[$className]['serializer'],
76
            $this->mappings[$className]['validation'],
77
            $this->mappings[$className]['schema']
78
        );
79
    }
80
81
    /**
82
     * Get all documents
83
     *
84
     * @return Document[]
85
     */
86
    public function getDocuments()
87
    {
88
        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
    private function processDocument(
103
        $className,
104
        \DOMElement $doctrineMapping,
105
        \DOMElement $serializerMapping = null,
106
        \DOMElement $validationMapping = null,
107
        array $schemaMapping = null
108
    ) {
109
        if ($serializerMapping === null) {
110
            $serializerFields = [];
111
        } else {
112
            $serializerFields = array_reduce(
113
                $this->getSerializerFields($serializerMapping),
114
                function (array $fields, array $field) {
115
                    $fields[$field['fieldName']] = $field;
116
                    return $fields;
117
                },
118
                []
119
            );
120
        }
121
122
        if ($validationMapping === null) {
123
            $validationFields = [];
124
        } else {
125
            $validationFields = array_reduce(
126
                $this->getValidationFields($validationMapping),
127
                function (array $fields, array $field) {
128
                    $fields[$field['fieldName']] = $field;
129
                    return $fields;
130
                },
131
                []
132
            );
133
        }
134
135
        if ($schemaMapping === null) {
136
            $schemaFields = [];
137
        } else {
138
            $schemaFields = $schemaMapping;
139
        }
140
141
        $fields = [];
142
        foreach ($this->getDoctrineFields($doctrineMapping) as $doctrineField) {
143
            $serializerField = isset($serializerFields[$doctrineField['name']]) ?
144
                $serializerFields[$doctrineField['name']] :
145
                null;
146
            $validationField = isset($validationFields[$doctrineField['name']]) ?
147
                $validationFields[$doctrineField['name']] :
148
                null;
149
            $schemaField = isset($schemaFields[$doctrineField['name']]) ?
150
                $schemaFields[$doctrineField['name']] :
151
                null;
152
153
            if ($doctrineField['type'] === 'collection') {
154
                $fields[] = new ArrayField(
155
                    $serializerField === null ? 'array<string>' : $serializerField['fieldType'],
156
                    $doctrineField['name'],
157
                    $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'],
158
                    !isset($schemaField['readOnly']) ? false : $schemaField['readOnly'],
159
                    $validationField === null ? false : $validationField['required'],
160
                    $serializerField === null ? false : $serializerField['searchable'],
161
                    !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
                );
163
            } else {
164
                $fields[] = new Field(
165
                    $doctrineField['type'],
166
                    $doctrineField['name'],
167
                    $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'],
168
                    !isset($schemaField['readOnly']) ? false : $schemaField['readOnly'],
169
                    $validationField === null ? false : $validationField['required'],
170
                    $serializerField === null ? false : $serializerField['searchable'],
171
                    !isset($schemaField['recordOriginException']) ? false : $schemaField['recordOriginException']
172
                );
173
            }
174
        }
175
        foreach ($this->getDoctrineEmbedOneFields($doctrineMapping) as $doctrineField) {
176
            $serializerField = isset($serializerFields[$doctrineField['name']]) ?
177
                $serializerFields[$doctrineField['name']] :
178
                null;
179
            $validationField = isset($validationFields[$doctrineField['name']]) ?
180
                $validationFields[$doctrineField['name']] :
181
                null;
182
            $schemaField = isset($schemaFields[$doctrineField['name']]) ?
183
                $schemaFields[$doctrineField['name']] :
184
                null;
185
186
            $fields[] = new EmbedOne(
187
                $this->getDocument($doctrineField['type']),
188
                $doctrineField['name'],
189
                $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'],
190
                !isset($schemaField['readOnly']) ? false : $schemaField['readOnly'],
191
                $validationField === null ? false : $validationField['required'],
192
                $serializerField === null ? false : $serializerField['searchable'],
193
                !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
            );
195
        }
196
        foreach ($this->getDoctrineEmbedManyFields($doctrineMapping) as $doctrineField) {
197
            $serializerField = isset($serializerFields[$doctrineField['name']]) ?
198
                $serializerFields[$doctrineField['name']] :
199
                null;
200
            $validationField = isset($validationFields[$doctrineField['name']]) ?
201
                $validationFields[$doctrineField['name']] :
202
                null;
203
204
            $fields[] = new EmbedMany(
205
                $this->getDocument($doctrineField['type']),
206
                $doctrineField['name'],
207
                $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'],
208
                !isset($schemaField['readOnly']) ? false : $schemaField['readOnly'],
209
                $validationField === null ? false : $validationField['required'],
210
                !isset($schemaField['recordOriginException']) ? false : $schemaField['recordOriginException']
211
            );
212
        }
213
214
        return new Document($className, $fields);
215
    }
216
217
    /**
218
     * Load doctrine class map
219
     *
220
     * @param Finder $finder Mapping finder
221
     * @return array
222
     */
223 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
    {
225
        $classMap = [];
226
        foreach ($finder as $file) {
227
            $document = new \DOMDocument();
228
            $document->load($file);
229
230
            $xpath = new \DOMXPath($document);
231
            $xpath->registerNamespace('doctrine', 'http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping');
232
233
            $classMap = array_reduce(
234
                iterator_to_array($xpath->query('//*[self::doctrine:document or self::doctrine:embedded-document]')),
235
                function (array $classMap, \DOMElement $element) {
236
                    $classMap[$element->getAttribute('name')] = $element;
237
                    return $classMap;
238
                },
239
                $classMap
240
            );
241
        }
242
243
        return $classMap;
244
    }
245
246
    /**
247
     * Load serializer class map
248
     *
249
     * @param Finder $finder Mapping finder
250
     * @return array
251
     */
252 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
    {
254
        $classMap = [];
255
        foreach ($finder as $file) {
256
            $document = new \DOMDocument();
257
            $document->load($file);
258
259
            $xpath = new \DOMXPath($document);
260
261
            $classMap = array_reduce(
262
                iterator_to_array($xpath->query('//class')),
263
                function (array $classMap, \DOMElement $element) {
264
                    $classMap[$element->getAttribute('name')] = $element;
265
                    return $classMap;
266
                },
267
                $classMap
268
            );
269
        }
270
271
        return $classMap;
272
    }
273
274
    /**
275
     * Load schema class map
276
     *
277
     * @param Finder $finder Mapping finder
278
     * @return array
279
     */
280
    private function loadSchemaClassMap(Finder $finder)
281
    {
282
        $classMap = [];
283
        foreach ($finder as $file) {
284
            $schema = json_decode(file_get_contents($file), true);
285
286
            if (!isset($schema['x-documentClass'])) {
287
                continue;
288
            }
289
290 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
                $classMap[$schema['x-documentClass']][$field]['required'] = true;
292
            }
293 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
                $classMap[$schema['x-documentClass']][$field]['searchable'] = 1;
295
            }
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
                $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
                }
307
            }
308
        }
309
310
        return $classMap;
311
    }
312
313
    /**
314
     * Load validation class map
315
     *
316
     * @param Finder $finder Mapping finder
317
     * @return array
318
     */
319 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
    {
321
        $classMap = [];
322
        foreach ($finder as $file) {
323
            $document = new \DOMDocument();
324
            $document->load($file);
325
326
            $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
                $classMap
336
            );
337
        }
338
339
        return $classMap;
340
    }
341
342
    /**
343
     * Get serializer fields
344
     *
345
     * @param \DOMElement $mapping Serializer XML mapping
346
     * @return array
347
     */
348
    private function getSerializerFields(\DOMElement $mapping)
349
    {
350
        $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
                    'searchable'  => (int) $element->getAttribute('searchable')
360
                ];
361
            },
362
            iterator_to_array($xpath->query('property', $mapping))
363
        );
364
    }
365
366
    /**
367
     * Get serializer field type
368
     *
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
378
        $xpath = new \DOMXPath($field->ownerDocument);
379
380
        $type = $xpath->query('type', $field)->item(0);
381
        return $type === null ? null : $type->nodeValue;
382
    }
383
384
    /**
385
     * Get validation fields
386
     *
387
     * @param \DOMElement $mapping Validation XML mapping
388
     * @return array
389
     */
390
    private function getValidationFields(\DOMElement $mapping)
391
    {
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
                    'required'  => $constraints->length > 0,
401
                ];
402
            },
403
            iterator_to_array($xpath->query('constraint:property', $mapping))
404
        );
405
    }
406
407
    /**
408
     * Get doctrine document fields
409
     *
410
     * @param \DOMElement $mapping Doctrine XML mapping
411
     * @return array
412
     */
413 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
                    'type' => $element->getAttribute('type'),
423
                ];
424
            },
425
            iterator_to_array($xpath->query('doctrine:field', $mapping))
426
        );
427
    }
428
429
    /**
430
     * Get doctrine document embed-one fields
431
     *
432
     * @param \DOMElement $mapping Doctrine XML mapping
433
     * @return array
434
     */
435 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
                    'type' => $element->getAttribute('target-document'),
445
                ];
446
            },
447
            iterator_to_array($xpath->query('*[self::doctrine:embed-one or self::doctrine:reference-one]', $mapping))
448
        );
449
    }
450
451
    /**
452
     * Get doctrine document embed-many fields
453
     *
454
     * @param \DOMElement $mapping Doctrine XML mapping
455
     * @return array
456
     */
457 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
    /**
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
     * @param Document $document       The document
480
     * @param string   $documentPrefix Document field prefix
481
     * @param string   $exposedPrefix  Exposed field prefix
482
     * @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
     *
484
     * @return array
485
     */
486
    public function getFieldNamesFlat(
487
        Document $document,
488
        $documentPrefix = '',
489
        $exposedPrefix = '',
490
        callable $callback = null
491
    ) {
492
        $result = [];
493
        foreach ($document->getFields() as $field) {
494 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
                $result[$documentPrefix . $field->getFieldName()] = $exposedPrefix . $field->getExposedName();
496
            }
497
498
            if ($field instanceof ArrayField) {
499 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
                    $result[$documentPrefix . $field->getFieldName() . '.0'] =
501
                        $exposedPrefix . $field->getExposedName() . '.0';
502
                }
503
            } elseif ($field instanceof EmbedOne) {
504
                $result = array_merge(
505
                    $result,
506
                    $this->getFieldNamesFlat(
507
                        $field->getDocument(),
508
                        $documentPrefix.$field->getFieldName().'.',
509
                        $exposedPrefix.$field->getExposedName().'.',
510
                        $callback
511
                    )
512
                );
513
            } elseif ($field instanceof EmbedMany) {
514 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
                    $result[$documentPrefix . $field->getFieldName() . '.0'] =
516
                        $exposedPrefix . $field->getExposedName() . '.0';
517
                }
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
530
        return $result;
531
    }
532
533
    /**
534
     * Simple function to check whether a given shall be returned in the output of getFieldNamesFlat
535
     * 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