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 |
||
| 15 | class TemplateFunctions |
||
| 16 | { |
||
| 17 | |||
| 18 | const ASSETS_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::asset"; |
||
| 19 | const ROUTE_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::route"; |
||
| 20 | const CONFIG_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::config"; |
||
| 21 | const BUTTON_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::button"; |
||
| 22 | const WIDGET_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::widget"; |
||
| 23 | const FORM_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::form"; |
||
| 24 | const RESOURCE_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::resource"; |
||
| 25 | const SESSION_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::session"; |
||
| 26 | const EXISTS_FLASH_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::existsFlash"; |
||
| 27 | const GET_FLASH_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::getFlash"; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Función que copia los recursos de las carpetas Public al DocumentRoot |
||
| 31 | * @param $string |
||
| 32 | * @param null $name |
||
| 33 | * @param bool|TRUE $return |
||
| 34 | * |
||
| 35 | * @return string |
||
| 36 | */ |
||
| 37 | public static function asset($string, $name = null, $return = true) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Función que devuelve una url correspondiente a una ruta |
||
| 53 | * @param string $path |
||
| 54 | * @param bool|FALSE $absolute |
||
| 55 | * @param array $params |
||
|
|
|||
| 56 | * |
||
| 57 | * @return string|null |
||
| 58 | */ |
||
| 59 | 1 | public static function route($path = '', $absolute = false, array $params = null) |
|
| 60 | { |
||
| 61 | 1 | $router = Router::getInstance(); |
|
| 62 | try { |
||
| 63 | 1 | return $router->getRoute($path, $absolute, $params); |
|
| 64 | } catch (\Exception $e) { |
||
| 65 | return $router->getRoute('', $absolute, $params); |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Función que devuelve un parámetro de la configuración |
||
| 71 | * @param $param |
||
| 72 | * @param string $default |
||
| 73 | * |
||
| 74 | * @return string |
||
| 75 | */ |
||
| 76 | 1 | public static function config($param, $default = '') |
|
| 77 | { |
||
| 78 | 1 | return Config::getInstance()->get($param) ?: $default; |
|
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Método que devuelve un botón en html para la plantilla de formularios |
||
| 83 | * @param array $button |
||
| 84 | */ |
||
| 85 | public static function button(array $button) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Función que pinta parte de un formulario |
||
| 94 | * @param array $field |
||
| 95 | * @param StringClass $label |
||
| 96 | */ |
||
| 97 | public static function widget(array $field, StringClass $label = null) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Función que deveulve un formulario en html |
||
| 115 | * @param Form $form |
||
| 116 | */ |
||
| 117 | public static function form(Form $form) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Función que copia un recurso directamente en el DocumentRoot |
||
| 126 | * @param string $path |
||
| 127 | * @param string $dest |
||
| 128 | * @param bool|FALSE $force |
||
| 129 | * |
||
| 130 | * @return string |
||
| 131 | * @throws ConfigException |
||
| 132 | */ |
||
| 133 | public static function resource($path, $dest, $force = false) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Método que extrae el pathname para un dominio |
||
| 144 | * @param string $path |
||
| 145 | * @param $domains |
||
| 146 | * |
||
| 147 | * @return mixed |
||
| 148 | */ |
||
| 149 | private static function extractPathname($path, $domains) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @param string $filename_path |
||
| 168 | */ |
||
| 169 | private static function processCssLines($filename_path) |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Método que copia el contenido de un recurso en su destino correspondiente |
||
| 182 | * @param string $name |
||
| 183 | * @param string $filename_path |
||
| 184 | * @param string $base |
||
| 185 | * @param string $file_path |
||
| 186 | */ |
||
| 187 | private static function putResourceContent($name, $filename_path, $base, $file_path) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Método que procesa un recurso para su copia en el DocumentRoot |
||
| 196 | * @param string $string |
||
| 197 | * @param string $name |
||
| 198 | * @param boolean $return |
||
| 199 | * @param string $filename_path |
||
| 200 | * |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | private static function processAsset($string, $name, $return, $filename_path) |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Template function for get a session var |
||
| 224 | * @param string $key |
||
| 225 | * @return mixed |
||
| 226 | */ |
||
| 227 | 1 | public static function session($key) |
|
| 228 | { |
||
| 229 | 1 | return Security::getInstance()->getSessionKey($key); |
|
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Template function that check if exists any flash session var |
||
| 234 | * @param string $key |
||
| 235 | * @return bool |
||
| 236 | */ |
||
| 237 | 1 | public static function existsFlash($key = '') |
|
| 238 | { |
||
| 239 | 1 | return null !== Security::getInstance()->getFlash($key); |
|
| 240 | } |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Template function that get a flash session var |
||
| 244 | * @param string $key |
||
| 245 | * @return mixed |
||
| 246 | */ |
||
| 247 | public static function getFlash($key) |
||
| 253 | } |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
arrayand suggests a stricter type likearray<String>.Most often this is a case of a parameter that can be null in addition to its declared types.