| Conditions | 10 |
| Paths | 36 |
| Total Lines | 212 |
| Code Lines | 153 |
| Lines | 0 |
| Ratio | 0 % |
| 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 |
||
| 152 | private function replaceString($string) |
||
| 153 | {
|
||
| 154 | if ($this->config[ 'allowPhpGlobals' ] === false) {
|
||
| 155 | $string = str_replace( |
||
| 156 | [ |
||
| 157 | '{{$GLOBALS}}',
|
||
| 158 | '{{$GLOBALS[%%]}}',
|
||
| 159 | '{{$_SERVER}}',
|
||
| 160 | '{{$_SERVER[%%]}}',
|
||
| 161 | '{{$_GET}}',
|
||
| 162 | '{{$_GET[%%]}}',
|
||
| 163 | '{{$_POST}}',
|
||
| 164 | '{{$_POST[%%]}}',
|
||
| 165 | '{{$_FILES}}',
|
||
| 166 | '{{$_FILES[%%]}}',
|
||
| 167 | '{{$_COOKIE}}',
|
||
| 168 | '{{$_COOKIE[%%]}}',
|
||
| 169 | '{{$_SESSION}}',
|
||
| 170 | '{{$_SESSION[%%]}}',
|
||
| 171 | '{{$_REQUEST}}',
|
||
| 172 | '{{$_REQUEST[%%]}}',
|
||
| 173 | '{{$_ENV}}',
|
||
| 174 | '{{$_ENV[%%]}}',
|
||
| 175 | |||
| 176 | // with spaces |
||
| 177 | '{{ $GLOBALS }}',
|
||
| 178 | '{{ $GLOBALS[%%] }}',
|
||
| 179 | '{{ $GLOBALS.%% }}',
|
||
| 180 | '{{ $_SERVER }}',
|
||
| 181 | '{{ $_SERVER[%%] }}',
|
||
| 182 | '{{ $_SERVER.%% }}',
|
||
| 183 | '{{ $_GET }}',
|
||
| 184 | '{{ $_GET[%%] }}',
|
||
| 185 | '{{ $_GET.%% }}',
|
||
| 186 | '{{ $_POST }}',
|
||
| 187 | '{{ $_POST[%%] }}',
|
||
| 188 | '{{ $_POST.%% }}',
|
||
| 189 | '{{ $_FILES }}',
|
||
| 190 | '{{ $_FILES[%%] }}',
|
||
| 191 | '{{ $_FILES.%% }}',
|
||
| 192 | '{{ $_COOKIE }}',
|
||
| 193 | '{{ $_COOKIE[%%] }}',
|
||
| 194 | '{{ $_COOKIE.%% }}',
|
||
| 195 | '{{ $_SESSION }}',
|
||
| 196 | '{{ $_SESSION[%%] }}',
|
||
| 197 | '{{ $_SESSION.%% }}',
|
||
| 198 | '{{ $_REQUEST }}',
|
||
| 199 | '{{ $_REQUEST[%%] }}',
|
||
| 200 | '{{ $_REQUEST.%% }}',
|
||
| 201 | '{{ $_ENV }}',
|
||
| 202 | '{{ $_ENV[%%] }}',
|
||
| 203 | '{{ $_ENV.%% }}',
|
||
| 204 | ], |
||
| 205 | '', |
||
| 206 | $string |
||
| 207 | ); |
||
| 208 | } |
||
| 209 | |||
| 210 | // php logical codes |
||
| 211 | $logicalCodes = [ |
||
| 212 | '@if(%%)' => '<?php if(\1): ?>', |
||
| 213 | '@elseif(%%)' => '<?php elseif(\1): ?>', |
||
| 214 | '@endif' => '<?php endif; ?>', |
||
| 215 | '@else' => '<?php else: ?>', |
||
| 216 | '@unless(%%)' => '<?php if(\1): ?>', |
||
| 217 | '@endunless' => '<?php endif; ?>', |
||
| 218 | |||
| 219 | // with spaces |
||
| 220 | '@if( %% )' => '<?php if(\1): ?>', |
||
| 221 | '@elseif( %% )' => '<?php elseif(\1): ?>', |
||
| 222 | '@unless( %% )' => '<?php if(\1): ?>', |
||
| 223 | '@if (%%)' => '<?php if(\1): ?>', |
||
| 224 | '@elseif (%%)' => '<?php elseif(\1): ?>', |
||
| 225 | '@unless (%%)' => '<?php if(\1): ?>', |
||
| 226 | '@if ( %% )' => '<?php if(\1): ?>', |
||
| 227 | '@elseif ( %% )' => '<?php elseif(\1): ?>', |
||
| 228 | '@unless ( %% )' => '<?php if(\1): ?>', |
||
| 229 | ]; |
||
| 230 | |||
| 231 | // php loop codes |
||
| 232 | $loopCodes = [ |
||
| 233 | '@foreach(%%)' => '<?php foreach(\1): ?>', |
||
| 234 | '@endforeach' => '<?php endforeach; ?>', |
||
| 235 | '@for(%%)' => '<?php for(\1): ?>', |
||
| 236 | '@endfor' => '<?php endfor; ?>', |
||
| 237 | '@while(%%)' => '<?php while(\1): ?>', |
||
| 238 | '@endwhile' => '<?php endwhile; ?>', |
||
| 239 | '@continue' => '<?php continue; ?>', |
||
| 240 | '@break' => '<?php break; ?>', |
||
| 241 | |||
| 242 | // with spaces |
||
| 243 | '@foreach (%%)' => '<?php foreach(\1): ?>', |
||
| 244 | '@for (%%)' => '<?php for(\1): ?>', |
||
| 245 | '@while (%%)' => '<?php while(\1): ?>', |
||
| 246 | ]; |
||
| 247 | |||
| 248 | // php function codes |
||
| 249 | $functionsCodes = [ |
||
| 250 | '@lang(%%)' => '<?php echo $language->getLine(\1); ?>', |
||
| 251 | '@include(%%)' => '<?php echo $this->parseFile(\1); ?>', |
||
| 252 | '@include(%%, %%)' => '<?php echo $this->parseFile(\1, \2); ?>', |
||
| 253 | '@yield(%%)' => '<?php echo $this->sections[\1]; ?>', |
||
| 254 | '@each(%%, %%, %%)' => '<?php echo $this->parsePartials(\1, \2, \3); ?>', |
||
| 255 | '@extends(%%)' => '@extends not supported', |
||
| 256 | '@choice(%%,%%)' => '@choice not supported', |
||
| 257 | |||
| 258 | // with spaces |
||
| 259 | '@lang (%%)' => '<?php echo $language->getLine(\1); ?>', |
||
| 260 | '@include (%%)' => '<?php echo $this->parseFile(\1); ?>', |
||
| 261 | '@include (%%, %%)' => '<?php echo $this->parseFile(\1, \2); ?>', |
||
| 262 | '@yield (%%)' => '<?php echo $this->sections[\1]; ?>', |
||
| 263 | '@each (%%, %%, %%)' => '<?php echo $this->parsePartials(\1, \2, \3); ?>', |
||
| 264 | '@extends (%%)' => '@extends not supported', |
||
| 265 | '@choice (%%,%%)' => '@choice not supported', |
||
| 266 | '@lang( %% )' => '<?php echo $language->getLine(\1); ?>', |
||
| 267 | '@include( %% )' => '<?php echo $this->parseFile(\1); ?>', |
||
| 268 | '@include( %%, %% )' => '<?php echo $this->parseFile(\1, \2); ?>', |
||
| 269 | '@yield( %% )' => '<?php echo $this->sections[\1]; ?>', |
||
| 270 | '@each( %%, %%, %% )' => '<?php echo $this->parsePartials(\1, \2, \3); ?>', |
||
| 271 | '@extends( %% )' => '@extends not supported', |
||
| 272 | '@choice( %%,%% )' => '@choice not supported', |
||
| 273 | ]; |
||
| 274 | |||
| 275 | if ($this->config[ 'allowPhpFunctions' ] === false) {
|
||
| 276 | $functionsCodes[ '@%%(%%)' ] = ''; |
||
| 277 | } elseif (is_array($this->config[ 'allowPhpFunctions' ]) AND count( |
||
| 278 | $this->config[ 'allowPhpFunctions' ] |
||
| 279 | ) > 0 |
||
| 280 | ) {
|
||
| 281 | foreach ($this->config[ 'allowPhpFunctions' ] as $functionName) {
|
||
| 282 | $functionsCodes[ '@' . $functionName . '(%%)' ] = '<?php echo ' . $functionName . '(\1); ?>'; |
||
| 283 | } |
||
| 284 | } |
||
| 285 | |||
| 286 | // php variables codes |
||
| 287 | $variablesCodes = [ |
||
| 288 | '{{%% ? %% : %%}}' => '<?php echo (\1 ? \2 : \3); ?>',
|
||
| 289 | '{{%% or %%}}' => '<?php echo ( empty(\1) ? \2 : \1 ); ?>',
|
||
| 290 | '{{%% || %%}}' => '<?php echo ( empty(\1) ? \2 : \1 ); ?>',
|
||
| 291 | '{{$%%->%%(%%)}}' => '<?php echo $\1->\2(\3); ?>',
|
||
| 292 | '{{$%%->%%}}' => '<?php echo @$\1->\2; ?>',
|
||
| 293 | '{{$%%[%%]}}' => '<?php echo @$\1[\2]; ?>',
|
||
| 294 | '{{$%%.%%}}' => '<?php echo @$\1[\2]; ?>',
|
||
| 295 | '{{$%% = %%}}' => '<?php $\1 = \2; ?>',
|
||
| 296 | '{{$%%++}}' => '<?php $\1++; ?>',
|
||
| 297 | '{{$%%--}}' => '<?php $\1--; ?>',
|
||
| 298 | '{{$%%}}' => '<?php echo (!empty($\1) ? $\1 : ""); ?>',
|
||
| 299 | '{{/*}}' => '<?php /*',
|
||
| 300 | '{{*/}}' => '*/ ?>',
|
||
| 301 | '{{%%}}' => '<?php echo (\1); ?>',
|
||
| 302 | '{{!! $%% !!}}' => '<?php echo htmlentities($\1, ENT_HTML5); ?>',
|
||
| 303 | '{{-- %% --}}' => '',
|
||
| 304 | |||
| 305 | // with spaces |
||
| 306 | '{{ %% ? %% : %% }}' => '<?php echo (\1 ? \2 : \3); ?>',
|
||
| 307 | '{{ %% or %% }}' => '<?php echo ( empty(\1) ? \2 : \1 ); ?>',
|
||
| 308 | '{{ %% || %% }}' => '<?php echo ( empty(\1) ? \2 : \1 ); ?>',
|
||
| 309 | '{{ $%%->%%(%%) }}' => '<?php echo $\1->\2(\3); ?>',
|
||
| 310 | '{{ $%%->%% }}' => '<?php echo @$\1->\2; ?>',
|
||
| 311 | '{{ $%%[%%] }}' => '<?php echo @$\1[\2]; ?>',
|
||
| 312 | '{{ $%%.%% }}' => '<?php echo @$\1[\2]; ?>',
|
||
| 313 | '{{ $%% = %% }}' => '<?php $\1 = \2; ?>',
|
||
| 314 | '{{ $%%++ }}' => '<?php $\1++; ?>',
|
||
| 315 | '{{ $%%-- }}' => '<?php $\1--; ?>',
|
||
| 316 | '{{ $%% }}' => '<?php echo (!empty($\1) ? $\1 : ""); ?>',
|
||
| 317 | '{{ /* }}' => '<?php /*',
|
||
| 318 | '{{ */ }}' => '*/ ?>',
|
||
| 319 | '{{ %% }}' => '<?php echo (\1); ?>',
|
||
| 320 | ]; |
||
| 321 | |||
| 322 | if ($this->config[ 'allowPhpConstants' ] === true) {
|
||
| 323 | $constantsVariables = get_defined_constants(true); |
||
| 324 | |||
| 325 | if ( ! empty($constantsVariables[ 'user' ])) {
|
||
| 326 | foreach ($constantsVariables[ 'user' ] as $constant => $value) {
|
||
| 327 | $variablesCodes[ '{{ ' . $constant . ' }}' ] = '<?php echo ' . $constant . '; ?>';
|
||
| 328 | } |
||
| 329 | } |
||
| 330 | } |
||
| 331 | |||
| 332 | $phpCodes = array_merge($logicalCodes, $loopCodes, $functionsCodes, $variablesCodes); |
||
| 333 | |||
| 334 | $patterns = $replace = []; |
||
| 335 | foreach ($phpCodes as $tplCode => $phpCode) {
|
||
| 336 | $patterns[] = '#' . str_replace('%%', '(.+)', preg_quote($tplCode, '#')) . '#U';
|
||
| 337 | $replace[] = $phpCode; |
||
| 338 | } |
||
| 339 | |||
| 340 | /*replace our pseudo language in template with php code*/ |
||
| 341 | $string = preg_replace($patterns, $replace, $string); |
||
| 342 | |||
| 343 | extract($this->vars); |
||
| 344 | |||
| 345 | /* |
||
| 346 | * Buffer the output |
||
| 347 | * |
||
| 348 | * We buffer the output for two reasons: |
||
| 349 | * 1. Speed. You get a significant speed boost. |
||
| 350 | * 2. So that the final rendered template can be post-processed by |
||
| 351 | * the output class. Why do we need post processing? For one thing, |
||
| 352 | * in order to show the elapsed page load time. Unless we can |
||
| 353 | * intercept the content right before it's sent to the browser and |
||
| 354 | * then stop the timer it won't be accurate. |
||
| 355 | */ |
||
| 356 | ob_start(); |
||
| 357 | |||
| 358 | echo eval('?>' . preg_replace('/;*\s*\?>/', '; ?>', $string));
|
||
|
|
|||
| 359 | |||
| 360 | $output = ob_get_contents(); |
||
| 361 | @ob_end_clean(); |
||
| 362 | |||
| 363 | return $output; |
||
| 364 | } |
||
| 444 | } |