Completed
Push — feature/EVO-5751-text-index-mo... ( 0ab3ac...8fecb1 )
by
unknown
62:16 queued 57:01
created

DocumentMap::getFieldNamesFlat()   C

Complexity

Conditions 8
Paths 13

Size

Total Lines 46
Code Lines 33

Duplication

Lines 11
Ratio 23.91 %

Code Coverage

Tests 36
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 11
loc 46
ccs 36
cts 36
cp 1
rs 5.5555
c 1
b 0
f 0
cc 8
eloc 33
nc 13
nop 4
crap 8
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']
0 ignored issues
show
Unused Code introduced by
The call to ArrayField::__construct() has too many arguments starting with $serializerField === nul...izerField['searchable'].

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

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...
191 5
            );
192 5
        }
193 10
        foreach ($this->getDoctrineEmbedManyFields($doctrineMapping) as $doctrineField) {
194 10
            $serializerField = isset($serializerFields[$doctrineField['name']]) ?
195 10
                $serializerFields[$doctrineField['name']] :
196 10
                null;
197 10
            $validationField = isset($validationFields[$doctrineField['name']]) ?
198 6
                $validationFields[$doctrineField['name']] :
199 10
                null;
200
201 10
            $fields[] = new EmbedMany(
202 10
                $this->getDocument($doctrineField['type']),
203 10
                $doctrineField['name'],
204 10
                $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'],
205 10
                !isset($schemaField['readOnly']) ? false : $schemaField['readOnly'],
206 10
                $validationField === null ? false : $validationField['required']
207 5
            );
208 5
        }
209
210 10
        return new Document($className, $fields);
211
    }
212
213
    /**
214
     * Load doctrine class map
215
     *
216
     * @param Finder $finder Mapping finder
217
     * @return array
218
     */
219 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...
220
    {
221 10
        $classMap = [];
222 10
        foreach ($finder as $file) {
223 10
            $document = new \DOMDocument();
224 10
            $document->load($file);
225
226 10
            $xpath = new \DOMXPath($document);
227 10
            $xpath->registerNamespace('doctrine', 'http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping');
228
229 10
            $classMap = array_reduce(
230 10
                iterator_to_array($xpath->query('//*[self::doctrine:document or self::doctrine:embedded-document]')),
231
                function (array $classMap, \DOMElement $element) {
232 10
                    $classMap[$element->getAttribute('name')] = $element;
233 10
                    return $classMap;
234 10
                },
235
                $classMap
236 5
            );
237 5
        }
238
239 10
        return $classMap;
240
    }
241
242
    /**
243
     * Load serializer class map
244
     *
245
     * @param Finder $finder Mapping finder
246
     * @return array
247
     */
248 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...
249
    {
250 10
        $classMap = [];
251 10
        foreach ($finder as $file) {
252 10
            $document = new \DOMDocument();
253 10
            $document->load($file);
254
255 10
            $xpath = new \DOMXPath($document);
256
257 10
            $classMap = array_reduce(
258 10
                iterator_to_array($xpath->query('//class')),
259
                function (array $classMap, \DOMElement $element) {
260 10
                    $classMap[$element->getAttribute('name')] = $element;
261 10
                    return $classMap;
262 10
                },
263
                $classMap
264 5
            );
265 5
        }
266
267 10
        return $classMap;
268
    }
269
270
    /**
271
     * Load schema class map
272
     *
273
     * @param Finder $finder Mapping finder
274
     * @return array
275
     */
276 10
    private function loadSchemaClassMap(Finder $finder)
277
    {
278 10
        $classMap = [];
279 10
        foreach ($finder as $file) {
280 10
            $schema = json_decode(file_get_contents($file), true);
281
282 10
            if (!isset($schema['x-documentClass'])) {
283 10
                continue;
284
            }
285
286 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...
287 2
                $classMap[$schema['x-documentClass']][$field]['required'] = true;
288 1
            }
289 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...
290 2
                $classMap[$schema['x-documentClass']][$field]['searchable'] = 1;
291 1
            }
292 2 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...
293 2
                $classMap[$schema['x-documentClass']][$field]['readOnly'] = true;
294 1
            }
295 5
        }
296
297 10
        return $classMap;
298
    }
