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 UserAPIToken { |
||
|
|
|||
| 10 | |||
| 11 | const USER_TOKENS_TABLE = 'user_tokens'; |
||
| 12 | |||
| 13 | /** |
||
| 14 | * @var string $consumerKey The unique ID of the tool consumer from whence the user is making requests to the LTI |
||
| 15 | **/ |
||
| 16 | private $consumerKey; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @var string $id The unique ID (within the context of a particular Tool Consumer) of a particular user |
||
| 20 | **/ |
||
| 21 | private $id; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var mysqli $sql A MySQL connection |
||
| 25 | **/ |
||
| 26 | private $sql; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string|null $token This user's API access token (if acquired) or NULL if not yet acquired |
||
| 30 | **/ |
||
| 31 | private $token = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var string|null $apiUrl The URL of the API for which this user's token is valid, NULL if no token |
||
| 35 | **/ |
||
| 36 | private $apiUrl = null; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Create a new UserAPIToken to either register a new user in the |
||
| 40 | * USER_TOKENS_TABLE or look up an existing user. |
||
| 41 | * |
||
| 42 | * @param string $consumerKey The unique ID of the Tool Consumer from whence the user is making requests to the LTI |
||
| 43 | * @param string $userId The unique ID of the user within that Tool Consumer |
||
| 44 | * @param mysqli $mysqli An active MySQL database connection to update USER_TOKEN_TABLE |
||
| 45 | * |
||
| 46 | * @throws UserAPIToken_Exception CONSUMER_KEY_REQUIRED If no consumer key is provided |
||
| 47 | * @throws UserAPIToken_Exception USER_ID_REQUIRED If no user ID is provided |
||
| 48 | * @throws UserAPIToken_Exception MYSQLI_REQUIRED If no MySQL database connection is provided |
||
| 49 | * @throws UserAPIToken_Excpetion MYSQLI_ERROR If the user cannot be found or inserted in USER_TOKEN_TABLE |
||
| 50 | **/ |
||
| 51 | public function __construct($consumerKey, $userId, $mysqli) { |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @return string|boolean The API access token for this user, or FALSE if no token has been acquired |
||
| 95 | **/ |
||
| 96 | public function getToken() { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Stores a new API Token into USER_TOKEN_TABLE for this user |
||
| 105 | * |
||
| 106 | * @param string $token A new API access token for this user |
||
| 107 | * |
||
| 108 | * @return boolean Returns TRUE if the token is successfully stored in USER_TOKEN_TABLE, FALSE otherwise |
||
| 109 | * |
||
| 110 | * @throws UserAPIToken_Exception TOKEN_REQUIRED If no token is provided |
||
| 111 | **/ |
||
| 112 | View Code Duplication | public function setToken($token) { |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @return string|boolean The URL of the API for which the user's API token is valid, or FALSE if no token has been acquired |
||
| 137 | **/ |
||
| 138 | function getAPIUrl() { |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Stores a new URL for the API URL for which the user's API access token is valid in USER_TOKEN_TABLE |
||
| 147 | * |
||
| 148 | * @param string $apiUrl The URL of the API |
||
| 149 | * |
||
| 150 | * @return boolean TRUE if the URL of the API is stored in USER_TOKEN_TABLE, FALSE otherwise |
||
| 151 | * |
||
| 152 | * @throws UserAPITokenException API_URL_REQUIRED If no URL is provided |
||
| 153 | **/ |
||
| 154 | View Code Duplication | public function setAPIUrl($apiUrl) { |
|
| 175 | } |
||
| 176 | |||
| 191 | ?> |
||
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.