From 24cf287d631aaf33f12a8a591e9884e7eda27a68 Mon Sep 17 00:00:00 2001 From: Dan Hemberger Date: Wed, 19 Jan 2022 07:53:08 -0800 Subject: [PATCH] Fix remaining deprecation warnings in PHP 8.1 This is a followup to #24 that fixes all remaining deprecated calls in PHP 8.1 that occur during execution of the unit tests. Deprecations are converted to exceptions to make the tests more strict (as in #27, for warnings and notices). The primary cause of these deprecation warnings is passing a `null` pattern to any of the `cleanupWS*` functions, which then pass it to `strlen`. In PHP 8.1, passing null to non-nullable internal functions is deprecated. In https://github.com/vanilla/nbbc/pull/24#issuecomment-1006023802, we got some insight into the intended design of the `cleanupWS*` functions from @seanofw, the original author of this package. With the use of the PHP 7 null coalescing operator, we can respect the intended design by _always_ calling the `cleanupWS*` functions, e.g.: $newpos = $this->cleanupWSByIteratingPointer( $this->tag_rules[$tag_name]['after_tag'] ?? '', $pos + 1, $this->stack ); instead of: if (isset($this->tag_rules[$tag_name]['after_tag'])) { $newpos = $this->cleanupWSByIteratingPointer( $this->tag_rules[$tag_name]['after_tag'], $pos + 1, $this->stack ); } else { $newpos = $pos + 1; } That is, if the pattern is not defined or is null, we pass the empty string as the pattern instead, since all `cleanupWS*` functions return early if the pattern is the empty string. --- src/BBCode.php | 66 ++++++++++++------------------------------ tests/phpunit.xml.dist | 1 + 2 files changed, 20 insertions(+), 47 deletions(-) diff --git a/src/BBCode.php b/src/BBCode.php index 69085bc..219c721 100644 --- a/src/BBCode.php +++ b/src/BBCode.php @@ -1437,15 +1437,15 @@ protected function generateOutput($pos) { $this->lost_start_tags[$name]++; } - $end = $this->cleanupWSByIteratingPointer($rule['before_endtag'], 0, $output); - $this->cleanupWSByPoppingStack($rule['after_tag'], $output); + $end = $this->cleanupWSByIteratingPointer($rule['before_endtag'] ?? '', 0, $output); + $this->cleanupWSByPoppingStack($rule['after_tag'] ?? '', $output); $tag_body = $this->collectTextReverse($output, count($output) - 1, $end); // Note: We don't process 'after_endtag' because the invisible end tag // always butts up against another tag, so there's *never* any whitespace // after it. Attempting to process 'after_endtag' would just be a waste // of time because it'd never match. But 'before_tag' is useful, though. - $this->cleanupWSByPoppingStack($rule['before_tag'], $this->stack); + $this->cleanupWSByPoppingStack($rule['before_tag'] ?? '', $this->stack); Debugger::debug("Internal_GenerateOutput: optional-tag's content: " .htmlspecialchars($tag_body)."
\n"); @@ -1593,15 +1593,11 @@ protected function finishTag($tag_name) { // do end-tag cleanup by popping, and we do start-tag cleanup by skipping // $pos forward. (We add one because we've actually rewound the stack // to the start tag itself.) - if (isset($this->tag_rules[$tag_name]) && isset($this->tag_rules[$tag_name]['after_tag'])) { - $newpos = $this->cleanupWSByIteratingPointer( - $this->tag_rules[$tag_name]['after_tag'], - $pos + 1, - $this->stack - ); - } else { - $newpos = $pos + 1; - } + $newpos = $this->cleanupWSByIteratingPointer( + $this->tag_rules[$tag_name]['after_tag'] ?? '', + $pos + 1, + $this->stack + ); $delta = $newpos - ($pos + 1); if ($this->debug) { @@ -1616,11 +1612,7 @@ protected function finishTag($tag_name) { $output = $this->generateOutput($newpos); // Clean off any whitespace before the end tag that doesn't belong there. - if (isset($this->tag_rules[$tag_name]) && isset($this->tag_rules[$tag_name]['before_endtag'])) { - $newend = $this->cleanupWSByIteratingPointer($this->tag_rules[$tag_name]['before_endtag'], 0, $output); - } else { - $newend = 0; - } + $newend = $this->cleanupWSByIteratingPointer($this->tag_rules[$tag_name]['before_endtag'] ?? '', 0, $output); $output = $this->collectTextReverse($output, count($output) - 1, $newend); if ($this->debug) @@ -2146,13 +2138,9 @@ protected function processIsolatedTag($tag_name, $tag_params, $tag_rule) { return; } - if (isset($tag_rule['before_tag'])) { - $this->cleanupWSByPoppingStack($tag_rule['before_tag'], $this->stack); - } + $this->cleanupWSByPoppingStack($tag_rule['before_tag'] ?? '', $this->stack); $output = $this->doTag(self::BBCODE_OUTPUT, $tag_name, $tag_params['_default'], $tag_params, ""); - if (isset($tag_rule['after_tag'])) { - $this->cleanupWSByEatingInput($tag_rule['after_tag']); - } + $this->cleanupWSByEatingInput($tag_rule['after_tag'] ?? ''); if ($this->debug) { Debugger::debug("ProcessIsolatedTag: isolated tag [".htmlspecialchars($tag_name) @@ -2258,17 +2246,9 @@ protected function processVerbatimTag($tag_name, $tag_params, $tag_rule) { Debugger::debug("Internal_ProcessVerbatimTag: found end tag.
\n"); // Clean up whitespace everywhere except before the start tag. - if (isset($tag_rule['after_tag'])) { - $newstart = $this->cleanupWSByIteratingPointer($tag_rule['after_tag'], $start, $this->stack); - } else { - $newstart = $start; - } - if (isset($tag_rule['before_endtag'])) { - $this->cleanupWSByPoppingStack($tag_rule['before_endtag'], $this->stack); - } - if (isset($tag_rule['after_endtag'])) { - $this->cleanupWSByEatingInput($tag_rule['after_endtag']); - } + $newstart = $this->cleanupWSByIteratingPointer($tag_rule['after_tag'] ?? '', $start, $this->stack); + $this->cleanupWSByPoppingStack($tag_rule['before_endtag'] ?? '', $this->stack); + $this->cleanupWSByEatingInput($tag_rule['after_endtag'] ?? ''); // Collect the output from $newstart to the top of the stack, and then // quickly pop off all of those tokens. @@ -2282,9 +2262,7 @@ protected function processVerbatimTag($tag_name, $tag_params, $tag_rule) { // Clean up whitespace before the start tag (the tag was never pushed // onto the stack itself, so we don't need to remove it). - if (isset($tag_rule['before_tag'])) { - $this->cleanupWSByPoppingStack($tag_rule['before_tag'], $this->stack); - } + $this->cleanupWSByPoppingStack($tag_rule['before_tag'] ?? '', $this->stack); // Found the end tag, so process this tag immediately with // the contents collected between them. Note that we do NOT @@ -2508,9 +2486,7 @@ protected function parseEndTagToken() { $start_tag_params = $start_tag_node[self::BBCODE_STACK_TAG]; $this->computeCurrentClass(); - if (isset($this->tag_rules[$tag_name]) && isset($this->tag_rules[$tag_name]['before_tag'])) { - $this->cleanupWSByPoppingStack($this->tag_rules[$tag_name]['before_tag'], $this->stack); - } + $this->cleanupWSByPoppingStack($this->tag_rules[$tag_name]['before_tag'] ?? '', $this->stack); $start_tag_params['_endtag'] = $tag_params['_tag']; $start_tag_params['_hasend'] = true; $output = $this->doTag( @@ -2521,9 +2497,7 @@ protected function parseEndTagToken() { $contents ); - if (isset($this->tag_rules[$tag_name]['after_endtag'])) { - $this->cleanupWSByEatingInput($this->tag_rules[$tag_name]['after_endtag']); - } + $this->cleanupWSByEatingInput($this->tag_rules[$tag_name]['after_endtag'] ?? ''); if ($this->debug) { Debugger::debug("Internal_ParseEndTagToken: end tag [/" @@ -2610,8 +2584,7 @@ public function parse($string) { $this->was_limited = false; // Remove any initial whitespace in pre-trim mode. - if (strlen($this->pre_trim) > 0) - $this->cleanupWSByEatingInput($this->pre_trim); + $this->cleanupWSByEatingInput($this->pre_trim); // In plain mode, we generate newlines instead of
tags. $newline = $this->plain_mode ? "\n" : "
\n"; @@ -2764,8 +2737,7 @@ public function parse($string) { Debugger::debug("
\nParse Done: done main parse; packing stack as text string.
\n"); // Remove any trailing whitespace in post-trim mode. - if (strlen($this->post_trim) > 0) - $this->cleanupWSByPoppingStack($this->post_trim, $this->stack); + $this->cleanupWSByPoppingStack($this->post_trim, $this->stack); // Everything left on the stack should be HTML (or broken tags), so pop it // all off as plain text, concatenate it, and return it. diff --git a/tests/phpunit.xml.dist b/tests/phpunit.xml.dist index 02a59a2..0484991 100644 --- a/tests/phpunit.xml.dist +++ b/tests/phpunit.xml.dist @@ -3,6 +3,7 @@