Passed
Push — feature/initial-implementation ( fae671...591f29 )
by Fike
02:37
created

ViewOperations::merge()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
c 0
b 0
f 0
rs 8.8571
cc 6
eloc 11
nc 17
nop 1
1
<?php
2
3
namespace AmaTeam\ElasticSearch\Entity\Mapping\Structure;
4
5
use AmaTeam\ElasticSearch\API\Entity\Mapping\Structure\View;
6
use AmaTeam\ElasticSearch\API\Entity\Mapping\Structure\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
            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...
15
                $target->setType($view->getType());
16
            }
17
            foreach ($view->getParameters() as $parameter => $value) {
18
                $target->setParameter($parameter, $value);
19
            }
20
            foreach ($view->getIgnoredProperties() as $property) {
21
                $target->addIgnoredProperty($property);
22
            }
23
            foreach ($view->getExistingProperties() as $property) {
24
                $target->forgetIgnoredProperty($property);
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