Passed
Pull Request — master (#232)
by
unknown
15:42
created

HasRecursiveRelationships   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Importance

Changes 10
Bugs 0 Features 0
Metric Value
wmc 6
eloc 10
c 10
b 0
f 0
dl 0
loc 25
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A isChildOf() 0 3 2
A depthRelatedTo() 0 10 3
A isParentOf() 0 3 1
1
<?php
2
3
namespace Staudenmeir\LaravelAdjacencyList\Eloquent;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Staudenmeir\LaravelAdjacencyList\Eloquent\Traits\HasAdjacencyList;
7
use Staudenmeir\LaravelCte\Eloquent\QueriesExpressions;
8
9
trait HasRecursiveRelationships
10
{
11
    use HasAdjacencyList;
12
    use QueriesExpressions;
13
14
    public function isParentOf(Model $model)
15
    {
16
        return $this->children->contains($model);
17
    }
18
19
    public function isChildOf(Model $model)
20
    {
21
        return $this->parent ? $this->parent->is($model) : false;
22
    }
23
24
    public function depthRelatedTo(Model $model)
25
    {
26
        $currentModel = $this->bloodline->firstWhere('id', $this->getKey());
0 ignored issues
show
Bug introduced by
It seems like getKey() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
        $currentModel = $this->bloodline->firstWhere('id', $this->/** @scrutinizer ignore-call */ getKey());
Loading history...
27
        $relatedModel = $this->bloodline->firstWhere('id', $model->getKey());
28
29
        if ($currentModel && $relatedModel) {
30
            return $currentModel->depth - $relatedModel->depth;
31
        }
32
33
        return null;
34
    }
35
}
36