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 |
||
| 14 | class TemplateFunctions |
||
| 15 | { |
||
| 16 | |||
| 17 | const ASSETS_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::asset"; |
||
| 18 | const ROUTE_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::route"; |
||
| 19 | const CONFIG_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::config"; |
||
| 20 | const BUTTON_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::button"; |
||
| 21 | const WIDGET_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::widget"; |
||
| 22 | const FORM_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::form"; |
||
| 23 | const RESOURCE_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::resource"; |
||
| 24 | const SESSION_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::session"; |
||
| 25 | const EXISTS_FLASH_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::existsFlash"; |
||
| 26 | const GET_FLASH_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::getFlash"; |
||
| 27 | const GET_QUERY_FUNCTION = "\\PSFS\\base\\extension\\TemplateFunctions::query"; |
||
| 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 | 1 | public static function asset($string, $name = null, $return = true) |
|
| 38 | { |
||
| 39 | |||
| 40 | 1 | $file_path = ""; |
|
| 41 | 1 | if (!file_exists($file_path)) { |
|
| 42 | 1 | $file_path = BASE_DIR . $string; |
|
| 43 | } |
||
| 44 | 1 | $filename_path = AssetsParser::findDomainPath($string, $file_path); |
|
| 45 | |||
| 46 | 1 | $file_path = self::processAsset($string, $name, $return, $filename_path); |
|
| 47 | 1 | $return_path = (empty($name)) ? Request::getInstance()->getRootUrl() . '/' . $file_path : $name; |
|
| 48 | 1 | return ($return) ? $return_path : ''; |
|
| 49 | } |
||
| 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 = []) |
|
| 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 = '') |
|
| 80 | |||
| 81 | /** |
||
| 82 | * Función que devuelve un query string |
||
| 83 | * @param string $query |
||
| 84 | * |
||
| 85 | * @return string |
||
| 86 | */ |
||
| 87 | public static function query($query) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * Método que devuelve un botón en html para la plantilla de formularios |
||
| 94 | * @param array $button |
||
| 95 | */ |
||
| 96 | public static function button(array $button) |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Función que pinta parte de un formulario |
||
| 105 | * @param array $field |
||
| 106 | * @param StringClass $label |
||
| 107 | */ |
||
| 108 | public static function widget(array $field, StringClass $label = null) |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Función que deveulve un formulario en html |
||
| 126 | * @param Form $form |
||
| 127 | */ |
||
| 128 | public static function form(Form $form) |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Función que copia un recurso directamente en el DocumentRoot |
||
| 137 | * @param string $path |
||
| 138 | * @param string $dest |
||
| 139 | * @param bool|FALSE $force |
||
| 140 | * |
||
| 141 | * @return string |
||
| 142 | * @throws ConfigException |
||
| 143 | */ |
||
| 144 | 1 | public static function resource($path, $dest, $force = false) |
|
| 145 | { |
||
| 146 | 1 | $debug = Config::getInstance()->getDebugMode(); |
|
| 147 | 1 | $domains = Template::getDomains(true); |
|
| 148 | 1 | $filename_path = self::extractPathname($path, $domains); |
|
| 149 | 1 | \PSFS\Services\GeneratorService::copyResources($dest, $force, $filename_path, $debug); |
|
| 150 | 1 | return ''; |
|
| 151 | } |
||
| 152 | |||
| 153 | /** |
||
| 154 | * Método que extrae el pathname para un dominio |
||
| 155 | * @param string $path |
||
| 156 | * @param $domains |
||
| 157 | * |
||
| 158 | * @return mixed |
||
|
|
|||
| 159 | */ |
||
| 160 | 1 | private static function extractPathname($path, $domains) |
|
| 161 | { |
||
| 162 | 1 | $filename_path = $path; |
|
| 163 | 1 | View Code Duplication | if (!file_exists($path) && !empty($domains)) { |
| 164 | 1 | foreach ($domains as $domain => $paths) { |
|
| 165 | 1 | $domain_filename = str_replace($domain, $paths["public"], $path); |
|
| 166 | 1 | if (file_exists($domain_filename)) { |
|
| 167 | 1 | $filename_path = $domain_filename; |
|
| 168 | 1 | continue; |
|
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | } |
||
| 173 | |||
| 174 | 1 | return $filename_path; |
|
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param string $filename_path |
||
| 179 | */ |
||
| 180 | private static function processCssLines($filename_path) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Método que copia el contenido de un recurso en su destino correspondiente |
||
| 193 | * @param string $name |
||
| 194 | * @param string $filename_path |
||
| 195 | * @param string $base |
||
| 196 | * @param string $file_path |
||
| 197 | */ |
||
| 198 | 1 | private static function putResourceContent($name, $filename_path, $base, $file_path) |
|
| 199 | { |
||
| 200 | 1 | $data = file_get_contents($filename_path); |
|
| 201 | 1 | if (!empty($name)) file_put_contents(WEB_DIR . DIRECTORY_SEPARATOR . $name, $data); |
|
| 202 | 1 | else file_put_contents($base . $file_path, $data); |
|
| 203 | 1 | } |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Método que procesa un recurso para su copia en el DocumentRoot |
||
| 207 | * @param string $string |
||
| 208 | * @param string $name |
||
| 209 | * @param boolean $return |
||
| 210 | * @param string $filename_path |
||
| 211 | * |
||
| 212 | * @return string |
||
| 213 | */ |
||
| 214 | 1 | private static function processAsset($string, $name, $return, $filename_path) |
|
| 215 | { |
||
| 216 | 1 | $file_path = $filename_path; |
|
| 217 | 1 | if (file_exists($filename_path)) { |
|
| 218 | 1 | list($base, $html_base, $file_path) = AssetsParser::calculateAssetPath($string, $name, $return, $filename_path); |
|
| 219 | //Creamos el directorio si no existe |
||
| 220 | 1 | GeneratorHelper::createDir($base . $html_base); |
|
| 221 | //Si se ha modificado |
||
| 222 | 1 | if (!file_exists($base . $file_path) || filemtime($base . $file_path) < filemtime($filename_path)) { |
|
| 223 | 1 | if ($html_base == 'css') { |
|
| 224 | self::processCssLines($filename_path); |
||
| 225 | } |
||
| 226 | 1 | self::putResourceContent($name, $filename_path, $base, $file_path); |
|
| 227 | } |
||
| 228 | } |
||
| 229 | |||
| 230 | 1 | return $file_path; |
|
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Template function for get a session var |
||
| 235 | * @param string $key |
||
| 236 | * @return mixed |
||
| 237 | */ |
||
| 238 | 1 | public static function session($key) |
|
| 239 | { |
||
| 240 | 1 | return Security::getInstance()->getSessionKey($key); |
|
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Template function that check if exists any flash session var |
||
| 245 | * @param string $key |
||
| 246 | * @return bool |
||
| 247 | */ |
||
| 248 | 1 | public static function existsFlash($key = '') |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Template function that get a flash session var |
||
| 255 | * @param string $key |
||
| 256 | * @return mixed |
||
| 257 | */ |
||
| 258 | public static function getFlash($key) |
||
| 264 | |||
| 265 | public static function translate($key) { |
||
| 266 | |||
| 267 | } |
||
| 268 | } |
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.