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 |
||
9 | class Stats extends Client |
||
10 | { |
||
11 | 6 | public function __construct($steamId) |
|
17 | |||
18 | /** |
||
19 | * @deprecated |
||
20 | * |
||
21 | * @param $appId |
||
22 | * |
||
23 | * @return array |
||
24 | */ |
||
25 | public function GetPlayerAchievementsAPI($appId) |
||
52 | |||
53 | 2 | public function GetPlayerAchievements($appId) |
|
99 | |||
100 | 1 | View Code Duplication | public function GetGlobalAchievementPercentagesForApp($gameId) |
101 | { |
||
102 | // Set up the api details |
||
103 | 1 | $this->method = __FUNCTION__; |
|
104 | 1 | $this->version = 'v0002'; |
|
105 | |||
106 | // Set up the arguments |
||
107 | $arguments = [ |
||
108 | 1 | 'gameid' => $gameId, |
|
109 | 1 | 'l' => 'english', |
|
110 | ]; |
||
111 | |||
112 | // Get the client |
||
113 | 1 | $client = $this->setUpClient($arguments)->achievementpercentages; |
|
114 | |||
115 | 1 | return $client->achievements; |
|
116 | } |
||
117 | |||
118 | /** |
||
119 | * @param $appId int Steam 64 id |
||
120 | * @param $all bool Return all stats when true and only achievements when false |
||
121 | * |
||
122 | * @return mixed |
||
123 | */ |
||
124 | 2 | public function GetUserStatsForGame($appId, $all = false) |
|
147 | |||
148 | /** |
||
149 | * @param $appId |
||
150 | * |
||
151 | * @link https://wiki.teamfortress.com/wiki/WebAPI/GetSchemaForGame |
||
152 | * |
||
153 | * @return mixed |
||
154 | */ |
||
155 | 1 | View Code Duplication | public function GetSchemaForGame($appId) |
156 | { |
||
157 | // Set up the api details |
||
158 | 1 | $this->method = __FUNCTION__; |
|
159 | 1 | $this->version = 'v0002'; |
|
160 | |||
161 | // Set up the arguments |
||
162 | $arguments = [ |
||
163 | 1 | 'appid' => $appId, |
|
164 | 1 | 'l' => 'english', |
|
165 | ]; |
||
166 | |||
167 | // Get the client |
||
168 | 1 | return $this->setUpClient($arguments); |
|
169 | } |
||
170 | |||
171 | 1 | protected function convertToObjects($achievements) |
|
181 | } |
||
182 |
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.