Passed
Push — master ( 2be340...cdee59 )
by Jonas
13:41
created

IsConcatenableAncestorsRelation   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 17
c 1
b 0
f 0
dl 0
loc 60
ccs 18
cts 18
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addEagerConstraintsToDeepRelationship() 0 5 1
A matchResultsForDeepRelationship() 0 17 4
A buildDictionaryForDeepRelationship() 0 9 1
1
<?php
2
3
namespace Staudenmeir\LaravelAdjacencyList\Eloquent\Relations\Traits\Concatenation;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Collection;
7
use Illuminate\Database\Eloquent\Model;
8
9
trait IsConcatenableAncestorsRelation
10
{
11
    use IsConcatenableRelation;
12
13
    /**
14
     * Set the constraints for an eager load of the deep relation.
15
     *
16
     * @param \Illuminate\Database\Eloquent\Builder $query
17
     * @param array $models
18
     * @return void
19
     */
20 20
    public function addEagerConstraintsToDeepRelationship(Builder $query, array $models): void
21
    {
22 20
        $this->addEagerConstraints($models);
0 ignored issues
show
Bug introduced by
The method addEagerConstraints() does not exist on Staudenmeir\LaravelAdjac...enableAncestorsRelation. Did you maybe mean addEagerConstraintsToDeepRelationship()? ( Ignorable by Annotation )

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

22
        $this->/** @scrutinizer ignore-call */ 
23
               addEagerConstraints($models);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
23
24 20
        $this->mergeExpressions($query, $this->query);
25
    }
26
27
    /**
28
     * Match the eagerly loaded results for a deep relationship to their parents.
29
     *
30
     * @param array $models
31
     * @param \Illuminate\Database\Eloquent\Collection $results
32
     * @param string $relation
33
     * @return array
34
     */
35 20
    public function matchResultsForDeepRelationship(array $models, Collection $results, string $relation): array
36
    {
37 20
        $dictionary = $this->buildDictionaryForDeepRelationship($results);
38
39 20
        $attribute = $this->andSelf ? $this->localKey : $this->getForeignKeyName();
0 ignored issues
show
Bug introduced by
It seems like getForeignKeyName() 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

39
        $attribute = $this->andSelf ? $this->localKey : $this->/** @scrutinizer ignore-call */ getForeignKeyName();
Loading history...
40
41 20
        foreach ($models as $model) {
42 20
            $key = $model->$attribute;
43
44 20
            if (isset($dictionary[$key])) {
45 20
                $value = $this->related->newCollection($dictionary[$key]);
46
47 20
                $model->setRelation($relation, $value);
48
            }
49
        }
50
51 20
        return $models;
52
    }
53
54
    /**
55
     * Build the model dictionary for a deep relation.
56
     *
57
     * @param \Illuminate\Database\Eloquent\Collection $results
58
     * @return array
59
     */
60 20
    protected function buildDictionaryForDeepRelationship(Collection $results): array
61
    {
62 20
        $pathSeparator = $this->related->getPathSeparator();
63
64 20
        return $results->mapToDictionary(function (Model $result) use ($pathSeparator) {
65 20
            $key = strtok($result->laravel_through_key, $pathSeparator);
66
67 20
            return [$key => $result];
68 20
        })->all();
69
    }
70
}
71