Conditions | 16 |
Paths | 384 |
Total Lines | 94 |
Code Lines | 43 |
Lines | 5 |
Ratio | 5.32 % |
Changes | 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 |
||
24 | function Smarty_function_icon($params) |
||
25 | { |
||
26 | $src = ''; |
||
27 | $height = ''; |
||
28 | $alt = ''; |
||
29 | $name = ''; |
||
30 | $width = ''; |
||
31 | $extra = ''; |
||
32 | |||
33 | /* |
||
34 | @todo provide usage help text in error message |
||
35 | if (empty($params['name']) and empty($params['src'])) { |
||
36 | trigger_error('Provide "name" or "src".', E_USER_ERROR); |
||
37 | |||
38 | return; |
||
39 | } */ |
||
40 | |||
41 | extract($params); |
||
42 | |||
43 | /* |
||
44 | * if the src attribute contains a http://SERVER_NAME URL its cutted of |
||
45 | */ |
||
46 | if (isset($src) and empty($src) === false) { |
||
47 | $needle = 'http://' . $_SERVER['SERVER_NAME'] . '/'; |
||
48 | $pos = mb_strpos($src, $needle); |
||
49 | if ($src !== null and is_int($pos)) { |
||
50 | #\Koch\Debug\Debug::printR($pos); |
||
51 | $src = mb_substr($src, $pos + mb_strlen($needle)); |
||
52 | $name = basename($src); |
||
53 | } |
||
54 | } |
||
55 | |||
56 | // we have two alternatives : |
||
57 | // a) src => user has set src, defining the path to the image and imagename |
||
58 | // b) icondir, name => user has defined the icons dir (relative to core/images folder) and the name of a png file |
||
59 | // check if it is a valid one |
||
60 | $icondir_whitelist = ['icons', 'lullacons']; |
||
61 | if ((isset($icondir)) and in_array($icondir, $icondir_whitelist, true)) { |
||
62 | // valid |
||
63 | $icondir .= ''; // leave this. would else be an empty if statement |
||
64 | } else { // fallback to a valid default |
||
65 | $icondir = 'icons'; |
||
66 | } |
||
67 | |||
68 | // transform name into a valid image src |
||
69 | $src = realpath(APPLICATION_PATH . 'themes/' . 'core/images/' . $icondir . DIRECTORY_SEPARATOR . $name . '.png'); |
||
70 | |||
71 | // if we got no valid src, set a default image |
||
72 | if (isset($src) and is_file($src) === false) { |
||
73 | #$src = WWW_ROOT_THEMES_CORE . 'images/noimage.gif'; |
||
74 | $src = APPLICATION_PATH . 'themes/' . 'core/images/noimage.gif'; |
||
75 | $name = 'No Image found.' . $src; |
||
76 | } |
||
77 | |||
78 | // we got no height, set it to zero |
||
79 | if (empty($height)) { |
||
80 | $height = 0; |
||
81 | } |
||
82 | |||
83 | // we got no width, ok then its zero again |
||
84 | if (empty($width)) { |
||
85 | $width = 0; |
||
86 | } |
||
87 | |||
88 | // we got no height nor width. well let's detect it automatically then. |
||
89 | View Code Duplication | if (($height === 0) || ($width === 0)) { |
|
90 | $currentimagesize = getimagesize($src); |
||
91 | $width = $currentimagesize[0]; |
||
92 | $height = $currentimagesize[1]; |
||
93 | } |
||
94 | |||
95 | // we got no alternative text. let's add a default text with $name; |
||
96 | if (($src !== null) and empty($alt)) { |
||
97 | $file = $src; |
||
98 | |||
99 | $info = pathinfo($file); |
||
100 | $file_name = basename($file, '.' . $info['extension']); |
||
101 | $alt = $file_name; |
||
102 | } |
||
103 | |||
104 | // no extra attributes to add, then let it be an empty string |
||
105 | if (empty($extra)) { |
||
106 | $extra = ''; |
||
107 | } |
||
108 | |||
109 | // prepare link: transform absolute path into webpath and apply slashfix |
||
110 | $src = str_replace(APPLICATION_PATH . 'themes/', WWW_ROOT_THEMES, $src); |
||
111 | $src = str_replace('\\', '/', $src); |
||
112 | |||
113 | $html = '<img src="' . $src . '" height="' . $height . '" width="' |
||
114 | . $width . '" alt="' . $alt . '" ' . $extra . ' />'; |
||
115 | |||
116 | return $html; |
||
117 | } |
||
118 |
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.