Skip to content

Commit e5a412c

Browse files
mychidarkogithub-actions[bot]
authored andcommitted
chore: fix styling
1 parent 2c8bc19 commit e5a412c

File tree

2 files changed

+50
-34
lines changed

2 files changed

+50
-34
lines changed

src/Veins.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Leaf Veins
99
* ---
1010
* Simple, fast and powerful PHP template engine.
11-
*
11+
*
1212
* @package Leaf\Veins
1313
* @version 2.0.0
1414
* @since 1.0.0

src/Veins/Parser.php

Lines changed: 49 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ class Parser
2020
protected $tags = [
2121
'loop' => [
2222
'({loop.*?})',
23-
'/{loop="(?<variable>\${0,1}[^"]*)"(?: as (?<key>\$.*?)(?: => (?<value>\$.*?)){0,1}){0,1}}/'
23+
'/{loop="(?<variable>\${0,1}[^"]*)"(?: as (?<key>\$.*?)(?: => (?<value>\$.*?)){0,1}){0,1}}/',
2424
],
2525
'loop_close' => ['({\/loop})', '/{\/loop}/'],
2626
'loop_break' => ['({break})', '/{break}/'],
2727
'loop_continue' => ['({continue})', '/{continue}/'],
2828
'foreach' => [
2929
'({foreach.*?})',
30-
'/{foreach="(?<variable>\${0,1}[^"]*)"(?: as (?<key>\$.*?)(?: => (?<value>\$.*?)){0,1}){0,1}}/'
30+
'/{foreach="(?<variable>\${0,1}[^"]*)"(?: as (?<key>\$.*?)(?: => (?<value>\$.*?)){0,1}){0,1}}/',
3131
],
3232
'foreach_close' => ['({\/foreach})', '/{\/foreach}/'],
3333
'foreach_break' => ['({break})', '/{break}/'],
@@ -45,7 +45,7 @@ class Parser
4545
'include' => ['({include.*?})', '/{include="([^"]*)"}/'],
4646
'function' => [
4747
'({function.*?})',
48-
'/{function="([a-zA-Z_][a-zA-Z_0-9\:]*)(\(.*\)){0,1}"}/'
48+
'/{function="([a-zA-Z_][a-zA-Z_0-9\:]*)(\(.*\)){0,1}"}/',
4949
],
5050
'ternary' => ['({.[^{?}]*?\?.*?\:.*?})', '/{(.[^{?}]*?)\?(.*?)\:(.*?)}/'],
5151
'variable' => ['({\$.*?})', '/{(\$.*?)}/'],
@@ -70,7 +70,7 @@ class Parser
7070
'posix_getpwuid', 'posix_kill', 'posix_mkfifo', 'posix_setpgid',
7171
'posix_setsid', 'posix_setuid', 'posix_uname', 'proc_close',
7272
'proc_get_status', 'proc_nice', 'proc_open', 'proc_terminate',
73-
'syslog', 'xmlrpc_entity_decode'
73+
'syslog', 'xmlrpc_entity_decode',
7474
];
7575

7676
public function __construct(array $config)
@@ -98,6 +98,7 @@ public static function checkTemplate(array $config, string $template)
9898
filemtime($template) > filemtime($parsedTemplate)
9999
) {
100100
$parser = new self($config);
101+
101102
return $parser->parse($template, $parsedTemplate);
102103
}
103104

@@ -126,39 +127,44 @@ public function parse(string $template, string $parsedTemplate): string
126127
$codeSplit = preg_split("/" . implode("|", $tagSplit) . "/", $template, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY);
127128

128129
//variables initialization
129-
$parsedCode = $commentIsOpen = $ignoreIsOpen = NULL;
130+
$parsedCode = $commentIsOpen = $ignoreIsOpen = null;
130131
$openIf = $loopLevel = 0;
131132

