Completed
Push — master ( d6d863...730950 )
by Freek
22:10 queued 19:06
created

Tag::findByNameOrCreate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 2
nop 2
dl 0
loc 23
rs 9.0856
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use App\Models\Presenters\TagPresenter;
6
use Spatie\Blender\Model\Traits\Draftable;
7
use Spatie\Tags\Tag as SpatieTag;
8
9
class Tag extends SpatieTag
10
{
11
    use TagPresenter, Draftable;
12
13
    /**
14
     * @param string $type
15
     *
16
     * @return bool
17
     */
18
    public function hasType(string $type): bool
19
    {
20
        return $this->type === $type;
21
    }
22
23
    public static function findOrCreate($name, string $type = null, string $locale = null): Tag
24
    {
25
        if ($existingTag = parent::findFromString($name, $type)) {
0 ignored issues
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (findFromString() instead of findOrCreate()). Are you sure this is correct? If so, you might want to change this to $this->findFromString().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
26
            return $existingTag;
27
        }
28
29
        $tag = parent::findOrCreate($name, $type);
30
31
        $tag->setTranslations('name', array_fill_keys(config('app.locales'), $name));
32
33
        $tag->save();
34
35
        return $tag;
36
    }
37
}
38