| Total Complexity | 43 |
| Total Lines | 311 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like CreateSmartyCode often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CreateSmartyCode, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 32 | class CreateSmartyCode |
||
| 33 | { |
||
| 34 | /** |
||
| 35 | * @public function constructor |
||
| 36 | * @param null |
||
| 37 | */ |
||
| 38 | public function __construct() |
||
| 39 | { |
||
| 40 | } |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @static function getInstance |
||
| 44 | * @param null |
||
| 45 | * @return Files\CreateSmartyCode |
||
| 46 | */ |
||
| 47 | public static function getInstance() |
||
| 48 | { |
||
| 49 | static $instance = false; |
||
| 50 | if (!$instance) { |
||
| 51 | $instance = new self(); |
||
| 52 | } |
||
| 53 | |||
| 54 | return $instance; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @public function getSmartyTag |
||
| 59 | * |
||
| 60 | * @param string $tag |
||
| 61 | * @param array $attributes |
||
| 62 | * @param string $content |
||
| 63 | * |
||
| 64 | * @param string $t |
||
| 65 | * @return string |
||
| 66 | */ |
||
| 67 | public function getSmartyTag($tag = '', $attributes = [], $content = '', $t = '') |
||
| 68 | { |
||
| 69 | if (empty($attributes)) { |
||
| 70 | $attributes = []; |
||
| 71 | } |
||
| 72 | $attr = $this->getAttributes($attributes); |
||
| 73 | $ret = "{$t}<{{$tag}{$attr}}>{$content}<{/{$tag}}>"; |
||
| 74 | |||
| 75 | return $ret; |
||
| 76 | } |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @private function setAttributes |
||
| 80 | * @param array $attributes |
||
| 81 | * |
||
| 82 | * @return string |
||
| 83 | */ |
||
| 84 | private function getAttributes($attributes) |
||
| 85 | { |
||
| 86 | $str = ''; |
||
| 87 | foreach ($attributes as $name => $value) { |
||
| 88 | if ('_' !== $name) { |
||
| 89 | $str .= ' ' . $name . '="' . $value . '"'; |
||
| 90 | } |
||
| 91 | } |
||
| 92 | |||
| 93 | return $str; |
||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @public function getSmartyEmpty |
||
| 98 | * @param string $empty |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function getSmartyEmpty($empty = '') |
||
| 103 | { |
||
| 104 | return (string)$empty; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @public function getSmartyComment |
||
| 109 | * @param string $comment |
||
| 110 | * @param string $t |
||
| 111 | * @param string $n |
||
| 112 | * @return string |
||
| 113 | */ |
||
| 114 | public function getSmartyComment($comment = '', $t = '', $n = "\n") |
||
| 115 | { |
||
| 116 | return "{$t}<{* {$comment} *}>{$n}"; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @public function getSmartyNoSimbol |
||
| 121 | * @param string $noSimbol |
||
| 122 | * @param string $t |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | public function getSmartyNoSimbol($noSimbol = '', $t = '') |
||
| 126 | { |
||
| 127 | return "{$t}<{{$noSimbol}}>"; |
||
| 128 | } |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @public function getSmartyConst |
||
| 132 | * @param string $language |
||
| 133 | * @param mixed $const |
||
| 134 | * @param string $t |
||
| 135 | * @param string $n |
||
| 136 | * @return string |
||
| 137 | */ |
||
| 138 | public function getSmartyConst($language, $const, $t = '', $n = '') |
||
| 139 | { |
||
| 140 | return "{$t}<{\$smarty.const.{$language}{$const}}>{$n}"; |
||
| 141 | } |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @public function getSmartySingleVar |
||
| 145 | * @param string $var |
||
| 146 | * @param string $t |
||
| 147 | * @param string $n |
||
| 148 | * @return string |
||
| 149 | */ |
||
| 150 | public function getSmartySingleVar($var, $t = '', $n = "") |
||
| 153 | } |
||
| 154 | |||
| 155 | /** |
||
| 156 | * @public function getSmartyDoubleVar |
||
| 157 | * @param string $leftVar |
||
| 158 | * @param string $rightVar |
||
| 159 | * @param string $t |
||
| 160 | * @param string $n |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | public function getSmartyDoubleVar($leftVar, $rightVar, $t = '', $n = "") |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @public function getSmartyIncludeFile |
||
| 170 | * @param $moduleDirname |
||
| 171 | * @param string $fileName |
||
| 172 | * @param bool $admin |
||
| 173 | * |
||
| 174 | * @param bool $q |
||
| 175 | * @param string $t |
||
| 176 | * @param string $n |
||
| 177 | * @param string $attributes |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function getSmartyIncludeFile($moduleDirname, $fileName = 'header', $admin = false, $q = false, $t = '', $n = "\n", $attributes = '') |
||
| 181 | { |
||
| 182 | $ret = ''; |
||
| 183 | if (!$admin && !$q) { |
||
| 184 | $ret = "{$t}<{include file='db:{$moduleDirname}_{$fileName}.tpl' {$attributes}}>{$n}"; |
||
| 185 | } elseif ($admin && !$q) { |
||
| 186 | $ret = "{$t}<{include file='db:{$moduleDirname}_admin_{$fileName}.tpl' {$attributes}}>{$n}"; |
||
| 187 | } elseif (!$admin && $q) { |
||
| 188 | $ret = "{$t}<{includeq file='db:{$moduleDirname}_{$fileName}.tpl' {$attributes}}>{$n}"; |
||
| 189 | } elseif ($admin && $q) { |
||
| 190 | $ret = "{$t}<{includeq file='db:{$moduleDirname}_admin_{$fileName}.tpl' {$attributes}}>{$n}"; |
||
| 191 | } |
||
| 192 | |||
| 193 | return $ret; |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * @public function getSmartyIncludeFileListSection |
||
| 198 | * @param $moduleDirname |
||
| 199 | * @param $fileName |
||
| 200 | * @param $itemName |
||
| 201 | * @param $arrayName |
||
| 202 | * @param string $t |
||
| 203 | * @param string $n |
||
| 204 | * @return string |
||
| 205 | */ |
||
| 206 | public function getSmartyIncludeFileListSection($moduleDirname, $fileName, $itemName, $arrayName, $t = '', $n = '') |
||
| 207 | { |
||
| 208 | return "{$t}<{include file='db:{$moduleDirname}_{$fileName}_list.tpl' {$itemName}=\${$arrayName}[i]}>{$n}"; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * @public function getSmartyIncludeFileListForeach |
||
| 213 | * @param $moduleDirname |
||
| 214 | * @param $fileName |
||
| 215 | * @param $tableFieldName |
||
| 216 | * @param string $t |
||
| 217 | * @param string $n |
||
| 218 | * @return string |
||
| 219 | */ |
||
| 220 | public function getSmartyIncludeFileListForeach($moduleDirname, $fileName, $tableFieldName, $t = '', $n = '') |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * @public function getSmartyConditions |
||
| 227 | * @param string $condition |
||
| 228 | * @param string $operator |
||
| 229 | * @param string $type |
||
| 230 | * @param string $contentIf |
||
| 231 | * @param mixed $contentElse |
||
| 232 | * @param bool $count |
||
| 233 | * @param bool $noSimbol |
||
| 234 | * @param string $t |
||
| 235 | * @param string $n |
||
| 236 | * @param bool $split |
||
| 237 | * @param mixed $default |
||
| 238 | * @return string |
||
| 239 | */ |
||
| 240 | public function getSmartyConditions($condition = '', $operator = '', $type = '', $contentIf = '', $contentElse = false, $count = false, $noSimbol = false, $t = '', $n = "\n", $split = true, $default = 'string') |
||
| 276 | } |
||
| 277 | |||
| 278 | /** |
||
| 279 | * @public function getSmartyForeach |
||
| 280 | * @param string $item |
||
| 281 | * @param string $from |
||
| 282 | * @param string $content |
||
| 283 | * @param string $name |
||
| 284 | * @param string $key |
||
| 285 | * @param string $t |
||
| 286 | * @param string $n |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | public function getSmartyForeach($item = 'item', $from = 'from', $content = 'content', $name = '', $key = '', $t = '', $n = "\n") |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @public function getSmartyForeachQuery |
||
| 302 | * @param string $item |
||
| 303 | * @param string $from |
||
| 304 | * @param string $content |
||
| 305 | * |
||
| 306 | * @param string $loop |
||
| 307 | * @param string $key |
||
| 308 | * @param string $t |
||
| 309 | * @param string $n |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | public function getSmartyForeachQuery($item = 'item', $from = 'from', $content = 'content', $loop = 'loop', $key = '', $t = '', $n = "\n") |
||
| 313 | { |
||
| 314 | $loop = '' != $loop ? " loop={$loop}" : ''; |
||
| 315 | $key = '' != $key ? " key={$key}" : ''; |
||
| 316 | $ret = "{$t}<{foreachq item={$item} from=\${$from}{$key}{$loop}}>{$n}"; |
||
| 317 | $ret .= "{$content}"; |
||
| 318 | $ret .= "{$t}<{/foreachq}>{$n}"; |
||
| 319 | |||
| 320 | return $ret; |
||
| 321 | } |
||
| 322 | |||
| 323 | /** |
||
| 324 | * @public function getSmartySection |
||
| 325 | * @param string $name |
||
| 326 | * @param string $loop |
||
| 327 | * @param string $content |
||
| 328 | * @param int $start |
||
| 329 | * @param int $step |
||
| 330 | * @param string $t |
||
| 331 | * @param string $n |
||
| 332 | * @return string |
||
| 333 | */ |
||
| 334 | public function getSmartySection($name = 'name', $loop = 'loop', $content = 'content', $start = 0, $step = 0, $t = '', $n = "\n") |
||
| 343 | } |
||
| 344 | } |
||
| 345 |