Passed
Push — master ( d78780...0417e4 )
by Aleksei
02:26
created

HasChangesTrait   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 22
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 22
rs 10
wmc 9

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hasChanges() 0 11 5
A sameReference() 0 7 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cycle\ORM\Relation\Traits;
6
7
use Cycle\ORM\Promise\PromiseInterface;
8
use Cycle\ORM\Promise\ReferenceInterface;
9
10
trait HasChangesTrait
11
{
12
    public function hasChanges($related, $original): bool
13
    {
14
        if ($related instanceof PromiseInterface && $related->__loaded()) {
15
            return true;
16
        }
17
18
        if (null === $related && null === $original) {
19
            return false;
20
        }
21
22
        return !$this->sameReference($related, $original);
23
    }
24
25
    protected function sameReference($a, $b): bool
26
    {
27
        if (!$a instanceof ReferenceInterface || !$b instanceof ReferenceInterface) {
28
            return false;
29
        }
30
31
        return $a->__role() === $b->__role() && $a->__scope() === $b->__scope();
32
    }
33
}
34