MatchTrait::match()   B
last analyzed

Complexity

Conditions 10
Paths 7

Size

Total Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 7.6666
c 0
b 0
f 0
cc 10
nc 7
nop 2

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
 * Spiral, Core Components
4
 *
5
 * @author Wolfy-J
6
 */
7
8
namespace Spiral\ORM\Entities\Relations\Traits;
9
10
use Spiral\ORM\RecordInterface;
11
12
/**
13
 * Provides ability to compare entity and query.
14
 */
15
trait MatchTrait
16
{
17
    /**
18
     * Match entity by field intersection, instance values or primary key.
19
     *
20
     * @param RecordInterface             $record
21
     * @param RecordInterface|array|mixed $query
22
     *
23
     * @return bool
24
     */
25
    protected function match(RecordInterface $record, $query): bool
26
    {
27
        if ($record === $query) {
28
            //Strict search
29
            return true;
30
        }
31
32
        //Primary key comparision
33
        if (is_scalar($query) && $record->primaryKey() == $query) {
34
            return true;
35
        }
36
37
        //Record based comparision
38
        if ($query instanceof RecordInterface) {
39
            //Matched by PK (assuming both same class)
40
            if (!empty($query->primaryKey()) && $query->primaryKey() == $record->primaryKey()) {
41
                return true;
42
            }
43
44
            //Matched by content (this is a bit tricky!)
45
            if ($record->packValue() == $query->packValue()) {
46
                return true;
47
            }
48
49
            return false;
50
        }
51
52
        //Field intersection
53
        if (is_array($query) && array_intersect_assoc($record->packValue(), $query) == $query) {
54
            return true;
55
        }
56
57
        return false;
58
    }
59
}