299
300
    /**
301
     * Load validation class map
302
     *
303
     * @param Finder $finder Mapping finder
304
     * @return array
305
     */
306 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...
307
    {
308 10
        $classMap = [];
309 10
        foreach ($finder as $file) {
310 10
            $document = new \DOMDocument();
311 10
            $document->load($file);
312
313 10
            $xpath = new \DOMXPath($document);
314 10
            $xpath->registerNamespace('constraint', 'http://symfony.com/schema/dic/constraint-mapping');
315
316 10
            $classMap = array_reduce(
317 10
                iterator_to_array($xpath->query('//constraint:class')),
318
                function (array $classMap, \DOMElement $element) {
319 10
                    $classMap[$element->getAttribute('name')] = $element;
320 10
                    return $classMap;
321 10
                },
322
                $classMap
323 5
            );
324 5
        }
325
326 10
        return $classMap;
327
    }
328
329
    /**
330
     * Get serializer fields
331
     *
332
     * @param \DOMElement $mapping Serializer XML mapping
333
     * @return array
334
     */
335 10
    private function getSerializerFields(\DOMElement $mapping)
336
    {
337 10
        $xpath = new \DOMXPath($mapping->ownerDocument);
338
339 10
        return array_map(
340
            function (\DOMElement $element) {
341
                return [
342 10
                    'fieldName'   => $element->getAttribute('name'),
343 10
                    'fieldType'   => $this->getSerializerFieldType($element),
344 10
                    'exposedName' => $element->getAttribute('serialized-name') ?: $element->getAttribute('name'),
345 10
                    'readOnly'    => $element->getAttribute('read-only') === 'true',
346 10
                    'searchable'  => (int) $element->getAttribute('searchable')
347 5
                ];
348 10
            },
349 10
            iterator_to_array($xpath->query('property', $mapping))
350 5
        );
351
    }
352
353
    /**
354
     * Get serializer field type
355
     *
356
     * @param \DOMElement $field Field node
357
     * @return string|null
358
     */
359 10
    private function getSerializerFieldType(\DOMElement $field)
360
    {
361 10
        if ($field->getAttribute('type')) {
362 10
            return $field->getAttribute('type');
363
        }
364
365 2
        $xpath = new \DOMXPath($field->ownerDocument);
366
367 2
        $type = $xpath->query('type', $field)->item(0);
368 2
        return $type === null ? null : $type->nodeValue;
369
    }
370
371
    /**
372
     * Get validation fields
373
     *
374
     * @param \DOMElement $mapping Validation XML mapping
375
     * @return array
376
     */
377 10
    private function getValidationFields(\DOMElement $mapping)
378
    {
379 10
        $xpath = new \DOMXPath($mapping->ownerDocument);
380 10
        $xpath->registerNamespace('constraint', 'http://symfony.com/schema/dic/constraint-mapping');
381
382 10
        return array_map(
383
            function (\DOMElement $element) use ($xpath) {
384 4
                $constraints = $xpath->query('constraint:constraint[@name="NotBlank" or @name="NotNull"]', $element);
385
                return [
386 4
                    'fieldName' => $element->getAttribute('name'),
387 4
                    'required'  => $constraints->length > 0,
388 2
                ];
389 10
            },
390 10
            iterator_to_array($xpath->query('constraint:property', $mapping))
391 5
        );
392
    }
393
394
    /**
395
     * Get doctrine document fields
396
     *
397
     * @param \DOMElement $mapping Doctrine XML mapping
398
     * @return array
399
     */
400 10 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...
401
    {
402 10
        $xpath = new \DOMXPath($mapping->ownerDocument);
403 10
        $xpath->registerNamespace('doctrine', 'http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping');
404
405 10
        return array_map(
406
            function (\DOMElement $element) {
407
                return [
408 10
                    'name' => $element->getAttribute('fieldName'),
409 10
                    'type' => $element->getAttribute('type'),
410 5
                ];
411 10
            },
412 10
            iterator_to_array($xpath->query('doctrine:field', $mapping))
413 5
        );
414
    }
415
416
    /**
417
     * Get doctrine document embed-one fields
418
     *
419
     * @param \DOMElement $mapping Doctrine XML mapping
420
     * @return array
421
     */
