Passed
Push — main ( a267a2...e9480d )
by Jonas
12:42 queued 09:49
created

HasOneJson   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 26
c 1
b 0
f 0
dl 0
loc 61
rs 10
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getResults() 0 7 3
A initRelation() 0 7 2
A newRelatedInstanceFor() 0 3 1
A match() 0 3 1
A matchOne() 0 24 4
1
<?php
2
3
namespace Staudenmeir\EloquentJsonRelations\Relations;
4
5
use Illuminate\Database\Eloquent\Collection;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Relations\Concerns\SupportsDefaultModels;
8
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
9
10
class HasOneJson extends HasManyJson
11
{
12
    use SupportsDefaultModels;
13
14
    /** @inheritDoc */
15
    public function getResults()
16
    {
17
        if (is_null($this->getParentKey())) {
18
            return $this->getDefaultFor($this->parent);
19
        }
20
21
        return $this->first() ?: $this->getDefaultFor($this->parent);
22
    }
23
24
    /** @inheritDoc */
25
    public function initRelation(array $models, $relation)
26
    {
27
        foreach ($models as $model) {
28
            $model->setRelation($relation, $this->getDefaultFor($model));
29
        }
30
31
        return $models;
32
    }
33
34
    /** @inheritDoc */
35
    public function match(array $models, Collection $results, $relation)
36
    {
37
        return $this->matchOne($models, $results, $relation);
38
    }
39
40
    /** @inheritDoc */
41
    public function matchOne(array $models, Collection $results, $relation)
42
    {
43
        if ($this->hasCompositeKey()) {
44
            $this->matchWithCompositeKey($models, $results, $relation, 'one');
45
        } else {
46
            HasOneOrMany::matchOneOrMany($models, $results, $relation, 'one');
47
        }
48
49
        if ($this->key) {
50
            foreach ($models as $model) {
51
                $model->setRelation(
52
                    $relation,
53
                    $this->hydratePivotRelation(
54
                        new Collection(
55
                            array_filter([$model->$relation])
0 ignored issues
show
Bug introduced by
array_filter(array($model->$relation)) of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Database\Eloq...llection::__construct(). ( Ignorable by Annotation )

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

55
                            /** @scrutinizer ignore-type */ array_filter([$model->$relation])
Loading history...
56
                        ),
57
                        $model,
58
                        fn (Model $model) => $model->{$this->getPathName()}
59
                    )->first()
60
                );
61
            }
62
        }
63
64
        return $models;
65
    }
66
67
    /** @inheritDoc */
68
    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

68
    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...
69
    {
70
        return $this->related->newInstance();
71
    }
72
}
73