Passed
Push — next ( 230762...ff9bff )
by Bas
08:27
created

HasOneOrMany   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 10
c 1
b 0
f 0
dl 0
loc 38
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
    public function firstOrCreate(array $attributes = [], array $values = [])
17
    {
18
        /** @phpstan-ignore-next-line */
19
        $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
        if (is_null($instance)) {
21
            $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
        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
    public function firstOrNew(array $attributes = [], array $values = [])
35
    {
36
        /** @phpstan-ignore-next-line */
37
        $instance = $this->where(associativeFlatten($attributes))->first();
38
        if (is_null($instance)) {
39
            $instance = $this->related->newInstance(array_merge($attributes, $values));
40
41
            $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
        return $instance;
45
    }
46
}
47