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 |
||
| 24 | class Validator |
||
| 25 | { |
||
| 26 | /** |
||
| 27 | * The translator |
||
| 28 | * |
||
| 29 | * @var \Jaxon\Utils\Translation\Translator |
||
| 30 | */ |
||
| 31 | protected $xTranslator; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * The config manager |
||
| 35 | * |
||
| 36 | * @var \Jaxon\Utils\Config\Config |
||
| 37 | */ |
||
| 38 | protected $xConfig; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The last error message |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | protected $sErrorMessage; |
||
| 46 | |||
| 47 | public function __construct($xTranslator, $xConfig) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * Get the last error message |
||
| 57 | * |
||
| 58 | * @return string The last error message |
||
| 59 | */ |
||
| 60 | public function getErrorMessage() |
||
| 64 | |||
| 65 | /** |
||
| 66 | * Validate a function name |
||
| 67 | * |
||
| 68 | * @param string $sName The function name |
||
| 69 | * |
||
| 70 | * @return bool True if the function name is valid, and false if not |
||
| 71 | */ |
||
| 72 | public function validateFunction($sName) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Validate an event name |
||
| 80 | * |
||
| 81 | * @param string $sName The event name |
||
| 82 | * |
||
| 83 | * @return bool True if the event name is valid, and false if not |
||
| 84 | */ |
||
| 85 | public function validateEvent($sName) |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Validate a class name |
||
| 93 | * |
||
| 94 | * @param string $sName The class name |
||
| 95 | * |
||
| 96 | * @return bool True if the class name is valid, and false if not |
||
| 97 | */ |
||
| 98 | public function validateClass($sName) |
||
| 103 | |||
| 104 | /** |
||
| 105 | * Validate a method name |
||
| 106 | * |
||
| 107 | * @param string $sName The function name |
||
| 108 | * |
||
| 109 | * @return bool True if the method name is valid, and false if not |
||
| 110 | */ |
||
| 111 | public function validateMethod($sName) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Validate an uploaded file |
||
| 120 | * |
||
| 121 | * @param string $sName The uploaded file variable name |
||
| 122 | * @param array $aUploadedFile The file data received in the $_FILES array |
||
| 123 | * |
||
| 124 | * @return bool True if the file data are valid, and false if not |
||
| 125 | */ |
||
| 126 | public function validateUploadedFile($sName, array $aUploadedFile) |
||
| 163 | } |
||
| 164 |
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.