From 7662dfeddcb3df36efd2ea2f384ff65a8716303f Mon Sep 17 00:00:00 2001 From: thiemowmde Date: Wed, 4 Oct 2023 17:19:14 +0200 Subject: [PATCH] MagicWord: Make use of native count feature in preg_replace The only reason for the callback was to check if a replacement happened. We can do the same with the native $count feature. Also inline a trivial sort() callback. It was effectively @internal as it was obviously never meant to be used from outside of the class. Both methods have been marked as public in I3588d9d. I think this was just a mistake. Change-Id: Ie33d4347f710fe227f9cbd8833c192c11d6e7c89 --- RELEASE-NOTES-1.41 | 2 ++ includes/parser/MagicWord.php | 51 ++++------------------------------- 2 files changed, 7 insertions(+), 46 deletions(-) diff --git a/RELEASE-NOTES-1.41 b/RELEASE-NOTES-1.41 index 810c3b645c5..35cdcc455a6 100644 --- a/RELEASE-NOTES-1.41 +++ b/RELEASE-NOTES-1.41 @@ -538,11 +538,13 @@ because of Phabricator reports. * The following MagicWord methods have been removed without deprecation. There were no known uses. - ::addToArray + - ::compareStringLength - ::getVariableRegex - ::getVariableStartToEndRegex - ::getWasModified - ::matchStart - ::matchVariableStartToEnd + - ::pregRemoveAndRecord - ::substituteCallback * The following MagicWordArray methods have been removed or made private without deprecation. There were no known uses outside of the class. diff --git a/includes/parser/MagicWord.php b/includes/parser/MagicWord.php index 2a3d07e83f8..6c30924bdd1 100644 --- a/includes/parser/MagicWord.php +++ b/includes/parser/MagicWord.php @@ -86,9 +86,6 @@ class MagicWord { /** @var string */ private $mBaseRegex = ''; - /** @var bool */ - private $mFound = false; - /** @var Language */ private $contLang; @@ -133,7 +130,7 @@ class MagicWord { // Sort the synonyms by length, descending, so that the longest synonym // matches in precedence to the shortest $synonyms = $this->mSynonyms; - usort( $synonyms, [ $this, 'compareStringLength' ] ); + usort( $synonyms, static fn ( $a, $b ) => strlen( $b ) <=> strlen( $a ) ); $escSyn = []; foreach ( $synonyms as $synonym ) { @@ -148,22 +145,6 @@ class MagicWord { $this->mRegexStartToEnd = "/^(?:{$this->mBaseRegex})$/{$case}"; } - /** - * A comparison function that returns -1, 0 or 1 depending on whether the - * first string is longer, the same length or shorter than the second - * string. - * - * @param string $s1 - * @param string $s2 - * - * @return int - */ - public function compareStringLength( $s1, $s2 ) { - $l1 = strlen( $s1 ); - $l2 = strlen( $s2 ); - return $l2 <=> $l1; // descending - } - /** * Gets a regex representing matching the word * @@ -260,14 +241,8 @@ class MagicWord { * @return bool */ public function matchAndRemove( &$text ) { - $this->mFound = false; - $text = preg_replace_callback( - $this->getRegex(), - [ $this, 'pregRemoveAndRecord' ], - $text - ); - - return $this->mFound; + $text = preg_replace( $this->getRegex(), '', $text, -1, $count ); + return (bool)$count; } /** @@ -275,24 +250,8 @@ class MagicWord { * @return bool */ public function matchStartAndRemove( &$text ) { - $this->mFound = false; - $text = preg_replace_callback( - $this->getRegexStart(), - [ $this, 'pregRemoveAndRecord' ], - $text - ); - - return $this->mFound; - } - - /** - * Used in matchAndRemove() - * - * @return string - */ - public function pregRemoveAndRecord() { - $this->mFound = true; - return ''; + $text = preg_replace( $this->getRegexStart(), '', $text, -1, $count ); + return (bool)$count; } /**