| Conditions | 13 |
| Paths | 6 |
| Total Lines | 79 |
| Code Lines | 48 |
| Lines | 0 |
| Ratio | 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 |
||
| 25 | public function widgetImageAction() |
||
|
1 ignored issue
–
show
|
|||
| 26 | { |
||
| 27 | if (!empty($_GET['text'])) { |
||
| 28 | $widgetCode = explode(':', preg_replace('!^{WIDGET:!isU', '', preg_replace('!}$!isU', '', urldecode($_GET['text'])))); |
||
| 29 | $text = 'Виджет: '; |
||
| 30 | $widget = false; //Widget::get($widgetCode[0], 'widget_filename'); |
||
| 31 | |||
| 32 | if ($widget) { |
||
| 33 | $text .= $widget->widget_name . "\n"; |
||
| 34 | $i = 1; |
||
| 35 | if (isset($widgetCode[$i]) && $widget->widget_params) { |
||
| 36 | $params = json_decode($widget->widget_params, true); |
||
| 37 | if ($params) { |
||
| 38 | foreach ($params as $param) { |
||
| 39 | if (!isset($widgetCode[$i])) |
||
| 40 | break; |
||
| 41 | if ($param['type'] == 'select') { |
||
| 42 | $item = $param['model']::get($widgetCode[$i++]); |
||
| 43 | if ($item) { |
||
| 44 | $text .= $param['name'] . ': ' . $item->$param['showCol'] . "\n"; |
||
| 45 | } else { |
||
| 46 | $text .= $widgetCode[$i - 1]; |
||
| 47 | } |
||
| 48 | } else { |
||
| 49 | $value = $widgetCode[$i++]; |
||
| 50 | if (mb_strlen($value, 'utf-8') > 50) { |
||
| 51 | $value = mb_substr($value, 0, 50) . '...'; |
||
| 52 | } |
||
| 53 | $text .= $param['name'] . ': ' . $value . "\n"; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | } |
||
| 57 | } else { |
||
| 58 | unset($widgetCode[0]); |
||
| 59 | foreach ($widgetCode as $item) { |
||
| 60 | $text .= $item . "\n"; |
||
| 61 | } |
||
| 62 | } |
||
| 63 | } else { |
||
| 64 | foreach ($widgetCode as $item) { |
||
| 65 | $text .= $item . "\n"; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | } else { |
||
| 69 | $text = 'text not defined'; |
||
| 70 | } |
||
| 71 | |||
| 72 | header('Content-type: image/png'); |
||
| 73 | // шрифт |
||
| 74 | $font = dirname(__FILE__) . '/../fonts/Cousine/Cousine-Regular.ttf'; |
||
| 75 | // вычисляем сколько места займёт текст |
||
| 76 | $bbox = imageftbbox(10, 0, $font, $text); |
||
| 77 | |||
| 78 | |||
| 79 | |||
| 80 | |||
| 81 | $width = abs($bbox[0]) + abs($bbox[2]); // distance from left to right |
||
| 82 | $height = abs($bbox[1]) + abs($bbox[5]); // distance from top to bottom |
||
| 83 | // размер изображения |
||
| 84 | $img = imagecreatetruecolor($width + 10, $height + 10); |
||
| 85 | // цвет текста |
||
| 86 | $black = imagecolorallocate($img, 255, 255, 255); |
||
| 87 | |||
| 88 | // цвет фона |
||
| 89 | $bg = imagecolorallocate($img, 85, 101, 115); |
||
| 90 | imagefilledrectangle($img, 0, 0, $width + 10, $height + 10, $bg); |
||
| 91 | |||
| 92 | // вычисляем координаты для центрирования |
||
| 93 | $x = (imagesx($img) - $bbox[4]) / 2; |
||
| 94 | $y = (imagesy($img) - $bbox[5] - $bbox[3]) / 2; |
||
| 95 | |||
| 96 | // добавляем текст на изображение |
||
| 97 | imagefttext($img, 10, 0, $x, $y, $black, $font, $text); |
||
| 98 | |||
| 99 | // выводим изображение |
||
| 100 | imagepng($img); |
||
| 101 | // освобождаем память |
||
| 102 | imagedestroy($img); |
||
| 103 | } |
||
| 104 | |||
| 106 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: