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 |
||
| 25 | class PasswordValidator {
|
||
| 26 | |||
| 27 | /** @var SecurityConfig */ |
||
| 28 | protected $config; |
||
| 29 | |||
| 30 | /** @var IL10N */ |
||
| 31 | protected $l; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * PasswordValidator constructor. |
||
| 35 | * |
||
| 36 | * @param SecurityConfig $config |
||
| 37 | * @param IL10N $l |
||
| 38 | */ |
||
| 39 | public function __construct(SecurityConfig $config, IL10N $l) {
|
||
| 43 | |||
| 44 | /** |
||
| 45 | * validate the given password satisfies defined password policy |
||
| 46 | * |
||
| 47 | * @param string $password |
||
| 48 | * @throws HintException |
||
| 49 | */ |
||
| 50 | public function validate($password) {
|
||
| 56 | |||
| 57 | /** |
||
| 58 | * validate the given password satisfies defined min length |
||
| 59 | * |
||
| 60 | * @param string $password |
||
| 61 | * @throws HintException |
||
| 62 | */ |
||
| 63 | public function checkPasswordLength($password) {
|
||
| 73 | |||
| 74 | /** |
||
| 75 | * validate if password contains at least one upper and one lower case character |
||
| 76 | * |
||
| 77 | * @param string $password |
||
| 78 | * @throws HintException |
||
| 79 | */ |
||
| 80 | View Code Duplication | public function checkUpperLowerCase($password) {
|
|
| 90 | |||
| 91 | /** |
||
| 92 | * validate the given password satisfies numeric character policy |
||
| 93 | * |
||
| 94 | * @param string $password |
||
| 95 | * @throws HintException |
||
| 96 | */ |
||
| 97 | View Code Duplication | public function checkNumericCharacters($password) {
|
|
| 107 | |||
| 108 | /** |
||
| 109 | * check if password contains at least one special character |
||
| 110 | * |
||
| 111 | * @param string $password |
||
| 112 | * @throws HintException |
||
| 113 | */ |
||
| 114 | View Code Duplication | public function checkSpecialCharacters($password) {
|
|
| 124 | |||
| 125 | public function hasNumericalCharacters($password) {
|
||
| 128 | |||
| 129 | public function hasUpperAndLowerCase($password) {
|
||
| 134 | |||
| 135 | public function hasSpecialCharacter($password) {
|
||
| 138 | } |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.