matchResultsForDeepRelationship()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
c 1
b 0
f 0
dl 0
loc 21
ccs 9
cts 9
cp 1
rs 10
cc 4
nc 4
nop 4
crap 4
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 IsConcatenableDescendantsRelation
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 42
    public function addEagerConstraintsToDeepRelationship(Builder $query, array $models): void
21
    {
22 42
        $andSelf = $this->andSelf;
23
24 42
        $this->andSelf = true;
0 ignored issues
show
Bug Best Practice introduced by
The property andSelf does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
25
26 42
        $this->addEagerConstraints($models);
0 ignored issues
show
Bug introduced by
The method addEagerConstraints() does not exist on Staudenmeir\LaravelAdjac...ableDescendantsRelation. 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

26
        $this->/** @scrutinizer ignore-call */ 
27
               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...
27
28 42
        $this->andSelf = $andSelf;
29
30 42
        $this->mergeExpressions($query, $this->query);
31
    }
32
33
    /**
34
     * Match the eagerly loaded results for a deep relationship to their parents.
35
     *
36
     * @param array $models
37
     * @param \Illuminate\Database\Eloquent\Collection $results
38
     * @param string $relation
39
     * @param string $type
40
     * @return array
41
     */
42 42
    public function matchResultsForDeepRelationship(
43
        array $models,
44
        Collection $results,
45
        string $relation,
46
        string $type = 'many'
47
    ): array {
48 42
        $dictionary = $this->buildDictionaryForDeepRelationship($results);
49
50 42
        foreach ($models as $model) {
51 42
            $key = $model->{$this->localKey};
52
53 42
            if (isset($dictionary[$key])) {
54 42
                $value = $dictionary[$key];
55
56 42
                $value = $type === 'one' ? reset($value) : $this->related->newCollection($value);
57
58 42
                $model->setRelation($relation, $value);
59
            }
60
        }
61
62 42
        return $models;
63
    }
64
65
    /**
66
     * Build the model dictionary for a deep relation.
67
     *
68
     * @param \Illuminate\Database\Eloquent\Collection $results
69
     * @return array
70
     */
71 42
    protected function buildDictionaryForDeepRelationship(Collection $results): array
72
    {
73 42
        $pathSeparator = $this->related->getPathSeparator();
74
75 42
        if (!$this->andSelf) {
76 18
            $results = $results->filter(
77 18
                fn (Model $result) => str_contains($result->laravel_through_key, $pathSeparator)
78 18
            );
79
        }
80
81 42
        return $results->mapToDictionary(function (Model $result) use ($pathSeparator) {
82 42
            $key = strtok($result->laravel_through_key, $pathSeparator);
83
84 42
            return [$key => $result];
85 42
        })->all();
86
    }
87
}
88