Passed
Push — experiment/id-key-conversion ( cdb360...206c4a )
by Bas
11:25 queued 15s
created

HasOneOrMany   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 4
eloc 10
c 2
b 0
f 0
dl 0
loc 38
ccs 11
cts 11
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A firstOrNew() 0 11 2
A firstOrCreate() 0 9 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Eloquent\Relations\Concerns;
6
7
trait HasOneOrMany
8
{
9
    /**
10
     * Get the first related record matching the attributes or create it.
11
     *
12
     * @param  array  $attributes
13
     * @param  array  $values
14
     * @return \Illuminate\Database\Eloquent\Model
15
     */
16 1
    public function firstOrCreate(array $attributes = [], array $values = [])
17
    {
18
        /** @phpstan-ignore-next-line */
19 1
        $instance = $this->where(associativeFlatten($attributes))->first();
0 ignored issues
show
Bug introduced by
It seems like where() 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

19
        $instance = $this->/** @scrutinizer ignore-call */ where(associativeFlatten($attributes))->first();
Loading history...
20 1
        if (is_null($instance)) {
21 1
            $instance = $this->create(array_merge($attributes, $values));
0 ignored issues
show
Bug introduced by
It seems like create() 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

21
            /** @scrutinizer ignore-call */ 
22
            $instance = $this->create(array_merge($attributes, $values));
Loading history...
22
        }
23
24 1
        return $instance;
25
    }
26
27
    /**
28
     * Get the first related model record matching the attributes or instantiate it.
29
     *
30
     * @param  array  $attributes
31
     * @param  array  $values
32
     * @return \Illuminate\Database\Eloquent\Model
33
     */
34 1
    public function firstOrNew(array $attributes = [], array $values = [])
35
    {
36
        /** @phpstan-ignore-next-line */
37 1
        $instance = $this->where(associativeFlatten($attributes))->first();
38 1
        if (is_null($instance)) {
39 1
            $instance = $this->related->newInstance(array_merge($attributes, $values));
40
41 1
            $this->setForeignAttributesForCreate($instance);
0 ignored issues
show
Bug introduced by
It seems like setForeignAttributesForCreate() 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

41
            $this->/** @scrutinizer ignore-call */ 
42
                   setForeignAttributesForCreate($instance);
Loading history...
42
        }
43
44 1
        return $instance;
45
    }
46
}
47