TrainingUser   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 77
Duplicated Lines 10.39 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 1
dl 8
loc 77
ccs 0
cts 34
cp 0
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A user() 0 4 1
A author() 0 4 1
A training() 0 4 1
A attachments() 0 4 1
A scopeActiveUsers() 0 6 1
A setAttribute() 8 8 3
A scopeRenewableTrainings() 0 6 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
2
3
namespace SET;
4
5
use Illuminate\Database\Eloquent\Model;
6
7
class TrainingUser extends Model
8
{
9
    /**
10
     * @var string
11
     */
12
    protected $table = 'training_user';
13
    /**
14
     * @var bool
15
     */
16
    public $timestamps = true;
17
18
    /**
19
     * @var array
20
     */
21
    protected $fillable = ['training_id', 'due_date', 'completed_date', 'comment', 'author_id', 'user_id', 'stop_renewal'];
22
23
    public function user()
24
    {
25
        return $this->belongsTo('SET\User');
26
    }
27
28
    public function author()
29
    {
30
        return $this->belongsTo('SET\User', 'author_id');
31
    }
32
33
    public function training()
34
    {
35
        return $this->belongsTo('SET\Training');
36
    }
37
38
    public function attachments()
39
    {
40
        return $this->morphMany('SET\Attachment', 'imageable');
41
    }
42
43
    /**
44
     * Allows you to call activeUsers() on notes
45
     * to return only notes with active users.
46
     *
47
     * @param $query
48
     *
49
     * @return mixed
50
     */
51
    public function scopeActiveUsers($query)
52
    {
53
        return $query->whereHas('user', function ($q) {
54
            $q->where('status', 'active');
55
        });
56
    }
57
58
    /**
59
     * @param string $key
60
     * @param mixed  $value
61
     *
62
     * @return $this
63
     */
64 View Code Duplication
    public function setAttribute($key, $value)
65
    {
66
        if (is_scalar($value) && $value === '') {
67
            $value = null;
68
        }
69
70
        return parent::setAttribute($key, $value);
71
    }
72
73
    /**
74
     * @param $query
75
     * @param $input
76
     */
77
    public function scopeRenewableTrainings($query)
78
    {
79
        return $query->whereHas('training', function ($q) {
80
            $q->where('renews_in', '>', 0);
81
        });
82
    }
83
}
84