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

ViewOperations   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 30
c 0
b 0
f 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A toMutable() 0 3 2
A from() 0 3 1
B merge() 0 18 5
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