Player   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 108
Duplicated Lines 12.04 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 10
Bugs 1 Features 5
Metric Value
wmc 7
c 10
b 1
f 5
lcom 1
cbo 1
dl 13
loc 108
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A team() 0 4 1
A lastMatches() 0 6 1
A goals() 0 6 1
A appearances() 0 6 1
A avg() 0 6 1
A scopeStatistics() 0 10 1
A getBest() 13 13 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 App\Lib\DsManager\Models\Orm;
4
5
/**
6
 * Class Player
7
 * @package App\Lib\DsManager\Models\Orm
8
 */
9
class Player extends DsManagerOrm
10
{
11
    /**
12
     *
13
     */
14
    const PLAYER_STATS_LIMIT = 5;
15
16
17
    /**
18
     * @var string
19
     */
20
    protected $table = 'players';
21
22
    /**
23
     * @var array
24
     */
25
    protected $fillable = [
26
        'name',
27
        'surname',
28
        'age',
29
        'nationality',
30
        'skillAvg',
31
        'wageReq',
32
        'val',
33
        'role',
34
        'team_id'
35
    ];
36
37
    /**
38
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
39
     */
40
    public function team()
41
    {
42
        return $this->belongsTo(Team::class);
43
    }
44
45
    /**
46
     * @return mixed
47
     */
48
    public function lastMatches()
49
    {
50
        return $this->hasMany(MatchPlayer::class)
51
            ->orderBy('updated_at', 'DESC')
52
            ->limit(5);
53
    }
54
55
    /**
56
     * @return mixed
57
     */
58
    public function goals()
59
    {
60
        return $this->hasOne(MatchPlayer::class)
61
            ->selectRaw('player_id, sum(goals) as count')
62
            ->groupBy('player_id');
63
    }
64
65
    /**
66
     * @return mixed
67
     */
68
    public function appearances()
69
    {
70
        return $this->hasOne(MatchPlayer::class)
71
            ->selectRaw('player_id, count(match_id) as count')
72
            ->groupBy('player_id');
73
    }
74
75
    /**
76
     * @return mixed
77
     */
78
    public function avg()
79
    {
80
        return $this->hasOne(MatchPlayer::class)
81
            ->selectRaw('player_id, round(avg(vote),2) as avg')
82
            ->groupBy('player_id');
83
    }
84
85
    /**
86
     * @param $query
87
     * @return mixed
88
     */
89
    public function scopeStatistics($query)
90
    {
91
        return $query->with(
92
            'goals',
93
            'appearances',
94
            'avg',
95
            'lastMatches',
96
            'team'
97
        );
98
    }
99
100
    /**
101
     * @return array
102
     */
103 View Code Duplication
    public static function getBest()
0 ignored issues
show
Duplication introduced by
This method 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...
104
    {
105
        $result = MatchPlayer::selectRaw(
106
            'player_id, COUNT(*) as appearances , AVG(vote) avg, SUM(goals) goals'
107
        )->where('goals', '>', 0)
108
            ->orderByRaw('SUM(goals) DESC,COUNT(*) DESC')
109
            ->groupBy('player_id')->take(self::PLAYER_STATS_LIMIT)->get()->keyBy('player_id')->toArray();
110
        $players = Player::with('team')->whereIn('id', array_keys($result))->get()->toArray();
111
        $result = array_map(function ($player) use ($result) {
112
            return array_merge($player,$result[$player['id']]);
113
        }, $players);
114
        return $result;
115
    }
116
}