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 |
||
| 7 | View Code Duplication | class Strlen extends AbstractStringCase implements SanitizeRuleInterface |
|
|
|
|||
| 8 | { |
||
| 9 | /** @var int */ |
||
| 10 | protected $len; |
||
| 11 | |||
| 12 | /** @var string */ |
||
| 13 | protected $padString; |
||
| 14 | |||
| 15 | /** @var int */ |
||
| 16 | protected $padType; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * @param int $len The string length. |
||
| 20 | * @param string $pad_string Pad using this string. |
||
| 21 | * @param int $pad_type A `STR_PAD_*` constant. |
||
| 22 | */ |
||
| 23 | 21 | public function __construct(int $len, string $padString = ' ', int $padType = STR_PAD_RIGHT) |
|
| 29 | |||
| 30 | /** |
||
| 31 | * Sanitizes a string to an exact length by padding or chopping it. |
||
| 32 | * |
||
| 33 | * @param object $subject The subject to be filtered. |
||
| 34 | * @param string $field The subject field name. |
||
| 35 | * |
||
| 36 | * @return bool True if the value was sanitized, false if not. |
||
| 37 | */ |
||
| 38 | 21 | public function __invoke($subject, string $field): bool |
|
| 53 | } |
||
| 54 |
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.