Completed
Push — master ( a8021b...bf7228 )
by
unknown
14s queued 11s
created

MappingBuilder::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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