Passed
Push — master ( b3421d...183369 )
by Jonas
16:50 queued 14:48
created

HasOneDeep   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 19
c 3
b 0
f 0
dl 0
loc 71
ccs 21
cts 21
cp 1
rs 10
wmc 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initRelation() 0 7 2
A getResults() 0 3 2
A newRelatedInstanceFor() 0 3 1
A match() 0 22 5
1
<?php
2
3
namespace Staudenmeir\EloquentHasManyDeep;
4
5
use Illuminate\Database\Eloquent\Collection;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Eloquent\Collection was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels;
8
9
class HasOneDeep extends HasManyDeep
10
{
11
    use SupportsDefaultModels;
12
13
    /**
14
     * Get the results of the relationship.
15
     *
16
     * @return mixed
17
     */
18 14
    public function getResults()
19
    {
20 14
        return $this->first() ?: $this->getDefaultFor(end($this->throughParents));
21
    }
22
23
    /**
24
     * Initialize the relation on a set of models.
25
     *
26
     * @param array $models
27
     * @param string $relation
28
     * @return array
29
     */
30 6
    public function initRelation(array $models, $relation)
31
    {
32 6
        foreach ($models as $model) {
33 6
            $model->setRelation($relation, $this->getDefaultFor($model));
34
        }
35
36 6
        return $models;
37
    }
38
39
    /**
40
     * Match the eagerly loaded results to their parents.
41
     *
42
     * @param array $models
43
     * @param \Illuminate\Database\Eloquent\Collection $results
44
     * @param string $relation
45
     * @return array
46
     */
47 6
    public function match(array $models, Collection $results, $relation)
48
    {
49 6
        if ($this->customEagerMatchingCallbacks) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->customEagerMatchingCallbacks of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
50 2
            foreach ($this->customEagerMatchingCallbacks as $callback) {
51 2
                $models = $callback($models, $results, $relation, 'one');
52
            }
53
54 2
            return $models;
55
        }
56
57 4
        $dictionary = $this->buildDictionary($results);
58
59 4
        foreach ($models as $model) {
60 4
            if (isset($dictionary[$key = $model->getAttribute($this->localKey)])) {
61 4
                $model->setRelation(
62 4
                    $relation,
63 4
                    reset($dictionary[$key])
64 4
                );
65
            }
66
        }
67
68 4
        return $models;
69
    }
70
71
    /**
72
     * Make a new related instance for the given model.
73
     *
74
     * @param \Illuminate\Database\Eloquent\Model $parent
75
     * @return \Illuminate\Database\Eloquent\Model
76
     */
77 6
    public function newRelatedInstanceFor(Model $parent)
0 ignored issues
show
Unused Code introduced by
The parameter $parent is not used and could be removed. ( Ignorable by Annotation )

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

77
    public function newRelatedInstanceFor(/** @scrutinizer ignore-unused */ Model $parent)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
78
    {
79 6
        return $this->related->newInstance();
80
    }
81
}
82