Completed
Pull Request — master (#1734)
by
unknown
05:52
created

MappingBuilder::buildMapping()   F

Complexity

Conditions 11
Paths 512

Size

Total Lines 44

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 11.4763

Importance

Changes 0
Metric Value
dl 0
loc 44
rs 3.8277
c 0
b 0
f 0
ccs 16
cts 19
cp 0.8421
cc 11
nc 512
nop 2
crap 11.4763

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
/*
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\PostMappingBuilderEvent;
17
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
18
19
class MappingBuilder
20
{
21
    /**
22
     * @var EventDispatcherInterface
23
     */
24 8
    private $dispatcher;
25
26 8
    public function __construct(EventDispatcherInterface $eventDispatcher)
27
    {
28 8
        $this->dispatcher = $eventDispatcher;
29 8
    }
30 8
31
    /**
32
     * Builds mappings for an entire index.
33 8
     *
34 8
     * @return array
35 5
     */
36
    public function buildIndexMapping(IndexConfigInterface $indexConfig)
37
    {
38 8
        $mapping = $this->buildMapping($indexConfig->getModel(), $indexConfig);
39
40
        $mappingIndex = [];
41
        if (!empty($mapping)) {
42
            $mappingIndex['mappings'] = $mapping;
43
        }
44
45
        $settings = $indexConfig->getSettings();
46 5
        if (!empty($settings)) {
47
            $mappingIndex['settings'] = $settings;
48 5
        }
49 5
50
        $this->dispatcher->dispatch($event = new PostMappingBuilderEvent($indexConfig, $mapping));
0 ignored issues
show
Compatibility introduced by
$indexConfig of type object<FOS\ElasticaBundl...n\IndexConfigInterface> is not a sub-type of object<FOS\ElasticaBundl...figuration\IndexConfig>. It seems like you assume a concrete implementation of the interface FOS\ElasticaBundle\Confi...on\IndexConfigInterface to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

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