HasOneOrMany   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 46
ccs 14
cts 14
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A matchOneSimple() 0 3 1
A matchOneOrManySimple() 0 22 4
1
<?php
2
3
namespace Volosyuk\SimpleEloquent\Relations;
4
5
use Illuminate\Support\Collection;
6
use Volosyuk\SimpleEloquent\ModelAccessor;
7
8
/**
9
 * @package Volosyuk\SimpleEloquent
10
 */
11
trait HasOneOrMany
12
{
13
    /**
14
     * Match the eagerly loaded results to their single parents.
15
     *
16
     * @param  array   $models
17
     * @param  Collection  $results
18
     * @param  string  $relation
19
     * @return array
20
     */
21 2
    protected function matchOneSimple(array $models, Collection $results, $relation)
22
    {
23 2
        return $this->matchOneOrManySimple($models, $results, $relation, 'one');
24
    }
25
26
    /**
27
     * Match the eagerly loaded results to their many parents.
28
     *
29
     * @param  array   $models
30
     * @param  Collection  $results
31
     * @param  string  $relation
32
     * @param  string  $type
33
     * @return array
34
     */
35 2
    protected function matchOneOrManySimple(array &$models, Collection $results, $relation, $type)
36
    {
37 2
        $dictionary = [];
38
39 2
        $foreign = $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
        /** @scrutinizer ignore-call */ 
40
        $foreign = $this->getForeignKeyName();
Loading history...
40
41 2
        foreach ($results as $result) {
42 2
            $dictionary[ModelAccessor::get($result, $foreign)][] = $result;
43
        }
44
45 2
        foreach ($models as &$model) {
46 2
            $key = ModelAccessor::get($model, $this->localKey);
47
48 2
            if (isset($dictionary[$key])) {
49 2
                $value = $this->getRelationValue($dictionary, $key, $type);
0 ignored issues
show
Bug introduced by
It seems like getRelationValue() 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

49
                /** @scrutinizer ignore-call */ 
50
                $value = $this->getRelationValue($dictionary, $key, $type);
Loading history...
50
51 2
                ModelAccessor::set($model, $relation, $value);
52
            }
53
        }
54 2
        unset($model);
55
56 2
        return $models;
57
    }
58
}
59