Tag   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 29
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A hasType() 0 4 1
A findOrCreate() 0 14 2
1
<?php
2
3
namespace App\Models;
4
5
use Spatie\Tags\Tag as SpatieTag;
6
use App\Models\Presenters\TagPresenter;
7
use Spatie\Blender\Model\Traits\Draftable;
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;
0 ignored issues
show
Documentation introduced by
The property type does not exist on object<App\Models\Tag>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
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