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 |
||
| 8 | class FlipsideAPIUser extends User |
||
| 9 | { |
||
| 10 | private $userData; |
||
| 11 | private $groupData = null; |
||
| 12 | private $apiUrl = 'https://profiles.burningflipside.com/api/v1'; |
||
| 13 | |||
| 14 | public function __construct($data = false, $apiUrl = false) |
||
| 15 | { |
||
| 16 | View Code Duplication | if(($data !== false) && !isset($data['extended'])) |
|
| 17 | { |
||
| 18 | //Generic user object |
||
| 19 | //TODO get from API |
||
| 20 | } |
||
| 21 | else |
||
| 22 | { |
||
| 23 | if(isset($data['extended'])) |
||
| 24 | { |
||
| 25 | $this->userData = $data['extended']; |
||
| 26 | } |
||
| 27 | } |
||
| 28 | if($apiUrl !== false) |
||
| 29 | { |
||
| 30 | $this->apiUrl = $apiUrl; |
||
| 31 | } |
||
| 32 | } |
||
| 33 | |||
| 34 | public function isInGroupNamed($name) |
||
| 55 | |||
| 56 | public function __get($propName) |
||
| 65 | |||
| 66 | public function __set($propName, $value) |
||
| 69 | } |
||
| 70 | |||
| 71 |
This check looks for the bodies of
ifstatements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.These
ifbodies can be removed. If you have an empty if but statements in theelsebranch, consider inverting the condition.could be turned into
This is much more concise to read.