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 |
||
| 39 | class PushController extends OCSController { |
||
| 40 | |||
| 41 | /** @var IDBConnection */ |
||
| 42 | private $db; |
||
| 43 | |||
| 44 | /** @var ISession */ |
||
| 45 | private $session; |
||
| 46 | |||
| 47 | /** @var IUserSession */ |
||
| 48 | private $userSession; |
||
| 49 | |||
| 50 | /** @var IProvider */ |
||
| 51 | private $tokenProvider; |
||
| 52 | |||
| 53 | /** @var Manager */ |
||
| 54 | private $identityProof; |
||
| 55 | |||
| 56 | /** @var Crypto */ |
||
| 57 | private $crypto; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param string $appName |
||
| 61 | * @param IRequest $request |
||
| 62 | * @param IDBConnection $db |
||
| 63 | * @param ISession $session |
||
| 64 | * @param IUserSession $userSession |
||
| 65 | * @param IProvider $tokenProvider |
||
| 66 | * @param Manager $identityProof |
||
| 67 | * @param Crypto $crypto |
||
| 68 | */ |
||
| 69 | public function __construct($appName, IRequest $request, IDBConnection $db, ISession $session, IUserSession $userSession, IProvider $tokenProvider, Manager $identityProof, Crypto $crypto) { |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @NoAdminRequired |
||
| 82 | * @NoCSRFRequired |
||
| 83 | * |
||
| 84 | * @param string $pushTokenHash |
||
| 85 | * @param string $devicePublicKey |
||
| 86 | * @return JSONResponse |
||
| 87 | */ |
||
| 88 | public function registerDevice($pushTokenHash, $devicePublicKey) { |
||
| 89 | $user = $this->userSession->getUser(); |
||
| 90 | if (!$user instanceof IUser) { |
||
|
|
|||
| 91 | return new JSONResponse([], Http::STATUS_UNAUTHORIZED); |
||
| 92 | } |
||
| 93 | |||
| 94 | if (!preg_match('/^([a-f0-9]{128})$/', $pushTokenHash)) { |
||
| 95 | return new JSONResponse(['message' => 'Invalid hashed push token'], Http::STATUS_BAD_REQUEST); |
||
| 96 | } |
||
| 97 | |||
| 98 | View Code Duplication | if (strlen($devicePublicKey) !== 450 || |
|
| 99 | strpos($devicePublicKey, '-----BEGIN PUBLIC KEY-----') !== 0 || |
||
| 100 | strpos($devicePublicKey, '-----END PUBLIC KEY-----') !== 426) { |
||
| 101 | return new JSONResponse(['message' => 'Invalid device public key'], Http::STATUS_BAD_REQUEST); |
||
| 102 | } |
||
| 103 | |||
| 104 | $tokenId = $this->session->get('token-id'); |
||
| 105 | try { |
||
| 106 | $token = $this->tokenProvider->getTokenById($tokenId); |
||
| 107 | } catch (InvalidTokenException $e) { |
||
| 108 | return new JSONResponse(['message' => 'Could not identify session token'], Http::STATUS_BAD_REQUEST); |
||
| 109 | } |
||
| 110 | |||
| 111 | $key = $this->identityProof->getKey($user); |
||
| 112 | |||
| 113 | try { |
||
| 114 | $created = $this->savePushToken($user, $token, $devicePublicKey, $pushTokenHash); |
||
| 115 | } catch (\BadMethodCallException $e) { |
||
| 116 | return new JSONResponse(['message' => 'Invalid device public key'], Http::STATUS_BAD_REQUEST); |
||
| 117 | } |
||
| 118 | |||
| 119 | $encryptedData = $this->crypto->encrypt(sha1(json_encode([$user->getCloudId(), $token->getId()])), $user); |
||
| 120 | return new JSONResponse([ |
||
| 121 | 'publicKey' => $key->getPublic(), |
||
| 122 | 'deviceIdentifier' => $encryptedData['message'], |
||
| 123 | 'signature' => base64_encode($encryptedData['signature']), |
||
| 124 | ], $created ? Http::STATUS_CREATED : Http::STATUS_OK); |
||
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @NoAdminRequired |
||
| 129 | * @NoCSRFRequired |
||
| 130 | * |
||
| 131 | * @param string $devicePublicKey |
||
| 132 | * @return JSONResponse |
||
| 133 | */ |
||
| 134 | public function removeDevice($devicePublicKey) { |
||
| 135 | $user = $this->userSession->getUser(); |
||
| 136 | if (!$user instanceof IUser) { |
||
| 137 | return new JSONResponse([], Http::STATUS_UNAUTHORIZED); |
||
| 138 | } |
||
| 139 | |||
| 140 | View Code Duplication | if (strlen($devicePublicKey) !== 450 || |
|
| 141 | strpos($devicePublicKey, '-----BEGIN PUBLIC KEY-----') !== 0 || |
||
| 142 | strpos($devicePublicKey, '-----END PUBLIC KEY-----') !== 426) { |
||
| 143 | return new JSONResponse(['message' => 'Invalid device public key'], Http::STATUS_BAD_REQUEST); |
||
| 144 | } |
||
| 145 | |||
| 146 | $sessionId = $this->session->getId(); |
||
| 147 | try { |
||
| 148 | $token = $this->tokenProvider->getToken($sessionId); |
||
| 149 | } catch (InvalidTokenException $e) { |
||
| 150 | return new JSONResponse(['message' => 'Could not identify session token'], Http::STATUS_BAD_REQUEST); |
||
| 151 | } |
||
| 152 | |||
| 153 | try { |
||
| 154 | $this->deletePushToken($user, $token, $devicePublicKey); |
||
| 155 | } catch (\BadMethodCallException $e) { |
||
| 156 | return new JSONResponse(['message' => 'Invalid device public key'], Http::STATUS_BAD_REQUEST); |
||
| 157 | } |
||
| 158 | |||
| 159 | return new JSONResponse(); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param IUser $user |
||
| 164 | * @param IToken $token |
||
| 165 | * @param string $devicePublicKey |
||
| 166 | * @param string $pushTokenHash |
||
| 167 | * @return bool If the hash was new to the database |
||
| 168 | * @throws \BadMethodCallException |
||
| 169 | */ |
||
| 170 | protected function savePushToken(IUser $user, IToken $token, $devicePublicKey, $pushTokenHash) { |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param IUser $user |
||
| 191 | * @param IToken $token |
||
| 192 | * @param string $devicePublicKey |
||
| 193 | * @param string $pushTokenHash |
||
| 194 | * @return bool If the entry was created |
||
| 195 | */ |
||
| 196 | protected function insertPushToken(IUser $user, IToken $token, $devicePublicKey, $pushTokenHash) { |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @param IUser $user |
||
| 213 | * @param IToken $token |
||
| 214 | * @param string $devicePublicKey |
||
| 215 | * @param string $pushTokenHash |
||
| 216 | * @return bool If the entry was updated |
||
| 217 | * @throws \BadMethodCallException |
||
| 218 | */ |
||
| 219 | View Code Duplication | protected function updatePushToken(IUser $user, IToken $token, $devicePublicKey, $pushTokenHash) { |
|
| 235 | |||
| 236 | /** |
||
| 237 | * @param IUser $user |
||
| 238 | * @param IToken $token |
||
| 239 | * @param string $devicePublicKey |
||
| 240 | * @return bool If the entry was deleted |
||
| 241 | * @throws \BadMethodCallException |
||
| 242 | */ |
||
| 243 | View Code Duplication | protected function deletePushToken(IUser $user, IToken $token, $devicePublicKey) { |
|
| 258 | } |
||
| 259 |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.jsonfile (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.jsonto be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
requireorrequire-devsection?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceofchecks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.