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
This commit is contained in:
thiemowmde 2023-10-04 17:19:14 +02:00 committed by Thiemo Kreuz (WMDE)
parent 475e5d757e
commit 7662dfeddc
2 changed files with 7 additions and 46 deletions

View file

@ -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.

View file

@ -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;
}
/**