Passed
Push — dev ( 40f0b5...3a2e98 )
by Fike
02:50
created

ViewOperations::merge()   B

Complexity

Conditions 5
Paths 9

Size

Total Lines 18
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
rs 8.8571
cc 5
eloc 12
nc 9
nop 1
1
<?php
2
3
namespace AmaTeam\ElasticSearch\Entity\Mapping\Property;
4
5
use AmaTeam\ElasticSearch\API\Entity\Mapping\Property\View;
6
use AmaTeam\ElasticSearch\API\Entity\Mapping\Property\ViewInterface;
7
8
class ViewOperations
9
{
10
    public static function merge(ViewInterface... $views): View
11
    {
12
        $target = new View();
13
        foreach ($views as $view) {
14
            $target
15
                ->setChildViews($view->getChildViews())
16
                ->setAppendChildViews($view->shouldAppendChildViews());
17
            if ($view->getType()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $view->getType() of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
18
                $target->setType($view->getType());
19
            }
20
            if ($view->getTargetEntity()) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $view->getTargetEntity() of type null|string is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
21
                $target->setTargetEntity($view->getTargetEntity());
22
            }
23
            foreach ($view->getParameters() as $parameter => $value) {
24
                $target->setParameter($parameter, $value);
25
            }
26
        }
27
        return $target;
28
    }
29
30
    public static function from(ViewInterface $view): View
31
    {
32
        return static::merge($view);
33
    }
34
35
    public static function toMutable(ViewInterface $view): View
36
    {
37
        return $view instanceof View ? $view : static::from($view);
38
    }
39
}
40