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 |
||
| 4 | class SQLUser extends User |
||
| 5 | { |
||
| 6 | private $data; |
||
| 7 | private $auth; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Initialize a SQLUser object |
||
| 11 | * |
||
| 12 | * @param boolean|array $data The data to initialize the SQLUser with or false for an empty User |
||
| 13 | * @param boolean|\Auth\SQLAuthenticator The SQLAuthenticator instance that produced this user |
||
| 14 | */ |
||
| 15 | public function __construct($data = false, $auth = false) |
||
| 16 | { |
||
| 17 | $this->data = array(); |
||
| 18 | $this->auth = $auth; |
||
| 19 | View Code Duplication | if($data !== false) |
|
|
|
|||
| 20 | { |
||
| 21 | $this->data = $data; |
||
| 22 | if(isset($data['extended'])) |
||
| 23 | { |
||
| 24 | $this->data = $data['extended']; |
||
| 25 | } |
||
| 26 | } |
||
| 27 | } |
||
| 28 | |||
| 29 | public function isInGroupNamed($name) |
||
| 30 | { |
||
| 31 | if($this->auth === false) |
||
| 32 | { |
||
| 33 | return false; |
||
| 34 | } |
||
| 35 | $auth_data_set = $this->auth->dataSet; |
||
| 36 | $group_data_table = $auth_data_set['group']; |
||
| 37 | $uid = $this->uid; |
||
| 38 | $filter = new \Data\Filter("uid eq '$uid' and gid eq '$name'"); |
||
| 39 | $groups = $group_data_table->read($filter); |
||
| 40 | if($groups === false || !isset($groups[0])) |
||
| 41 | { |
||
| 42 | return false; |
||
| 43 | } |
||
| 44 | return true; |
||
| 45 | } |
||
| 46 | |||
| 47 | public function __get($propName) |
||
| 48 | { |
||
| 49 | if(isset($this->data[$propName])) |
||
| 50 | { |
||
| 51 | return $this->data[$propName]; |
||
| 52 | } |
||
| 53 | return false; |
||
| 54 | } |
||
| 55 | |||
| 56 | public function __set($propName, $value) |
||
| 59 | } |
||
| 60 | /* vim: set tabstop=4 shiftwidth=4 expandtab: */ |
||
| 61 |
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.