| 1 |  |  | <?php
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace App\Lib\DsManager\Models;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use App\Lib\DsManager\Models\Common\DsManagerModel;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | /**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * Class Team
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @package App\Lib\DsManager\Models
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class Team extends DsManagerModel
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | 	/**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | 	 * @var
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	 */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	public $name;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | 	/**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | 	 * @var
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | 	 */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | 	public $coach;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | 	/**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | 	 * @var
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | 	 */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | 	public $roster;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	/**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 * @var
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	public $nationality;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	/**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 * @return array
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	public function toArray()
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	{
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 		$result = [];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 		$result['name'] = $this->name;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 		$result['nationality'] = $this->nationality;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 		$result['coach'] = $this->coach->toArray();
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 		$roster = [];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  | 		foreach ($this->roster as $player) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 			$roster[] = $player->toArray();
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 		}
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		$result['roster'] = $roster;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 		return $result;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	}
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	/**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	 * @return string
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 	 */
 | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 51 |  | View Code Duplication | 	public function getAvgSkill()
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	{
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  | 		$c = 0;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		$tot = 0;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 		foreach ($this->roster as $player) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 			$tot += $player->skillAvg;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			$c++;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		}
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 		return bcdiv($tot, $c, 2);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	}
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 	/**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 	 * @return string
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  | 	 */
 | 
            
                                                                                                            
                            
            
                                                                    
                                                                                                        
            
            
                | 66 |  | View Code Duplication | 	public function getAvgAge()
 | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 	{
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  | 		$c = 0;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		$tot = 0;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  | 		foreach ($this->roster as $player) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 			$tot += $player->age;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 			$c++;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 		}
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  | 		return bcdiv($tot, $c, 2);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 	}
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	/**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	 * @param $role
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	 * @return array
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 81 |  |  | 	 */
 | 
            
                                                                        
                            
            
                                    
            
            
                | 82 |  |  | 	public function getPlayersForRole($role)
 | 
            
                                                                        
                            
            
                                    
            
            
                | 83 |  |  | 	{
 | 
            
                                                                        
                            
            
                                    
            
            
                | 84 |  |  | 		$result = [];
 | 
            
                                                                        
                            
            
                                    
            
            
                | 85 |  |  | 		foreach ($this->roster as $player) {
 | 
            
                                                                        
                            
            
                                    
            
            
                | 86 |  |  | 			if ($player->role == $role) {
 | 
            
                                                                        
                            
            
                                    
            
            
                | 87 |  |  | 				$result[] = $player;
 | 
            
                                                                        
                            
            
                                    
            
            
                | 88 |  |  | 			}
 | 
            
                                                                        
                            
            
                                    
            
            
                | 89 |  |  | 		}
 | 
            
                                                                        
                            
            
                                    
            
            
                | 90 |  |  | 		return $result;
 | 
            
                                                                        
                            
            
                                    
            
            
                | 91 |  |  | 	}
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  | 	/**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	 * @param $role
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  | 	 * @return null
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	 */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	public function getBestPlayerForRole($role)
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	{
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 		$players = $this->getPlayersForRole($role);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  | 		$maxSkill = 0;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  | 		$index = -1;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 		$i = 0;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  | 		foreach ($players as $player) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 			if ($player->skillAvg > $maxSkill) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 				$index = $i;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 				$maxSkill = $player->skillAvg;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  | 			}
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  | 			$i++;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  | 		}
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  | 		if ($index === -1) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  | 			return null;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  | 		}
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  | 		return $players[$index];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  | 	}
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  | 	/**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  | 	 * @return array
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  | 	 */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  | 	public function playersPerRoleArray()
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  | 	{
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  | 		$result = [];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  | 		foreach ($this->roster as $player) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  | 			$result[$player->role] = isset($result[$player->role]) ? $result[$player->role] + 1 : 1;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  | 		}
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  | 		return $result;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  | 	}
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	/**
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	 * @param array $array
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	 * @return mixed
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	 */
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  | 	public static function fromArray($array = [])
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  | 	{
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  | 		$roster = $array['roster'];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  | 		$coach = $array['coach'];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  | 		unset($array['roster']);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 		unset($array['coach']);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		$team = parent::fromArray($array);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  | 
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  | 		$team->coach = Coach::fromArray($coach);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  | 		$players = [];
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  | 		foreach ($roster as $roasterP) {
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  | 			$players[] = Player::fromArray($roasterP);
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  | 		}
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  | 		$team->roster = $players;
 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  | 		return $team;
 | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 149 |  |  | 	}
 | 
            
                                                        
            
                                    
            
            
                | 150 |  |  | } | 
            
                        
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.