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 |
||
| 18 | class ScratchTokenCredentialProvider extends CredentialProviderBase |
||
| 19 | { |
||
| 20 | /** @var EncryptionHelper */ |
||
| 21 | private $encryptionHelper; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * ScratchTokenCredentialProvider constructor. |
||
| 25 | * |
||
| 26 | * @param PdoDatabase $database |
||
| 27 | * @param SiteConfiguration $configuration |
||
| 28 | */ |
||
| 29 | public function __construct(PdoDatabase $database, SiteConfiguration $configuration) |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Validates a user-provided credential |
||
| 37 | * |
||
| 38 | * @param User $user The user to test the authentication against |
||
| 39 | * @param string $data The raw credential data to be validated |
||
| 40 | * |
||
| 41 | * @return bool |
||
| 42 | * @throws ApplicationLogicException |
||
| 43 | */ |
||
| 44 | public function authenticate(User $user, $data) |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @param User $user The user the credential belongs to |
||
| 74 | * @param int $factor The factor this credential provides |
||
| 75 | * @param string $data Unused. |
||
| 76 | */ |
||
| 77 | public function setCredential(User $user, $factor, $data) |
||
| 99 | |||
| 100 | /** |
||
| 101 | * @param int $userId |
||
| 102 | * |
||
| 103 | * @return int |
||
| 104 | * @throws ApplicationLogicException |
||
| 105 | */ |
||
| 106 | View Code Duplication | public function getRemaining($userId) |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @param int $userId |
||
| 121 | * |
||
| 122 | * @return int |
||
| 123 | * @throws ApplicationLogicException |
||
| 124 | */ |
||
| 125 | View Code Duplication | public function getTokens($userId) |
|
| 137 | } |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: