IsConcatenableAncestorsRelation   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 18
c 1
b 0
f 0
dl 0
loc 67
ccs 19
cts 19
cp 1
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addEagerConstraintsToDeepRelationship() 0 5 1
A matchResultsForDeepRelationship() 0 23 5
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 30
    public function addEagerConstraintsToDeepRelationship(Builder $query, array $models): void
21
    {
22 30
        $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 30
        $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
     * @param string $type
34
     * @return array
35
     */
36 30
    public function matchResultsForDeepRelationship(
37
        array $models,
38
        Collection $results,
39
        string $relation,
40
        string $type = 'many'
41
    ): array {
42 30
        $dictionary = $this->buildDictionaryForDeepRelationship($results);
43
44 30
        $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

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