422 10 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...
423
    {
424 10
        $xpath = new \DOMXPath($mapping->ownerDocument);
425 10
        $xpath->registerNamespace('doctrine', 'http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping');
426
427 10
        return array_map(
428
            function (\DOMElement $element) {
429
                return [
430 10
                    'name' => $element->getAttribute('field'),
431 10
                    'type' => $element->getAttribute('target-document'),
432 5
                ];
433 10
            },
434 10
            iterator_to_array($xpath->query('*[self::doctrine:embed-one or self::doctrine:reference-one]', $mapping))
435 5
        );
436
    }
437
438
    /**
439
     * Get doctrine document embed-many fields
440
     *
441
     * @param \DOMElement $mapping Doctrine XML mapping
442
     * @return array
443
     */
444 10 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...
445
    {
446 10
        $xpath = new \DOMXPath($mapping->ownerDocument);
447 10
        $xpath->registerNamespace('doctrine', 'http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping');
448
449 10
        return array_map(
450 10
            function (\DOMElement $element) {
451
                return [
452 10
                    'name' => $element->getAttribute('field'),
453 10
                    'type' => $element->getAttribute('target-document'),
454 5
                ];
455 10
            },
456 10
            iterator_to_array($xpath->query('*[self::doctrine:embed-many or self::doctrine:reference-many]', $mapping))
457 5
        );
458
    }
459
460
    /**
461
     * Gets an array of all fields, flat with full internal name in dot notation as key and
462
     * the exposed field name as value. You can pass a callable to limit the fields return a subset of fields.
463
     * If the callback returns true, the field will be included in the output. You will get the field definition
464
     * passed to your callback.
465
     *
466
     * @param Document $document       The document
467
     * @param string   $documentPrefix Document field prefix
468
     * @param string   $exposedPrefix  Exposed field prefix
469
     * @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...
470
     *
471
     * @return array
472
     */
473 4
    public function getFieldNamesFlat(
474
        Document $document,
475
        $documentPrefix = '',
476
        $exposedPrefix = '',
477
        callable $callback = null
478
    ) {
479 4
        $result = [];
480 4
        foreach ($document->getFields() as $field) {
481 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...
482 4
                $result[$documentPrefix . $field->getFieldName()] = $exposedPrefix . $field->getExposedName();
483 2
            }
484
485 4
            if ($field instanceof ArrayField) {
486 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...
487 2
                    $result[$documentPrefix . $field->getFieldName() . '.0'] =
488 2
                        $exposedPrefix . $field->getExposedName() . '.0';
489 1
                }
490 2
            } elseif ($field instanceof EmbedOne) {
491 4
                $result = array_merge(
492 2
                    $result,
493 4
                    $this->getFieldNamesFlat(
494 4
                        $field->getDocument(),
495 4
                        $documentPrefix.$field->getFieldName().'.',
496 4
                        $exposedPrefix.$field->getExposedName().'.',
497
                        $callback
498 2
                    )
499 2
                );
500 2
            } elseif ($field instanceof EmbedMany) {
501 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...
502 4
                    $result[$documentPrefix . $field->getFieldName() . '.0'] =
503 4
                        $exposedPrefix . $field->getExposedName() . '.0';
504 2
                }
505 4
                $result = array_merge(
506 2
                    $result,
507 4
                    $this->getFieldNamesFlat(
508 4
                        $field->getDocument(),
509 4
                        $documentPrefix.$field->getFieldName().'.0.',
510 4
                        $exposedPrefix.$field->getExposedName().'.0.',
511
                        $callback
512 2
                    )
513 2
                );
514 2
            }
515 2
        }
516
517 4
        return $result;
518
    }
519
520
    /**
521
     * Simple function to check whether a given shall be returned in the output of getFieldNamesFlat
522
     * and the optional given callback there.
523
     *
524
     * @param AbstractField $field    field
525
     * @param callable|null $callback optional callback
526
     *
527
     * @return bool|mixed true if field should be returned, false otherwise
528
     */
529 4
    private function getFlatFieldCheckCallback($field, callable $callback = null)
530
    {
531 4
        if (!is_callable($callback)) {
532 4
            return true;
533
        }
534
535 2
        return call_user_func($callback, $field);
536
    }
537
}
538