132133
// if the template is not empty
133-
if ($codeSplit)
134+
if ($codeSplit) {
134135

135136
//read all parsed code
136137
foreach ($codeSplit as $html) {
137138

138139
//close ignore tag
139-
if (!$commentIsOpen && preg_match($tagMatch['ignore_close'], $html))
140-
$ignoreIsOpen = FALSE;
140+
if (!$commentIsOpen && preg_match($tagMatch['ignore_close'], $html)) {
141+
$ignoreIsOpen = false;
142+
}
141143

142144
//code between tag ignore id deleted
143145
elseif ($ignoreIsOpen) {
144146
//ignore the code
145147
}
146148

147149
//close no parse tag
148-
elseif (preg_match($tagMatch['noparse_close'], $html))
149-
$commentIsOpen = FALSE;
150+
elseif (preg_match($tagMatch['noparse_close'], $html)) {
151+
$commentIsOpen = false;
152+
}
150153

151154
//code between tag noparse is not compiled
152-
elseif ($commentIsOpen)
155+
elseif ($commentIsOpen) {
153156
$parsedCode .= $html;
157+
}
154158

155159
//ignore
156-
elseif (preg_match($tagMatch['ignore'], $html))
157-
$ignoreIsOpen = TRUE;
160+
elseif (preg_match($tagMatch['ignore'], $html)) {
161+
$ignoreIsOpen = true;
162+
}
158163

159164
//noparse
160-
elseif (preg_match($tagMatch['noparse'], $html))
161-
$commentIsOpen = TRUE;
165+
elseif (preg_match($tagMatch['noparse'], $html)) {
166+
$commentIsOpen = true;
167+
}
162168

163169
//include tag
164170
elseif (preg_match($tagMatch['include'], $html, $matches)) {
@@ -194,7 +200,7 @@ public function parse(string $template, string $parsedTemplate): string
194200
$loopLevel++;
195201

196202
//replace the variable in the loop
197-
$var = $this->varReplace($matches['variable'], $loopLevel - 1, $escape = FALSE);
203+
$var = $this->varReplace($matches['variable'], $loopLevel - 1, $escape = false);
198204
if (preg_match('#\(#', $var)) {
199205
$newvar = "\$newvar{$loopLevel}";
200206
$assignNewVar = "$newvar=$var;";
@@ -260,7 +266,7 @@ public function parse(string $template, string $parsedTemplate): string
260266
$loopLevel++;
261267

262268
//replace the variable in the foreach
263-
$var = $this->varReplace($matches['variable'], $loopLevel - 1, $escape = FALSE);
269+
$var = $this->varReplace($matches['variable'], $loopLevel - 1, $escape = false);
264270
if (preg_match('#\(#', $var)) {
265271
$newvar = "\$newvar{$loopLevel}";
266272
$assignNewVar = "$newvar=$var;";
@@ -335,7 +341,7 @@ public function parse(string $template, string $parsedTemplate): string
335341
$this->blackList($condition);
336342

337343
//variable substitution into condition (no delimiter into the condition)
338-
$parsedCondition = $this->varReplace($condition, $loopLevel, $escape = FALSE);
344+
$parsedCondition = $this->varReplace($condition, $loopLevel, $escape = false);
339345

340346
//if code
341347
$parsedCode .= "<?php if( $parsedCondition ){ ?>";
@@ -354,7 +360,7 @@ public function parse(string $template, string $parsedTemplate): string
354360
$this->blackList($condition);
355361

356362
//variable substitution into condition (no delimiter into the condition)
357-
$parsedCondition = $this->varReplace($condition, $loopLevel, $escape = FALSE);
363+
$parsedCondition = $this->varReplace($condition, $loopLevel, $escape = false);
358364

359365
//elseif code
360366
$parsedCode .= "<?php }elseif( $parsedCondition ){ ?>";
@@ -404,10 +410,11 @@ public function parse(string $template, string $parsedTemplate): string
404410
$function = $matches[1];
405411

406412
// var replace
407-
if (isset($matches[2]))
408-
$parsedFunction = $function . $this->varReplace($matches[2], $loopLevel, $escape = FALSE, $echo = FALSE);
409-
else
413+
if (isset($matches[2])) {
414+
$parsedFunction = $function . $this->varReplace($matches[2], $loopLevel, $escape = false, $echo = false);
415+
} else {
410416
$parsedFunction = $function . "()";
417+
}
411418

412419
// check black list
413420
$this->blackList($parsedFunction);
@@ -418,13 +425,13 @@ public function parse(string $template, string $parsedTemplate): string
418425

419426
//ternary
420427
elseif (preg_match($tagMatch['ternary'], $html, $matches)) {
421-
$parsedCode .= "<?php echo " . '(' . $this->varReplace($matches[1], $loopLevel, $escape = TRUE, $echo = FALSE) . '?' . $this->varReplace($matches[2], $loopLevel, $escape = TRUE, $echo = FALSE) . ':' . $this->varReplace($matches[3], $loopLevel, $escape = TRUE, $echo = FALSE) . ')' . "; ?>";
428+
$parsedCode .= "<?php echo " . '(' . $this->varReplace($matches[1], $loopLevel, $escape = true, $echo = false) . '?' . $this->varReplace($matches[2], $loopLevel, $escape = true, $echo = false) . ':' . $this->varReplace($matches[3], $loopLevel, $escape = true, $echo = false) . ')' . "; ?>";
422429
}
423430

424431
//variables
425432
elseif (preg_match($tagMatch['variable'], $html, $matches)) {
426433
//variables substitution (es. {$title})
427-
$parsedCode .= "<?php " . $this->varReplace($matches[1], $loopLevel, $escape = TRUE, $echo = TRUE) . "; ?>";
434+
$parsedCode .= "<?php " . $this->varReplace($matches[1], $loopLevel, $escape = true, $echo = true) . "; ?>";
428435
}
429436

430437

@@ -436,7 +443,7 @@ public function parse(string $template, string $parsedTemplate): string
436443
// registered tags
437444
else {
438445

439-
$found = FALSE;
446+
$found = false;
440447
foreach ($this->config['customTags'] as $tags => $array) {
441448
if (preg_match_all('/' . $array['parse'] . '/', $html, $matches)) {
442449
$found = true;
@@ -449,6 +456,7 @@ public function parse(string $template, string $parsedTemplate): string
449456
}
450457
}
451458
}
459+
}
452460

453461

454462
if ($openIf > 0) {
@@ -461,6 +469,7 @@ public function parse(string $template, string $parsedTemplate): string
461469
if ($loopLevel > 0) {
462470
$trace = debug_backtrace();
463471
$caller = array_shift($trace);
472+
464473
throw new \Exception("Error! You need to close the {loop} tag in the string, loaded by {$caller['file']} at line {$caller['line']}");
465474
}
466475

@@ -474,12 +483,13 @@ public function parse(string $template, string $parsedTemplate): string
474483
return $parsedTemplate;
475484
}
476485

477-
protected function varReplace($html, $loopLevel = NULL, $escape = TRUE, $echo = FALSE)
486+
protected function varReplace($html, $loopLevel = null, $escape = true, $echo = false)
478487
{
479488

480489
// change variable name if loop level
481-
if (!empty($loopLevel))
482-
$html = preg_replace(array('/(\$key)\b/', '/(\$value)\b/', '/(\$counter)\b/'), array('${1}' . $loopLevel, '${1}' . $loopLevel, '${1}' . $loopLevel), $html);
490+
if (!empty($loopLevel)) {
491+
$html = preg_replace(['/(\$key)\b/', '/(\$value)\b/', '/(\$counter)\b/'], ['${1}' . $loopLevel, '${1}' . $loopLevel, '${1}' . $loopLevel], $html);
492+
}
483493

484494
// if it is a variable
485495
if (preg_match_all('/(\$[a-z_A-Z][^\s]*)/', $html, $matches)) {
@@ -499,13 +509,15 @@ protected function varReplace($html, $loopLevel = NULL, $escape = TRUE, $echo =
499509
if (!preg_match('/\$.*=.*/', $html)) {
500510

501511
// escape character
502-
if ($this->config['autoEscape'] && $escape)
512+
if ($this->config['autoEscape'] && $escape) {
503513
//$html = "htmlspecialchars( $html )";
504514
$html = "htmlspecialchars( $html, ENT_COMPAT, '" . $this->config['charset'] . "', FALSE )";
515+
}
505516

506517
// if is an assignment it doesn't add echo
507-
if ($echo)
518+
if ($echo) {
508519
$html = "echo " . $html;
520+
}
509521
}
510522
}
511523

@@ -537,19 +549,22 @@ protected function modifierReplace($html)
537549

538550
protected function blackList($html)
539551
{
540-
if (!$this->config['sandbox'] || !$this->blackList)
552+
if (!$this->config['sandbox'] || !$this->blackList) {
541553
return true;
554+
}
542555

543-
if (empty($this->config['blackListPreg']))
556+
if (empty($this->config['blackListPreg'])) {
544557
$this->config['blackListPreg'] = '#[\W\s]*' . implode('[\W\s]*|[\W\s]*', $this->blackList) . '[\W\s]*#';
558+
}
545559

546560
// check if the function is in the black list (or not in white list)
547561
if (preg_match($this->config['blackListPreg'], $html, $match)) {
548562
// find the line of the error
549563
$line = 0;
550564
$rows = explode("\n", 'code should be here');
551-
while (!strpos($rows[$line], $html) && $line + 1 < count($rows))
565+
while (!strpos($rows[$line], $html) && $line + 1 < count($rows)) {
552566
$line++;
567+
}
553568

554569
throw new \Exception('Syntax ' . $match[0] . ' not allowed in template: ' . '$templatePath' . ' at line ' . $line);
555570
}
@@ -558,6 +573,7 @@ protected function blackList($html)
558573
protected function conReplace($html)
559574
{
560575
$html = $this->modifierReplace($html);
576+
561577
return $html;
562578
}
563579

0 commit comments

Comments
 (0)