Completed
Push — master ( 164477...e1937a )
by
unknown
09:42 queued 10s
created

src/Index/MappingBuilder.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/*
4
 * This file is part of the FOSElasticaBundle package.
5
 *
6
 * (c) FriendsOfSymfony <https://friendsofsymfony.github.com/>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace FOS\ElasticaBundle\Index;
13
14
use FOS\ElasticaBundle\Configuration\IndexConfigInterface;
15
use FOS\ElasticaBundle\Configuration\IndexTemplateConfig;
16
use FOS\ElasticaBundle\Event\PostMappingBuildEvent;
17
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
18
19
class MappingBuilder
20
{
21
    /**
22
     * @var EventDispatcherInterface
23
     */
24
    private $dispatcher;
25
26 10
    public function __construct(EventDispatcherInterface $eventDispatcher)
27
    {
28 10
        $this->dispatcher = $eventDispatcher;
29 10
    }
30
31
    /**
32
     * Builds mappings for an entire index.
33
     */
34 8
    public function buildIndexMapping(IndexConfigInterface $indexConfig): array
35
    {
36 8
        $mappingIndex = [];
37 8
        $mapping = $this->buildMapping($indexConfig->getModel(), $indexConfig);
38 8
        $this->dispatcher->dispatch($event = new PostMappingBuildEvent($indexConfig, $mapping));
0 ignored issues
show
$event = new \FOS\Elasti...$indexConfig, $mapping) is of type object<FOS\ElasticaBundl...\PostMappingBuildEvent>, but the function expects a object<Symfony\Contracts\EventDispatcher\object>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39
40 8
        $mapping = $event->getMapping();
41 8
        $settings = $indexConfig->getSettings();
42
43 8
        if ($mapping) {
44 8
            $mappingIndex['mappings'] = $mapping;
45
        }
46
47 8
        if ($settings) {
48 5
            $mappingIndex['settings'] = $settings;
49
        }
50
51 8
        return $mappingIndex;
52
    }
53
54
    /**
55
     * Builds mappings for an entire index template.
56
     */
57 5
    public function buildIndexTemplateMapping(IndexTemplateConfig $indexTemplateConfig): array
58
    {
59 5
        $mapping = $this->buildIndexMapping($indexTemplateConfig);
60 5
        $mapping['template'] = $indexTemplateConfig->getTemplate();
61
62 5
        return $mapping;
63
    }
64
65
    /**
66
     * Builds mappings for a single type.
67
     */
68 9
    public function buildMapping(?string $model, IndexConfigInterface $indexConfig): array
69
    {
70 9
        $mapping = $indexConfig->getMapping();
71
72 9
        if (null !== $indexConfig->getDynamicDateFormats()) {
73 7
            $mapping['dynamic_date_formats'] = $indexConfig->getDynamicDateFormats();
74
        }
75
76 9
        if (null !== $indexConfig->getDateDetection()) {
77 1
            $mapping['date_detection'] = $indexConfig->getDateDetection();
78
        }
79
80 9
        if (null !== $indexConfig->getNumericDetection()) {
81 1
            $mapping['numeric_detection'] = $indexConfig->getNumericDetection();
82
        }
83
84 9
        if ($indexConfig->getAnalyzer()) {
85
            $mapping['analyzer'] = $indexConfig->getAnalyzer();
86
        }
87
88 9
        if (null !== $indexConfig->getDynamic()) {
89 1
            $mapping['dynamic'] = $indexConfig->getDynamic();
90
        }
91
92 9
        if (isset($mapping['dynamic_templates']) && !$mapping['dynamic_templates']) {
93 6
            unset($mapping['dynamic_templates']);
94
        }
95
96 9
        $this->fixProperties($mapping['properties']);
97 9
        if (!$mapping['properties']) {
98
            unset($mapping['properties']);
99
        }
100
101 9
        if ($model) {
102 2
            $mapping['_meta']['model'] = $model;
103
        }
104
105 9
        return $mapping;
106
    }
107
108
    /**
109
     * Fixes any properties and applies basic defaults for any field that does not have
110
     * required options.
111
     */
112 9
    private function fixProperties(array &$properties): void
113
    {
114 9
        foreach ($properties as $name => &$property) {
115 9
            unset($property['property_path']);
116 9
            $property['type'] = $property['type'] ?? 'text';
117
118 9
            if (isset($property['fields'])) {
119 1
                $this->fixProperties($property['fields']);
120
            }
121 9
            if (isset($property['properties'])) {
122 1
                $this->fixProperties($property['properties']);
123
            }
124
        }
125 9
    }
126
}
127