| Conditions | 31 |
| Paths | 9882 |
| Total Lines | 121 |
| Code Lines | 89 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 37 | function smarty_function_html_image($params, Smarty_Internal_Template $template) |
||
| 38 | { |
||
| 39 | $template->_checkPlugins( |
||
| 40 | array( |
||
| 41 | array( |
||
| 42 | 'function' => 'smarty_function_escape_special_chars', |
||
| 43 | 'file' => SMARTY_PLUGINS_DIR . 'shared.escape_special_chars.php' |
||
| 44 | ) |
||
| 45 | ) |
||
| 46 | ); |
||
| 47 | $alt = ''; |
||
| 48 | $file = ''; |
||
| 49 | $height = ''; |
||
| 50 | $width = ''; |
||
| 51 | $extra = ''; |
||
| 52 | $prefix = ''; |
||
| 53 | $suffix = ''; |
||
| 54 | $path_prefix = ''; |
||
| 55 | $basedir = isset($_SERVER[ 'DOCUMENT_ROOT' ]) ? $_SERVER[ 'DOCUMENT_ROOT' ] : ''; |
||
| 56 | foreach ($params as $_key => $_val) { |
||
| 57 | switch ($_key) { |
||
| 58 | case 'file': |
||
| 59 | case 'height': |
||
| 60 | case 'width': |
||
| 61 | case 'dpi': |
||
| 62 | case 'path_prefix': |
||
| 63 | case 'basedir': |
||
| 64 | $$_key = $_val; |
||
| 65 | break; |
||
| 66 | case 'alt': |
||
| 67 | if (!is_array($_val)) { |
||
| 68 | $$_key = smarty_function_escape_special_chars($_val); |
||
| 69 | } else { |
||
| 70 | throw new SmartyException( |
||
| 71 | "html_image: extra attribute '{$_key}' cannot be an array", |
||
| 72 | E_USER_NOTICE |
||
| 73 | ); |
||
| 74 | } |
||
| 75 | break; |
||
| 76 | case 'link': |
||
| 77 | case 'href': |
||
| 78 | $prefix = '<a href="' . $_val . '">'; |
||
| 79 | $suffix = '</a>'; |
||
| 80 | break; |
||
| 81 | default: |
||
| 82 | if (!is_array($_val)) { |
||
| 83 | $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"'; |
||
| 84 | } else { |
||
| 85 | throw new SmartyException( |
||
| 86 | "html_image: extra attribute '{$_key}' cannot be an array", |
||
| 87 | E_USER_NOTICE |
||
| 88 | ); |
||
| 89 | } |
||
| 90 | break; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | if (empty($file)) { |
||
|
|
|||
| 94 | trigger_error('html_image: missing \'file\' parameter', E_USER_NOTICE); |
||
| 95 | return; |
||
| 96 | } |
||
| 97 | if ($file[ 0 ] === '/') { |
||
| 98 | $_image_path = $basedir . $file; |
||
| 99 | } else { |
||
| 100 | $_image_path = $file; |
||
| 101 | } |
||
| 102 | // strip file protocol |
||
| 103 | if (stripos($params[ 'file' ], 'file://') === 0) { |
||
| 104 | $params[ 'file' ] = substr($params[ 'file' ], 7); |
||
| 105 | } |
||
| 106 | $protocol = strpos($params[ 'file' ], '://'); |
||
| 107 | if ($protocol !== false) { |
||
| 108 | $protocol = strtolower(substr($params[ 'file' ], 0, $protocol)); |
||
| 109 | } |
||
| 110 | if (isset($template->smarty->security_policy)) { |
||
| 111 | if ($protocol) { |
||
| 112 | // remote resource (or php stream, …) |
||
| 113 | if (!$template->smarty->security_policy->isTrustedUri($params[ 'file' ])) { |
||
| 114 | return; |
||
| 115 | } |
||
| 116 | } else { |
||
| 117 | // local file |
||
| 118 | if (!$template->smarty->security_policy->isTrustedResourceDir($_image_path)) { |
||
| 119 | return; |
||
| 120 | } |
||
| 121 | } |
||
| 122 | } |
||
| 123 | if (!isset($params[ 'width' ]) || !isset($params[ 'height' ])) { |
||
| 124 | // FIXME: (rodneyrehm) getimagesize() loads the complete file off a remote resource, use custom [jpg,png,gif]header reader! |
||
| 125 | if (!$_image_data = @getimagesize($_image_path)) { |
||
| 126 | if (!file_exists($_image_path)) { |
||
| 127 | trigger_error("html_image: unable to find '{$_image_path}'", E_USER_NOTICE); |
||
| 128 | return; |
||
| 129 | } elseif (!is_readable($_image_path)) { |
||
| 130 | trigger_error("html_image: unable to read '{$_image_path}'", E_USER_NOTICE); |
||
| 131 | return; |
||
| 132 | } else { |
||
| 133 | trigger_error("html_image: '{$_image_path}' is not a valid image file", E_USER_NOTICE); |
||
| 134 | return; |
||
| 135 | } |
||
| 136 | } |
||
| 137 | if (!isset($params[ 'width' ])) { |
||
| 138 | $width = $_image_data[ 0 ]; |
||
| 139 | } |
||
| 140 | if (!isset($params[ 'height' ])) { |
||
| 141 | $height = $_image_data[ 1 ]; |
||
| 142 | } |
||
| 143 | } |
||
| 144 | if (isset($params[ 'dpi' ])) { |
||
| 145 | if (strstr($_SERVER[ 'HTTP_USER_AGENT' ], 'Mac')) { |
||
| 146 | // FIXME: (rodneyrehm) wrong dpi assumption |
||
| 147 | // don't know who thought this up… even if it was true in 1998, it's definitely wrong in 2011. |
||
| 148 | $dpi_default = 72; |
||
| 149 | } else { |
||
| 150 | $dpi_default = 96; |
||
| 151 | } |
||
| 152 | $_resize = $dpi_default / $params[ 'dpi' ]; |
||
| 153 | $width = round($width * $_resize); |
||
| 154 | $height = round($height * $_resize); |
||
| 155 | } |
||
| 156 | return $prefix . '<img src="' . $path_prefix . $file . '" alt="' . $alt . '" width="' . $width . '" height="' . |
||
| 157 | $height . '"' . $extra . ' />' . $suffix; |
||
| 158 | } |
||
| 159 |