Completed
Pull Request — master (#88)
by D.
02:51
created

TrainingUser   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 66
Duplicated Lines 12.12 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
c 0
b 0
f 0
lcom 1
cbo 1
dl 8
loc 66
ccs 15
cts 15
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A user() 0 4 1
A training() 0 4 1
A attachments() 0 4 1
A scopeActiveUsers() 0 6 1
A setAttribute() 8 8 3
A author() 0 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
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 62
    public function user()
24
    {
25 62
        return $this->belongsTo('SET\User');
26
    }
27
28 1
    public function author()
29
    {
30 1
        return $this->belongsTo('SET\User', 'author_id');
31
    }
32
33 14
    public function training()
34
    {
35 14
        return $this->belongsTo('SET\Training');
36
    }
37
38 3
    public function attachments()
39
    {
40 3
        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 61
        return $query->whereHas('user', function ($q) {
54 61
            $q->where('status', 'active');
55 61
        });
56
    }
57
58
    /**
59
     * @param string $key
60
     * @param mixed  $value
61
     *
62
     * @return $this
63
     */
64 14 View Code Duplication
    public function setAttribute($key, $value)
65
    {
66 14
        if (is_scalar($value) && $value === '') {
67 2
            $value = null;
68
        }
69
70 14
        return parent::setAttribute($key, $value);
71
    }
72
}
73