Completed
Push — master ( 3f89d7...f790e9 )
by Mike
30:28 queued 29:00
created

AuthorWithInlineGlobalScope   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 57
loc 57
c 0
b 0
f 0
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A boot() 8 8 1
A books() 4 4 1
A printers() 4 4 1
A profile() 4 4 1
A getLatestBookAttribute() 7 7 1
A scopeStartsWithA() 4 4 1
A scopeNameStartsWith() 4 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php namespace GeneaLabs\LaravelModelCaching\Tests\Fixtures;
2
3
use GeneaLabs\LaravelModelCaching\Traits\Cachable;
4
use Illuminate\Database\Eloquent\Builder;
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Relations\HasMany;
7
use Illuminate\Database\Eloquent\Relations\HasOne;
8
use Illuminate\Database\Eloquent\SoftDeletes;
9
use GeneaLabs\LaravelModelCaching\CachedBuilder;
10
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
11
12 View Code Duplication
class AuthorWithInlineGlobalScope extends Model
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
{
14
    use Cachable;
15
    use SoftDeletes;
16
17
    protected $casts = [
18
        "finances" => "array",
19
    ];
20
    protected $fillable = [
21
        'name',
22
        'email',
23
        "finances",
24
    ];
25
    protected $table = "authors";
26
27
    protected static function boot()
28
    {
29
        parent::boot();
30
31
        static::addGlobalScope('inlineScope', function (CachedBuilder $builder) {
32
            return $builder->where('name', 'LIKE', "A%");
33
        });
34
    }
35
36
    public function books() : HasMany
37
    {
38
        return $this->hasMany(Book::class);
39
    }
40
41
    public function printers() : HasManyThrough
42
    {
43
        return $this->hasManyThrough(Printer::class, Book::class);
44
    }
45
46
    public function profile() : HasOne
47
    {
48
        return $this->hasOne(Profile::class);
49
    }
50
51
    public function getLatestBookAttribute()
52
    {
53
        return $this
54
            ->books()
55
            ->latest("id")
56
            ->first();
57
    }
58
59
    public function scopeStartsWithA(Builder $query) : Builder
60
    {
61
        return $query->where('name', 'LIKE', 'A%');
62
    }
63
64
    public function scopeNameStartsWith(Builder $query, string $startOfName) : Builder
65
    {
66
        return $query->where("name", "LIKE", "{$startOfName}%");
67
    }
68
}
69