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 |
||
| 8 | class PurifierValidator extends Validator |
||
| 9 | { |
||
| 10 | |||
| 11 | /** |
||
| 12 | * The default configuration for the HTMLPurifier_Config. |
||
| 13 | * |
||
| 14 | * @var array |
||
| 15 | */ |
||
| 16 | protected static $_purifierEmptyConfig = [ |
||
| 17 | 'Core.Encoding' => 'UTF-8', |
||
| 18 | 'HTML.Allowed' => '' |
||
| 19 | ]; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Check if the length of a HTML string is smaller or equal to a maximal length. |
||
| 23 | * |
||
| 24 | * @param string $check The value to check. |
||
| 25 | * @param int $max The maximal string length. |
||
| 26 | * |
||
| 27 | * @return bool |
||
| 28 | */ |
||
| 29 | View Code Duplication | public static function purifierMaxLength($check, $max) |
|
| 39 | |||
| 40 | /** |
||
| 41 | * Check if the length of a HTML string is greater or equal to a minimal length. |
||
| 42 | * |
||
| 43 | * @param string $check The value to check. |
||
| 44 | * @param int $min The minimal string length. |
||
| 45 | * |
||
| 46 | * @return bool |
||
| 47 | */ |
||
| 48 | View Code Duplication | public static function purifierMinLength($check, $min) |
|
| 58 | |||
| 59 | /** |
||
| 60 | * Parse a HTML string with HTMLPurifier to remove all/special HTML tags. |
||
| 61 | * |
||
| 62 | * @param bool|string $text The string to be parsed. |
||
| 63 | * |
||
| 64 | * @return bool|string |
||
| 65 | */ |
||
| 66 | protected static function _purifyEmpty($text = false) |
||
| 77 | } |
||
| 78 |
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.