| 1 |  |  | <?php
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace App\Lib\DsManager\Models\Orm;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * Class Team
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @package App\Lib\DsManager\Models\Orm
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | class Team extends DsManagerOrm
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |      *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |     const PLAYED_LIMIT = 5;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      *
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     const FUTURE_LIMIT = 3;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |      * @var string
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     protected $table = 'teams';
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @var array
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     protected $fillable = [
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |         'name',
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |         'nationality'
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     ];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @return \Illuminate\Database\Eloquent\Relations\HasMany
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function roster()
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         return $this->hasMany(Player::class);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @return \Illuminate\Database\Eloquent\Relations\HasMany
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     public function coach()
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         return $this->hasOne(Coach::class);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @return mixed
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 51 |  | View Code Duplication |     public function playedMatchesHome()
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         return $this->hasMany(
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             MatchResult::class,
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |             'home_team_id'
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         )->where('simulated', true)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             ->orderBy('updated_at', 'DESC')
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |             ->limit(self::PLAYED_LIMIT);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * @return mixed
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 64 |  | View Code Duplication |     public function futureMatchesHome()
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         return $this->hasMany(
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             MatchResult::class,
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             'home_team_id'
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         )->where('simulated', false)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             ->orderBy('updated_at', 'DESC')
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |             ->limit(self::FUTURE_LIMIT);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @return mixed
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 77 |  | View Code Duplication |     public function playedMatchesAway()
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |         return $this->hasMany(
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |             MatchResult::class,
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |             'away_team_id'
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |         )->where('simulated', true)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             ->orderBy('updated_at', 'DESC')
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |             ->limit(self::PLAYED_LIMIT);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * @return mixed
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 90 |  | View Code Duplication |     public function futureMatchesAway()
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         return $this->hasMany(
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             MatchResult::class,
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |             'away_team_id'
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         )->where('simulated', false)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |             ->orderBy('updated_at', 'DESC')
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             ->limit(self::FUTURE_LIMIT);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |      * @param $query
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |      * @return mixed
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |      */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     public function scopeComplete($query)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |     {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |         return $query->with(
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |             'roster',
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |             'coach',
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             'playedMatchesHome',
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             'playedMatchesHome.awayTeam',
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             'futureMatchesHome',
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |             'futureMatchesHome.awayTeam',
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |             'playedMatchesAway',
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |             'playedMatchesAway.homeTeam',
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |             'futureMatchesAway',
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |             'futureMatchesAway.homeTeam'
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         );
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     }
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |     /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @return array
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 123 |  |  |      */
 | 
            
                                                        
            
                                                                    
                                                                                                        
            
            
                | 124 |  | View Code Duplication |     public static function getBest()
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 125 |  |  |     {
 | 
            
                                                        
            
                                    
            
            
                | 126 |  |  |         $result = Match::selectRaw('winner_id as id, COUNT(*) as won')
 | 
            
                                                        
            
                                    
            
            
                | 127 |  |  |             ->whereNotNull('winner_id')->where('winner_id', '!=', 0)
 | 
            
                                                        
            
                                    
            
            
                | 128 |  |  |             ->orderByRaw('COUNT(*) DESC')->groupBy('winner_id')
 | 
            
                                                        
            
                                    
            
            
                | 129 |  |  |             ->take(20)->get()->keyBy('id')->toArray();
 | 
            
                                                        
            
                                    
            
            
                | 130 |  |  |         $teams = Team::whereIn('id', array_keys($result))->get()->toArray();
 | 
            
                                                        
            
                                    
            
            
                | 131 |  |  |         $result = array_map(function ($team) use ($result) {
 | 
            
                                                        
            
                                    
            
            
                | 132 |  |  |             $team['stats'] = $result[$team['id']];
 | 
            
                                                        
            
                                    
            
            
                | 133 |  |  |             return $team;
 | 
            
                                                        
            
                                    
            
            
                | 134 |  |  |         }, $teams);
 | 
            
                                                        
            
                                    
            
            
                | 135 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 136 |  |  |         return $result;
 | 
            
                                                        
            
                                    
            
            
                | 137 |  |  |     }
 | 
            
                                                        
            
                                    
            
            
                | 138 |  |  | 
 | 
            
                                                        
            
                                    
            
            
                | 139 |  |  | } | 
            
                        
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.