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

Mappings::conflicts()   B

Complexity

Conditions 10
Paths 8

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 9
nc 8
nop 2
dl 0
loc 16
rs 7.2765
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A Mappings::haveTypeConflict() 0 3 3

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
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