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 |
||
| 12 | class UsersService extends Base |
||
| 13 | { |
||
| 14 | /** |
||
| 15 | * Constructor of the class. |
||
| 16 | * |
||
| 17 | * @param object $database |
||
| 18 | */ |
||
| 19 | public function __construct(\PDO $database) |
||
| 20 | { |
||
| 21 | $this->database = $database; |
||
| 22 | } |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Check if the user exists. |
||
| 26 | * |
||
| 27 | * @param int $userId |
||
| 28 | * @return object $user |
||
| 29 | * @throws \Exception |
||
| 30 | */ |
||
| 31 | public function checkUser($userId) |
||
| 32 | { |
||
| 33 | $repo = new UsersRepository; |
||
| 34 | $stmt = $this->database->prepare($repo->getUserQuery()); |
||
| 35 | $stmt->bindParam('id', $userId); |
||
| 36 | $stmt->execute(); |
||
| 37 | $user = $stmt->fetchObject(); |
||
| 38 | if (!$user) { |
||
| 39 | throw new \Exception(self::USER_NOT_FOUND, 404); |
||
| 40 | } |
||
| 41 | |||
| 42 | return $user; |
||
| 43 | } |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Get all users. |
||
| 47 | * |
||
| 48 | * @return array |
||
| 49 | */ |
||
| 50 | public function getUsers() |
||
| 51 | { |
||
| 52 | $repository = new UsersRepository; |
||
| 53 | $query = $repository->getUsersQuery(); |
||
| 54 | $statement = $this->database->prepare($query); |
||
| 55 | $statement->execute(); |
||
| 56 | |||
| 57 | return $statement->fetchAll(); |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Get one user by id. |
||
| 62 | * |
||
| 63 | * @param int $userId |
||
| 64 | * @return array |
||
| 65 | */ |
||
| 66 | public function getUser($userId) |
||
| 67 | { |
||
| 68 | $user = $this->checkUser($userId); |
||
| 69 | |||
| 70 | return $user; |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Search users by name. |
||
| 75 | * |
||
| 76 | * @param string $str |
||
| 77 | * @return array |
||
| 78 | * @throws \Exception |
||
| 79 | */ |
||
| 80 | View Code Duplication | public function searchUsers($str) |
|
| 81 | { |
||
| 82 | $repo = new UsersRepository; |
||
| 83 | $stmt = $this->database->prepare($repo->searchUsersQuery()); |
||
| 84 | $name = '%' . $str . '%'; |
||
| 85 | $stmt->bindParam('name', $name); |
||
| 86 | $stmt->execute(); |
||
| 87 | $users = $stmt->fetchAll(); |
||
| 88 | |||
| 89 | if (!$users) { |
||
| 90 | throw new \Exception(self::USER_NAME_NOT_FOUND, 404); |
||
| 91 | } |
||
| 92 | |||
| 93 | return $users; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Create a user. |
||
| 98 | * |
||
| 99 | * @param array $input |
||
| 100 | * @return array |
||
| 101 | * @throws \Exception |
||
| 102 | */ |
||
| 103 | public function createUser($input) |
||
| 104 | { |
||
| 105 | if (!isset($input['name'])) { |
||
| 106 | throw new \Exception(self::USER_NAME_REQUIRED, 400); |
||
| 107 | } |
||
| 108 | $name = $input['name']; |
||
| 109 | $usernameValidator = v::alnum()->length(1, 100); |
||
| 110 | if (!$usernameValidator->validate($name)) { |
||
| 111 | throw new \Exception(self::USER_NAME_INVALID, 400); |
||
| 112 | } |
||
| 113 | $email = null; |
||
| 114 | if (isset($input['email'])) { |
||
| 115 | $email = $this->validateEmail($input['email']); |
||
| 116 | } |
||
| 117 | $repository = new UsersRepository; |
||
| 118 | $query = $repository->createUserQuery(); |
||
| 119 | $statement = $this->database->prepare($query); |
||
| 120 | $statement->bindParam('name', $name); |
||
| 121 | $statement->bindParam('email', $email); |
||
| 122 | $statement->execute(); |
||
| 123 | $user = $this->checkUser($this->database->lastInsertId()); |
||
| 124 | |||
| 125 | return $user; |
||
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Update a user. |
||
| 130 | * |
||
| 131 | * @param array $input |
||
| 132 | * @param int $userId |
||
| 133 | * @return array |
||
| 134 | * @throws \Exception |
||
| 135 | */ |
||
| 136 | View Code Duplication | public function updateUser($input, $userId) |
|
| 137 | { |
||
| 138 | $user = $this->checkUser($userId); |
||
| 139 | if (empty($input['name']) && empty($input['email'])) { |
||
| 140 | throw new \Exception(self::USER_INFO_REQUIRED, 400); |
||
| 141 | } |
||
| 142 | $username = isset($input['name']) ? $input['name'] : $user->name; |
||
| 143 | $email = $user->email; |
||
| 144 | if (isset($input['email'])) { |
||
| 145 | $email = $this->validateEmail($input['email']); |
||
| 146 | } |
||
| 147 | $repository = new UsersRepository; |
||
| 148 | $query = $repository->updateUserQuery(); |
||
| 149 | $statement = $this->database->prepare($query); |
||
| 150 | $statement->bindParam('id', $userId); |
||
| 151 | $statement->bindParam('name', $username); |
||
| 152 | $statement->bindParam('email', $email); |
||
| 153 | $statement->execute(); |
||
| 154 | |||
| 155 | return $this->checkUser($userId); |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Delete a user. |
||
| 160 | * |
||
| 161 | * @param int $userId |
||
| 162 | * @return array |
||
| 163 | */ |
||
| 164 | public function deleteUser($userId) |
||
| 175 | } |
||
| 176 |