Completed
Push — master ( 3a0efc...84f561 )
by Vincenzo
02:28
created

Player   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 104
Duplicated Lines 13.46 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 8
Bugs 0 Features 4
Metric Value
wmc 7
c 8
b 0
f 4
lcom 1
cbo 1
dl 14
loc 104
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() 14 14 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
     * @var string
14
     */
15
    protected $table = 'players';
16
17
    /**
18
     * @var array
19
     */
20
    protected $fillable = [
21
        'name',
22
        'surname',
23
        'age',
24
        'nationality',
25
        'skillAvg',
26
        'wageReq',
27
        'val',
28
        'role',
29
        'team_id'
30
    ];
31
32
    /**
33
     * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
34
     */
35
    public function team()
36
    {
37
        return $this->belongsTo(Team::class);
38
    }
39
40
    /**
41
     * @return mixed
42
     */
43
    public function lastMatches()
44
    {
45
        return $this->hasMany(MatchPlayer::class)
46
            ->orderBy('updated_at', 'DESC')
47
            ->limit(5);
48
    }
49
50
    /**
51
     * @return mixed
52
     */
53
    public function goals()
54
    {
55
        return $this->hasOne(MatchPlayer::class)
56
            ->selectRaw('player_id, sum(goals) as count')
57
            ->groupBy('player_id');
58
    }
59
60
    /**
61
     * @return mixed
62
     */
63
    public function appearances()
64
    {
65
        return $this->hasOne(MatchPlayer::class)
66
            ->selectRaw('player_id, count(match_id) as count')
67
            ->groupBy('player_id');
68
    }
69
70
    /**
71
     * @return mixed
72
     */
73
    public function avg()
74
    {
75
        return $this->hasOne(MatchPlayer::class)
76
            ->selectRaw('player_id, round(avg(vote),2) as avg')
77
            ->groupBy('player_id');
78
    }
79
80
    /**
81
     * @param $query
82
     * @return mixed
83
     */
84
    public function scopeStatistics($query)
85
    {
86
        return $query->with(
87
            'goals',
88
            'appearances',
89
            'avg',
90
            'lastMatches',
91
            'team'
92
        );
93
    }
94
95
    /**
96
     * @return array
97
     */
98 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...
99
    {
100
        $result = MatchPlayer::selectRaw(
101
            'player_id, COUNT(*) as appearances ,AVG(vote) avg, SUM(goals) goals'
102
        )->where('goals', '>', 0)
103
            ->orderByRaw('SUM(goals) DESC,COUNT(*) DESC')
104
            ->groupBy('player_id')->take(20)->get()->keyBy('player_id')->toArray();
105
        $players = Player::whereIn('id', array_keys($result))->get()->toArray();
106
        $result = array_map(function ($player) use ($result) {
107
            $player['stats'] = $result[$player['id']];
108
            return $player;
109
        }, $players);
110
        return $result;
111
    }
112
}