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:
Complex classes like FlipsideAPIUser often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FlipsideAPIUser, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 8 | class FlipsideAPIUser extends User |
||
| 9 | { |
||
| 10 | private $userData; |
||
| 11 | private $groupData = null; |
||
| 12 | |||
| 13 | public function __construct($data = false) |
||
| 14 | { |
||
| 15 | View Code Duplication | if(($data !== false) && !isset($data['extended'])) |
|
| 16 | { |
||
| 17 | //Generic user object |
||
| 18 | //TODO get from API |
||
| 19 | } |
||
| 20 | else |
||
| 21 | { |
||
| 22 | if(isset($data['extended'])) |
||
| 23 | { |
||
| 24 | $this->userData = $data['extended']; |
||
| 25 | } |
||
| 26 | } |
||
| 27 | } |
||
| 28 | |||
| 29 | public function isInGroupNamed($name) |
||
| 30 | { |
||
| 31 | if($this->groupData === null) |
||
| 32 | { |
||
| 33 | $resp = \Httpful\Request::get('https://profiles.test.burningflipside.com/api/v1/users/me/groups')->authenticateWith($this->userData->uid, $this->userData->userPassword)->send(); |
||
| 34 | if($resp->hasErrors()) |
||
| 35 | { |
||
| 36 | return false; |
||
| 37 | } |
||
| 38 | $this->groupData = $resp->body; |
||
| 39 | } |
||
| 40 | $count = count($this->groupData); |
||
| 41 | for($i = 0; $i < $count; $i++) |
||
| 42 | { |
||
| 43 | if($this->groupData[$i]->cn === $name) |
||
| 44 | { |
||
| 45 | return true; |
||
| 46 | } |
||
| 47 | } |
||
| 48 | return false; |
||
| 49 | } |
||
| 50 | |||
| 51 | public function getDisplayName() |
||
| 59 | |||
| 60 | public function getGivenName() |
||
| 68 | |||
| 69 | public function getEmail() |
||
| 77 | |||
| 78 | public function getUid() |
||
| 86 | |||
| 87 | public function getPhoneNumber() |
||
| 95 | |||
| 96 | public function getOrganization() |
||
| 104 | |||
| 105 | public function getTitles() |
||
| 113 | |||
| 114 | public function getState() |
||
| 122 | |||
| 123 | public function getCity() |
||
| 131 | |||
| 132 | public function getLastName() |
||
| 140 | |||
| 141 | public function getNickName() |
||
| 149 | |||
| 150 | public function getAddress() |
||
| 158 | |||
| 159 | public function getPostalCode() |
||
| 167 | |||
| 168 | public function getCountry() |
||
| 176 | |||
| 177 | public function getOrganizationUnits() |
||
| 185 | |||
| 186 | public function getLoginProviders() |
||
| 194 | } |
||
| 195 | |||
| 196 |
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.