HasEagerLoading::match()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 8
dl 0
loc 15
rs 10
c 1
b 0
f 0
cc 4
nc 4
nop 3
1
<?php
2
3
namespace Staudenmeir\EloquentHasManyDeep\Eloquent\Relations\Traits;
4
5
use Illuminate\Database\Eloquent\Collection;
6
7
trait HasEagerLoading
8
{
9
    /**
10
     * Set the constraints for an eager load of the relation.
11
     *
12
     * @param array $models
13
     * @return void
14
     */
15
    public function addEagerConstraints(array $models)
16
    {
17
        if ($this->customEagerConstraintsCallback) {
18
            ($this->customEagerConstraintsCallback)($this->query, $models);
19
            return;
20
        }
21
22
        if ($this->hasLeadingCompositeKey()) {
0 ignored issues
show
Bug introduced by
It seems like hasLeadingCompositeKey() 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

22
        if ($this->/** @scrutinizer ignore-call */ hasLeadingCompositeKey()) {
Loading history...
23
            $this->addEagerConstraintsWithCompositeKey($models);
0 ignored issues
show
Bug introduced by
The method addEagerConstraintsWithCompositeKey() does not exist on Staudenmeir\EloquentHasM...\Traits\HasEagerLoading. Did you maybe mean addEagerConstraints()? ( Ignorable by Annotation )

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

23
            $this->/** @scrutinizer ignore-call */ 
24
                   addEagerConstraintsWithCompositeKey($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...
24
        } else {
25
            parent::addEagerConstraints($models);
26
27
            if (is_array($this->foreignKeys[0])) {
28
                $this->query->where(
29
                    $this->throughParent->qualifyColumn($this->foreignKeys[0][0]),
30
                    '=',
31
                    $this->farParent->getMorphClass()
32
                );
33
            }
34
        }
35
    }
36
37
    /**
38
     * Match the eagerly loaded results to their parents.
39
     *
40
     * @param array $models
41
     * @param \Illuminate\Database\Eloquent\Collection $results
42
     * @param string $relation
43
     * @return array
44
     */
45
    public function match(array $models, Collection $results, $relation)
46
    {
47
        if ($this->customEagerMatchingCallbacks) {
48
            foreach ($this->customEagerMatchingCallbacks as $callback) {
49
                $models = $callback($models, $results, $relation);
50
            }
51
52
            return $models;
53
        }
54
55
        if ($this->hasLeadingCompositeKey()) {
56
            return $this->matchWithCompositeKey($models, $results, $relation);
0 ignored issues
show
Bug introduced by
It seems like matchWithCompositeKey() 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

56
            return $this->/** @scrutinizer ignore-call */ matchWithCompositeKey($models, $results, $relation);
Loading history...
57
        }
58
59
        return parent::match($models, $results, $relation);
60
    }
61
}
62