Completed
Pull Request — master (#116)
by
unknown
02:19
created

Location   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 107
Duplicated Lines 13.08 %

Coupling/Cohesion

Components 0
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 7
c 0
b 0
f 0
lcom 0
cbo 4
dl 14
loc 107
ccs 0
cts 14
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A devices() 0 4 1
A scopeBySite() 0 4 1
A site() 0 4 1
A getUpdatedAtHumanAttribute() 7 7 2
A getCreatedAtHumanAttribute() 7 7 2

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 App;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Spatie\Activitylog\Traits\LogsActivity;
7
use Illuminate\Support\Facades\Auth;
8
9
class Location extends Model
10
{
11
    use LogsActivity;
12
    
13
    /**
14
     * The attributes that are mass assignable.
15
     *
16
     * @var array
17
     */
18
    protected $fillable = [
19
        'name', 'site_id'
20
    ];
21
    
22
    /**
23
     * The attributes to ignore in the Activity Log
24
     *
25
     * @var array
26
     */
27
    protected static $ignoreChangedAttributes = ['updated_at'];
28
    
29
    /**
30
     * The attributes to log in the Activity Log
31
     *
32
     * @var array
33
     */
34
    protected static $logAttributes = [
35
        'name', 'site_id'
36
    ];
37
    
38
    /**
39
     * Only log those that have actually changed after the update.
40
     *
41
     * @var array
42
     */
43
    protected static $logOnlyDirty = true;
44
    
45
    /**
46
     * Update the updated_at and created_at timestamps?
47
     *
48
     * @var array
49
     */
50
    public $timestamps = true;
51
    
52
    /**
53
     * Get the site for the location
54
     */
55
    public function site()
56
    {
57
        return $this->belongsTo('App\Site', 'site_id');
58
    }
59
    
60
    /**
61
     * Get devices for the location
62
     */
63
    public function devices()
64
    {
65
        return $this->hasMany('App\Device');
66
    }
67
    
68
    /**
69
     * Scope a query to only include locations belonging to a given site
70
     *
71
     * @param \Illuminate\Database\Eloquent\Builder $query
72
     * @param int $site_id
73
     * @return \Illuminate\Database\Eloquent\Builder
74
     */
75
    public function scopeBySite($query, $site_id)
76
    {
77
        return $query->where('site_id', $site_id);
78
    }
79
    
80
    /**
81
     * Accessor: Get the location's last update time in seconds/minutes/hours since update or converted to user
82
     * friendly readable format.
83
     * If the time is less then a day old then display time since it last updated
84
     * If the time is greater then a day old then display the time in the format of Month day, year 12hour:mins am/pm
85
     * and using the user's preferred timezone
86
     *
87
     *
88
     * @return string
89
     */
90 View Code Duplication
    public function getUpdatedAtHumanAttribute()
91
    {
92
        if ($this->updated_at->diffInDays() > 0)
93
            return $this->updated_at->setTimezone(Auth::user()->timezone)->format('M d, Y h:i a');
0 ignored issues
show
Bug introduced by
Accessing timezone on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
94
        else
95
            return $this->updated_at->diffForHumans();
96
    }
97
    
98
    /**
99
     * Accessor: Get the location's creation time in seconds/minutes/hours since update or converted to user
100
     * friendly readable format.
101
     * If the time is less then a day old then display time since creation
102
     * If the time is greater then a day old then display the time in the format of Month day, year 12hour:mins am/pm
103
     * and using the user's preferred timezone
104
     *
105
     *
106
     * @return string
107
     */
108 View Code Duplication
    public function getCreatedAtHumanAttribute()
109
    {
110
        if ($this->created_at->diffInDays() > 0)
111
            return $this->created_at->setTimezone(Auth::user()->timezone)->format('M d, Y h:i a');
0 ignored issues
show
Bug introduced by
Accessing timezone on the interface Illuminate\Contracts\Auth\Authenticatable suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
112
        else
113
            return $this->created_at->diffForHumans();
114
    }
115
}
116