Completed
Pull Request — master (#1734)
by
unknown
03:41
created

MappingBuilder::buildIndexMapping()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.6666
c 0
b 0
f 0
cc 3
nc 4
nop 1
crap 3
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
     * @return array
35
     */
36 8
    public function buildIndexMapping(IndexConfigInterface $indexConfig)
37
    {
38 8
        $mapping = $this->buildMapping($indexConfig->getModel(), $indexConfig);
39
40 8
        $mappingIndex = [];
41 8
        if (!empty($mapping)) {
42 8
            $mappingIndex['mappings'] = $mapping;
43
        }
44
45 8
        $settings = $indexConfig->getSettings();
46 8
        if (!empty($settings)) {
47 5
            $mappingIndex['settings'] = $settings;
48
        }
49
50 8
        $this->dispatcher->dispatch($event = new PostMappingBuildEvent($indexConfig, $mapping));
0 ignored issues
show
Bug introduced by
It seems like $mapping defined by $this->buildMapping($ind...tModel(), $indexConfig) on line 38 can also be of type object<ArrayObject>; however, FOS\ElasticaBundle\Event...ildEvent::__construct() does only seem to accept array, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Documentation introduced by
$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...
51
52 8
        return $mappingIndex;
53
    }
54
55
    /**
56
     * Builds mappings for an entire index template.
57
     *
58
     * @return array
59
     */
60 5
    public function buildIndexTemplateMapping(IndexTemplateConfig $indexTemplateConfig)
61
    {
62 5
        $mapping = $this->buildIndexMapping($indexTemplateConfig);
63 5
        $mapping['template'] = $indexTemplateConfig->getTemplate();
64
65 5
        return $mapping;
66
    }
67
68
    /**
69
     * Builds mappings for a single type.
70
     *
71
     * @return array
72
     */
73 9
    public function buildMapping(?string $model, IndexConfigInterface $indexConfig)
74
    {
75 9
        $mapping = $indexConfig->getMapping();
76
77 9
        if (null !== $indexConfig->getDynamicDateFormats()) {
78 7
            $mapping['dynamic_date_formats'] = $indexConfig->getDynamicDateFormats();
79
        }
80
81 9
        if (null !== $indexConfig->getDateDetection()) {
82 1
            $mapping['date_detection'] = $indexConfig->getDateDetection();
83
        }
84
85 9
        if (null !== $indexConfig->getNumericDetection()) {
86 1
            $mapping['numeric_detection'] = $indexConfig->getNumericDetection();
87
        }
88
89 9
        if ($indexConfig->getAnalyzer()) {
90
            $mapping['analyzer'] = $indexConfig->getAnalyzer();
91
        }
92
93 9
        if (null !== $indexConfig->getDynamic()) {
94 1
            $mapping['dynamic'] = $indexConfig->getDynamic();
95
        }
96
97 9
        if (isset($mapping['dynamic_templates']) and empty($mapping['dynamic_templates'])) {
98 6
            unset($mapping['dynamic_templates']);
99
        }
100
101 9
        $this->fixProperties($mapping['properties']);
102 9
        if (!$mapping['properties']) {
103
            unset($mapping['properties']);
104
        }
105
106 9
        if ($model) {
107 2
            $mapping['_meta']['model'] = $model;
108
        }
109
110 9
        if (empty($mapping)) {
111
            // Empty mapping, we want it encoded as a {} instead of a []
112
            $mapping = new \ArrayObject();
113
        }
114
115 9
        return $mapping;
116
    }
117
118
    /**
119
     * Fixes any properties and applies basic defaults for any field that does not have
120
     * required options.
121
     *
122
     * @param $properties
123
     */
124 9
    private function fixProperties(&$properties)
125
    {
126 9
        foreach ($properties as $name => &$property) {
127 9
            unset($property['property_path']);
128
129 9
            if (!isset($property['type'])) {
130 3
                $property['type'] = 'text';
131
            }
132 9
            if (isset($property['fields'])) {
133 1
                $this->fixProperties($property['fields']);
134
            }
135 9
            if (isset($property['properties'])) {
136 1
                $this->fixProperties($property['properties']);
137
            }
138
        }
139 9
    }
140
}
141