Passed
Push — feature/initial-implementation ( fbd338...bbedf1 )
by Fike
02:24
created

Mappings   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 10
c 0
b 0
f 0
wmc 18

5 Methods

Rating   Name   Duplication   Size   Complexity  
A conflict() 0 12 4
A haveConflicts() 0 9 3
A havePropertyConflicts() 0 8 4
A haveParameterConflicts() 0 8 4
A haveTypeConflict() 0 3 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AmaTeam\ElasticSearch\Mapping;
6
7
use AmaTeam\ElasticSearch\API\Mapping\MappingInterface;
8
9
class Mappings
10
{
11
    public static function conflict(MappingInterface ...$mappings): bool
12
    {
13
        $mappings = array_values($mappings);
14
        for ($i = 0; $i < sizeof($mappings) - 1; $i++) {
15
            $subject = $mappings[$i];
16
            for ($j = $i + 1; $j < sizeof($mappings); $j++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function sizeof() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
17
                if (static::haveConflicts($subject, $mappings[$j])) {
18
                    return true;
19
                }
20
            }
21
        }
22
        return false;
23
    }
24
25
    private static function haveConflicts(MappingInterface $subject, MappingInterface $opponent): bool
26
    {
27
        if (static::haveTypeConflict($subject, $opponent)) {
28
            return true;
29
        }
30
        if (static::haveParameterConflicts($subject, $opponent)) {
31
            return true;
32
        }
33
        return static::havePropertyConflicts($subject, $opponent);
34
    }
35
36
    private static function haveTypeConflict(MappingInterface $subject, MappingInterface $opponent): bool
37
    {
38
        return $subject->getType() && $opponent->getType() && $subject->getType() !== $opponent->getType();
0 ignored issues
show
Bug Best Practice introduced by
The expression $opponent->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...
Bug Best Practice introduced by
The expression $subject->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...
39
    }
40
41
    private static function haveParameterConflicts(MappingInterface $subject, MappingInterface $opponent): bool
42
    {
43
        foreach ($subject->getParameters() as $parameter => $value) {
44
            if ($opponent->hasParameter($parameter) && $opponent->getParameter($parameter) != $value) {
45
                return true;
46
            }
47
        }
48
        return false;
49
    }
50
51
    private static function havePropertyConflicts(MappingInterface $subject, MappingInterface $opponent): bool
52
    {
53
        foreach ($subject->getProperties() as $name => $property) {
54
            if ($opponent->hasProperty($name) && static::haveConflicts($property, $opponent->getProperty($name))) {
55
                return true;
56
            }
57
        }
58
        return false;
59
    }
60
}
61