Merge pull request #10507 from creative-commoners/pulls/4/peg

ENH Add PHP 8.1 safe null-coalescing operators to peg file
This commit is contained in:
Guy Sartorelli 2022-09-15 13:17:39 +12:00 committed by GitHub
commit 52744c7848
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 40 additions and 40 deletions

View File

@ -247,7 +247,7 @@ class SSTemplateParser extends Parser implements TemplateParser
} }
$res['php'] .= ($sub['ArgumentMode'] == 'default') ? $sub['string_php'] : $res['php'] .= ($sub['ArgumentMode'] == 'default') ? $sub['string_php'] :
str_replace('$$FINAL', 'XML_val', $sub['php']); str_replace('$$FINAL', 'XML_val', $sub['php'] ?? '');
} }
/*!* /*!*
@ -356,13 +356,13 @@ class SSTemplateParser extends Parser implements TemplateParser
function InjectionVariables_Argument(&$res, $sub) function InjectionVariables_Argument(&$res, $sub)
{ {
$res['php'] .= str_replace('$$FINAL', 'XML_val', $sub['php']) . ','; $res['php'] .= str_replace('$$FINAL', 'XML_val', $sub['php'] ?? '') . ',';
} }
function InjectionVariables__finalise(&$res) function InjectionVariables__finalise(&$res)
{ {
if (substr($res['php'], -1) == ',') { if (substr($res['php'] ?? '', -1) == ',') {
$res['php'] = substr($res['php'], 0, -1); //remove last comma in the array $res['php'] = substr($res['php'] ?? '', 0, -1); //remove last comma in the array
} }
$res['php'] .= ']'; $res['php'] .= ']';
} }
@ -391,7 +391,7 @@ class SSTemplateParser extends Parser implements TemplateParser
*/ */
function Injection_STR(&$res, $sub) function Injection_STR(&$res, $sub)
{ {
$res['php'] = '$val .= '. str_replace('$$FINAL', 'XML_val', $sub['Lookup']['php']) . ';'; $res['php'] = '$val .= '. str_replace('$$FINAL', 'XML_val', $sub['Lookup']['php'] ?? '') . ';';
} }
/*!* /*!*
@ -456,12 +456,12 @@ class SSTemplateParser extends Parser implements TemplateParser
function Argument_QuotedString(&$res, $sub) function Argument_QuotedString(&$res, $sub)
{ {
$res['ArgumentMode'] = 'string'; $res['ArgumentMode'] = 'string';
$res['php'] = "'" . str_replace("'", "\\'", $sub['String']['text']) . "'"; $res['php'] = "'" . str_replace("'", "\\'", $sub['String']['text'] ?? '') . "'";
} }
function Argument_Lookup(&$res, $sub) function Argument_Lookup(&$res, $sub)
{ {
if (count($sub['LookupSteps']) == 1 && !isset($sub['LookupSteps'][0]['Call']['Arguments'])) { if (count($sub['LookupSteps'] ?? []) == 1 && !isset($sub['LookupSteps'][0]['Call']['Arguments'])) {
$res['ArgumentMode'] = 'default'; $res['ArgumentMode'] = 'default';
$res['lookup_php'] = $sub['php']; $res['lookup_php'] = $sub['php'];
$res['string_php'] = "'".$sub['LookupSteps'][0]['Call']['Method']['text']."'"; $res['string_php'] = "'".$sub['LookupSteps'][0]['Call']['Method']['text']."'";
@ -474,7 +474,7 @@ class SSTemplateParser extends Parser implements TemplateParser
function Argument_FreeString(&$res, $sub) function Argument_FreeString(&$res, $sub)
{ {
$res['ArgumentMode'] = 'string'; $res['ArgumentMode'] = 'string';
$res['php'] = "'" . str_replace("'", "\\'", trim($sub['text'])) . "'"; $res['php'] = "'" . str_replace("'", "\\'", trim($sub['text'] ?? '')) . "'";
} }
/*!* /*!*
@ -491,10 +491,10 @@ class SSTemplateParser extends Parser implements TemplateParser
if (!empty($res['php'])) { if (!empty($res['php'])) {
$res['php'] .= $sub['string_php']; $res['php'] .= $sub['string_php'];
} else { } else {
$res['php'] = str_replace('$$FINAL', 'XML_val', $sub['lookup_php']); $res['php'] = str_replace('$$FINAL', 'XML_val', $sub['lookup_php'] ?? '');
} }
} else { } else {
$res['php'] .= str_replace('$$FINAL', 'XML_val', $sub['php']); $res['php'] .= str_replace('$$FINAL', 'XML_val', $sub['php'] ?? '');
} }
} }
@ -524,7 +524,7 @@ class SSTemplateParser extends Parser implements TemplateParser
$php = ($sub['ArgumentMode'] == 'default' ? $sub['lookup_php'] : $sub['php']); $php = ($sub['ArgumentMode'] == 'default' ? $sub['lookup_php'] : $sub['php']);
// TODO: kinda hacky - maybe we need a way to pass state down the parse chain so // TODO: kinda hacky - maybe we need a way to pass state down the parse chain so
// Lookup_LastLookupStep and Argument_BareWord can produce hasValue instead of XML_val // Lookup_LastLookupStep and Argument_BareWord can produce hasValue instead of XML_val
$res['php'] .= str_replace('$$FINAL', 'hasValue', $php); $res['php'] .= str_replace('$$FINAL', 'hasValue', $php ?? '');
} }
} }
@ -630,7 +630,7 @@ class SSTemplateParser extends Parser implements TemplateParser
function CacheBlockArgument_QuotedString(&$res, $sub) function CacheBlockArgument_QuotedString(&$res, $sub)
{ {
$res['php'] = "'" . str_replace("'", "\\'", $sub['String']['text']) . "'"; $res['php'] = "'" . str_replace("'", "\\'", $sub['String']['text'] ?? '') . "'";
} }
function CacheBlockArgument_Lookup(&$res, $sub) function CacheBlockArgument_Lookup(&$res, $sub)
@ -653,7 +653,7 @@ class SSTemplateParser extends Parser implements TemplateParser
$res['php'] = ''; $res['php'] = '';
} }
$res['php'] .= str_replace('$$FINAL', 'XML_val', $sub['php']); $res['php'] .= str_replace('$$FINAL', 'XML_val', $sub['php'] ?? '');
} }
/*!* /*!*
@ -749,7 +749,7 @@ class SSTemplateParser extends Parser implements TemplateParser
$res['php'] .= 'return $val;' . PHP_EOL; $res['php'] .= 'return $val;' . PHP_EOL;
$res['php'] .= '};' . PHP_EOL; $res['php'] .= '};' . PHP_EOL;
$key = 'sha1($keyExpression())' // Global key $key = 'sha1($keyExpression())' // Global key
. '.\'_' . sha1($sub['php']) // sha of template . '.\'_' . sha1($sub['php'] ?? '') // sha of template
. (isset($res['key']) && $res['key'] ? "_'.sha1(".$res['key'].")" : "'") // Passed key . (isset($res['key']) && $res['key'] ? "_'.sha1(".$res['key'].")" : "'") // Passed key
. ".'_$block'"; // block index . ".'_$block'"; // block index
// Get any condition // Get any condition
@ -782,7 +782,7 @@ class SSTemplateParser extends Parser implements TemplateParser
function OldTPart_QuotedString(&$res, $sub) function OldTPart_QuotedString(&$res, $sub)
{ {
$entity = $sub['String']['text']; $entity = $sub['String']['text'];
if (strpos($entity, '.') === false) { if (strpos($entity ?? '', '.') === false) {
$res['php'] .= "\$scope->XML_val('I18NNamespace').'.$entity'"; $res['php'] .= "\$scope->XML_val('I18NNamespace').'.$entity'";
} else { } else {
$res['php'] .= "'$entity'"; $res['php'] .= "'$entity'";
@ -871,7 +871,7 @@ class SSTemplateParser extends Parser implements TemplateParser
break; break;
default: default:
$res['php'] .= str_replace('$$FINAL', 'obj', $sub['php']) . '->self()'; $res['php'] .= str_replace('$$FINAL', 'obj', $sub['php'] ?? '') . '->self()';
break; break;
} }
} }
@ -909,9 +909,9 @@ class SSTemplateParser extends Parser implements TemplateParser
if ($this->includeDebuggingComments) { // Add include filename comments on dev sites if ($this->includeDebuggingComments) { // Add include filename comments on dev sites
$res['php'] = $res['php'] =
'$val .= \'<!-- include '.addslashes($template).' -->\';'. "\n". '$val .= \'<!-- include '.addslashes($template ?? '').' -->\';'. "\n".
$res['php']. $res['php'].
'$val .= \'<!-- end include '.addslashes($template).' -->\';'. "\n"; '$val .= \'<!-- end include '.addslashes($template ?? '').' -->\';'. "\n";
} }
} }
@ -962,7 +962,7 @@ class SSTemplateParser extends Parser implements TemplateParser
$res['ArgumentCount'] = 1; $res['ArgumentCount'] = 1;
} else { } else {
$res['Arguments'] = $sub['Argument']; $res['Arguments'] = $sub['Argument'];
$res['ArgumentCount'] = count($res['Arguments']); $res['ArgumentCount'] = count($res['Arguments'] ?? []);
} }
} }
@ -971,7 +971,7 @@ class SSTemplateParser extends Parser implements TemplateParser
$blockname = $res['BlockName']['text']; $blockname = $res['BlockName']['text'];
$method = 'ClosedBlock_Handle_'.$blockname; $method = 'ClosedBlock_Handle_'.$blockname;
if (method_exists($this, $method)) { if (method_exists($this, $method ?? '')) {
$res['php'] = $this->$method($res); $res['php'] = $this->$method($res);
} elseif (isset($this->closedBlocks[$blockname])) { } elseif (isset($this->closedBlocks[$blockname])) {
$res['php'] = call_user_func($this->closedBlocks[$blockname], $res); $res['php'] = call_user_func($this->closedBlocks[$blockname], $res);
@ -1053,7 +1053,7 @@ class SSTemplateParser extends Parser implements TemplateParser
$res['ArgumentCount'] = 1; $res['ArgumentCount'] = 1;
} else { } else {
$res['Arguments'] = $sub['Argument']; $res['Arguments'] = $sub['Argument'];
$res['ArgumentCount'] = count($res['Arguments']); $res['ArgumentCount'] = count($res['Arguments'] ?? []);
} }
} }
@ -1062,7 +1062,7 @@ class SSTemplateParser extends Parser implements TemplateParser
$blockname = $res['BlockName']['text']; $blockname = $res['BlockName']['text'];
$method = 'OpenBlock_Handle_'.$blockname; $method = 'OpenBlock_Handle_'.$blockname;
if (method_exists($this, $method)) { if (method_exists($this, $method ?? '')) {
$res['php'] = $this->$method($res); $res['php'] = $this->$method($res);
} elseif (isset($this->openBlocks[$blockname])) { } elseif (isset($this->openBlocks[$blockname])) {
$res['php'] = call_user_func($this->openBlocks[$blockname], $res); $res['php'] = call_user_func($this->openBlocks[$blockname], $res);
@ -1087,7 +1087,7 @@ class SSTemplateParser extends Parser implements TemplateParser
} }
$php = ($arg['ArgumentMode'] == 'default') ? $arg['lookup_php'] : $arg['php']; $php = ($arg['ArgumentMode'] == 'default') ? $arg['lookup_php'] : $arg['php'];
return '$val .= Debug::show('.str_replace('FINALGET!', 'cachedCall', $php).');'; return '$val .= Debug::show('.str_replace('FINALGET!', 'cachedCall', $php ?? '').');';
} else { } else {
throw new SSTemplateParseException('Debug takes 0 or 1 argument only.', $this); throw new SSTemplateParseException('Debug takes 0 or 1 argument only.', $this);
} }
@ -1221,8 +1221,8 @@ class SSTemplateParser extends Parser implements TemplateParser
$text = $res['text']; $text = $res['text'];
// Unescape any escaped characters in the text, then put back escapes for any single quotes and backslashes // Unescape any escaped characters in the text, then put back escapes for any single quotes and backslashes
$text = stripslashes($text); $text = stripslashes($text ?? '');
$text = addcslashes($text, '\'\\'); $text = addcslashes($text ?? '', '\'\\');
// TODO: This is pretty ugly & gets applied on all files not just html. I wonder if we can make this // TODO: This is pretty ugly & gets applied on all files not just html. I wonder if we can make this
// non-dynamically calculated // non-dynamically calculated
@ -1234,8 +1234,8 @@ EOC;
// Because preg_replace replacement requires escaped slashes, addcslashes here // Because preg_replace replacement requires escaped slashes, addcslashes here
$text = preg_replace( $text = preg_replace(
'/(<a[^>]+href *= *)"#/i', '/(<a[^>]+href *= *)"#/i',
'\\1"\' . ' . addcslashes($code, '\\') . ' . \'#', '\\1"\' . ' . addcslashes($code ?? '', '\\') . ' . \'#',
$text $text ?? ''
); );
$res['php'] .= '$val .= \'' . $text . '\';' . PHP_EOL; $res['php'] .= '$val .= \'' . $text . '\';' . PHP_EOL;
@ -1257,7 +1257,7 @@ EOC;
*/ */
public function compileString($string, $templateName = "", $includeDebuggingComments = false, $topTemplate = true) public function compileString($string, $templateName = "", $includeDebuggingComments = false, $topTemplate = true)
{ {
if (!trim($string)) { if (!trim($string ?? '')) {
$code = ''; $code = '';
} else { } else {
parent::__construct($string); parent::__construct($string);
@ -1266,7 +1266,7 @@ EOC;
// Ignore UTF8 BOM at beginning of string. TODO: Confirm this is needed, make sure SSViewer handles UTF // Ignore UTF8 BOM at beginning of string. TODO: Confirm this is needed, make sure SSViewer handles UTF
// (and other encodings) properly // (and other encodings) properly
if (substr($string, 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) { if (substr($string ?? '', 0, 3) == pack("CCC", 0xef, 0xbb, 0xbf)) {
$this->pos = 3; $this->pos = 3;
} }
@ -1285,7 +1285,7 @@ EOC;
} }
// Include top level debugging comments if desired // Include top level debugging comments if desired
if ($includeDebuggingComments && $templateName && stripos($code, "<?xml") === false) { if ($includeDebuggingComments && $templateName && stripos($code ?? '', "<?xml") === false) {
$code = $this->includeDebuggingComments($code, $templateName); $code = $this->includeDebuggingComments($code, $templateName);
} }
@ -1301,12 +1301,12 @@ EOC;
{ {
// If this template contains a doctype, put it right after it, // If this template contains a doctype, put it right after it,
// if not, put it after the <html> tag to avoid IE glitches // if not, put it after the <html> tag to avoid IE glitches
if (stripos($code, "<!doctype") !== false) { if (stripos($code ?? '', "<!doctype") !== false) {
$code = preg_replace('/(<!doctype[^>]*("[^"]")*[^>]*>)/im', "$1\r\n<!-- template $templateName -->", $code); $code = preg_replace('/(<!doctype[^>]*("[^"]")*[^>]*>)/im', "$1\r\n<!-- template $templateName -->", $code ?? '');
$code .= "\r\n" . '$val .= \'<!-- end template ' . $templateName . ' -->\';'; $code .= "\r\n" . '$val .= \'<!-- end template ' . $templateName . ' -->\';';
} elseif (stripos($code, "<html") !== false) { } elseif (stripos($code ?? '', "<html") !== false) {
$code = preg_replace_callback('/(.*)(<html[^>]*>)(.*)/i', function ($matches) use ($templateName) { $code = preg_replace_callback('/(.*)(<html[^>]*>)(.*)/i', function ($matches) use ($templateName) {
if (stripos($matches[3], '<!--') === false && stripos($matches[3], '-->') !== false) { if (stripos($matches[3] ?? '', '<!--') === false && stripos($matches[3] ?? '', '-->') !== false) {
// after this <html> tag there is a comment close but no comment has been opened // after this <html> tag there is a comment close but no comment has been opened
// this most likely means that this <html> tag is inside a comment // this most likely means that this <html> tag is inside a comment
// we should not add a comment inside a comment (invalid html) // we should not add a comment inside a comment (invalid html)
@ -1317,11 +1317,11 @@ EOC;
// all other cases, add the comment and return it // all other cases, add the comment and return it
return "{$matches[1]}{$matches[2]}<!-- template $templateName -->{$matches[3]}"; return "{$matches[1]}{$matches[2]}<!-- template $templateName -->{$matches[3]}";
} }
}, $code); }, $code ?? '');
$code = preg_replace('/(<\/html[^>]*>)/i', "<!-- end template $templateName -->$1", $code); $code = preg_replace('/(<\/html[^>]*>)/i', "<!-- end template $templateName -->$1", $code ?? '');
} else { } else {
$code = str_replace('<?php' . PHP_EOL, '<?php' . PHP_EOL . '$val .= \'<!-- template ' . $templateName . $code = str_replace('<?php' . PHP_EOL, '<?php' . PHP_EOL . '$val .= \'<!-- template ' . $templateName .
' -->\';' . "\r\n", $code); ' -->\';' . "\r\n", $code ?? '');
$code .= "\r\n" . '$val .= \'<!-- end template ' . $templateName . ' -->\';'; $code .= "\r\n" . '$val .= \'<!-- end template ' . $templateName . ' -->\';';
} }
return $code; return $code;
@ -1337,6 +1337,6 @@ EOC;
*/ */
public function compileFile($template) public function compileFile($template)
{ {
return $this->compileString(file_get_contents($template), $template); return $this->compileString(file_get_contents($template ?? ''), $template);
} }
} }

View File

@ -3501,7 +3501,7 @@ class SSTemplateParser extends Parser implements TemplateParser
break; break;
default: default:
$res['php'] .= str_replace('$$FINAL', 'obj', $sub['php']) . '->self()'; $res['php'] .= str_replace('$$FINAL', 'obj', $sub['php'] ?? '') . '->self()';
break; break;
} }
} }

View File

@ -276,7 +276,7 @@ class TokenLiteral extends TokenExpressionable {
function match_code( $value ) { function match_code( $value ) {
// We inline single-character matches for speed // We inline single-character matches for speed
if ( !$this->contains_expression($value) && strlen( eval( 'return '. $value . ';' ) ) == 1 ) { if ( !$this->contains_expression($value) && strlen( eval( 'return '. $value . ';' ) ) == 1 ) {
return $this->match_fail_conditional( 'substr($this->string,$this->pos,1) == '.$value, return $this->match_fail_conditional( 'substr($this->string ?? \'\',$this->pos ?? 0,1) == '.$value,
PHPBuilder::build()->l( PHPBuilder::build()->l(
'$this->pos += 1;', '$this->pos += 1;',
$this->set_text($value) $this->set_text($value)