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 |
||
| 28 | class RegisterAccount |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @var string|bool $session_key The session key |
||
| 32 | */ |
||
| 33 | private $session_key = false; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var object $db The database connection object |
||
| 37 | */ |
||
| 38 | public $db; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Constructor |
||
| 42 | * @param object $db_connection The database connection object |
||
| 43 | * @param string $session_key The session key |
||
| 44 | */ |
||
| 45 | View Code Duplication | public function __construct( $db_connection = false, $session_key = false ) |
|
| 52 | |||
| 53 | /** |
||
| 54 | * Account Email Exists |
||
| 55 | * |
||
| 56 | * Run a query to determine if a user account email exists in the database. |
||
| 57 | * |
||
| 58 | * @param string $user_account_email The data value |
||
| 59 | * @return false|string The query result |
||
| 60 | */ |
||
| 61 | View Code Duplication | public function account_email_exists( $user_account_email ) |
|
| 72 | |||
| 73 | /** |
||
| 74 | * Is Registered |
||
| 75 | * |
||
| 76 | * Run a query to determine if a user account has been registered. |
||
| 77 | * |
||
| 78 | * @param int $user_account_id The data value |
||
| 79 | * @return array|bool The query result |
||
| 80 | */ |
||
| 81 | View Code Duplication | public function is_registered( $user_account_id ) |
|
| 95 | |||
| 96 | /** |
||
| 97 | * Update Emailed Hash |
||
| 98 | * |
||
| 99 | * Run a query to update an emailed hash. |
||
| 100 | * |
||
| 101 | * @param int $user_account_id The data value |
||
| 102 | * @param string $emailed_hash The data value |
||
| 103 | * @return null|boolean The query result |
||
| 104 | */ |
||
| 105 | public function update_emailed_hash( $user_account_id = false, $emailed_hash = false ) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Update Password |
||
| 127 | * |
||
| 128 | * Run a query to update a password. |
||
| 129 | * |
||
| 130 | * @param int $user_account_password The data value |
||
| 131 | * @param int $user_account_id The data value |
||
| 132 | * @param string $emailed_hash The data value |
||
| 133 | * @return null|boolean True/False |
||
| 134 | */ |
||
| 135 | public function update_password( $user_account_password = false, $user_account_id = false, $emailed_hash = false ) |
||
| 156 | |||
| 157 | } |
||
| 158 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: