Relation   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 62
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 12
c 3
b 0
f 0
dl 0
loc 62
ccs 15
cts 15
cp 1
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A addEagerConstraintsSimple() 0 3 1
A getKeys() 0 5 2
A eagerLoadAndMatchSimple() 0 5 1
A initSimpleRelation() 0 8 2
A getEagerSimple() 0 3 1
1
<?php
2
3
namespace Volosyuk\SimpleEloquent\Relations;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Support\Collection;
7
use stdClass;
8
use Volosyuk\SimpleEloquent\Builder;
9
use Volosyuk\SimpleEloquent\ModelAccessor;
10
11
/**
12
 * Trait SimpleRelation
13
 * @package Volosyuk\SimpleEloquent
14
 *
15
 * @property Builder $query
16
 * @property Model $parent
17
 */
18
trait Relation
19
{
20
    /**
21
     * @param $models
22
     * @param $name
23
     * @return array
24
     */
25 10
    public function eagerLoadAndMatchSimple($models, $name)
26
    {
27 10
        $results = $this->getEagerSimple();
28
29 10
        return $this->matchSimple($models, $results, $name);
0 ignored issues
show
Bug introduced by
It seems like matchSimple() 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

29
        return $this->/** @scrutinizer ignore-call */ matchSimple($models, $results, $name);
Loading history...
30
    }
31
32
    /**
33
     * @param array $models
34
     * @param $relation
35
     * @return array|stdClass[]
36
     */
37 11
    public function initSimpleRelation(array &$models, $relation)
38
    {
39 11
        foreach ($models as &$model) {
40 11
            ModelAccessor::set($model, $relation, null);
41
        }
42 11
        unset($model);
43
44 11
        return $models;
45
    }
46
47
    /**
48
     * Get the relationship for eager loading.
49
     *
50
     * @return Collection
51
     */
52 10
    protected function getEagerSimple()
53
    {
54 10
        return $this->getSimple();
0 ignored issues
show
Bug introduced by
It seems like getSimple() 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

54
        return $this->/** @scrutinizer ignore-call */ getSimple();
Loading history...
55
    }
56
57
    /**
58
     * Get all of the primary keys for an array of models.
59
     *
60
     * @param  array   $models
61
     * @param  string  $key
62
     * @return array
63
     */
64 7
    protected function getKeys(array $models, $key = null)
65
    {
66
        return array_unique(array_values(array_map(function ($value) use ($key) {
67 7
            return $key ? ModelAccessor::get($value, $key) : ModelAccessor::get($value, $this->parent->getKeyName());
68 7
        }, $models)));
69
    }
70
71
    /**
72
     * Set the constraints for an eager load of the relation.
73
     *
74
     * @param  array  $models
75
     * @return void
76
     */
77 4
    public function addEagerConstraintsSimple(array $models)
78
    {
79 4
        $this->query->whereIn($this->getQualifiedForeignKeyName(), $this->getKeys($models));
0 ignored issues
show
Bug introduced by
It seems like getQualifiedForeignKeyName() 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

79
        $this->query->whereIn($this->/** @scrutinizer ignore-call */ getQualifiedForeignKeyName(), $this->getKeys($models));
Loading history...
Bug introduced by
The method whereIn() does not exist on Volosyuk\SimpleEloquent\Builder. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

79
        $this->query->/** @scrutinizer ignore-call */ 
80
                      whereIn($this->getQualifiedForeignKeyName(), $this->getKeys($models));
Loading history...
80 4
    }
81
}
82

 

OSZAR »