From 11255770c5c5a889485a534f83633a6b89913454 Mon Sep 17 00:00:00 2001 From: Thiemo Kreuz Date: Wed, 1 Jun 2022 10:34:57 +0200 Subject: [PATCH] Make message key parser accept more than just underscores The best practice for message keys is to use dashes, not underscores. This codebase is quite old and traditionally uses underscores. I think we can make it flexible enough to work with both. Required for Ie64f4ab. Change-Id: I6f0584299a4f279ed929784927392eb0f72cbc80 --- src/ErrorReporter.php | 6 +++--- tests/phpunit/unit/AnchorFormatterTest.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/ErrorReporter.php b/src/ErrorReporter.php index 762866199..d913f058a 100644 --- a/src/ErrorReporter.php +++ b/src/ErrorReporter.php @@ -68,7 +68,7 @@ class ErrorReporter { $language = $this->getInterfaceLanguageAndSplitCache( $parser ); $msg = $this->messageLocalizer->msg( $key, ...$params )->inLanguage( $language ); - [ $type, ] = $this->parseTypeAndIdFromMessageKey( $msg->getKey() ); + [ $type ] = $this->parseTypeAndIdFromMessageKey( $msg->getKey() ); if ( $type === 'error' ) { // Take care; this is a sideeffect that might not belong to this class. @@ -124,10 +124,10 @@ class ErrorReporter { /** * @param string $messageKey Expected to be a message key like "cite_error_ref_too_many_keys" * - * @return string[] + * @return string[] Two elements, e.g. "error" and "ref_too_many_keys" */ private function parseTypeAndIdFromMessageKey( string $messageKey ): array { - return array_slice( explode( '_', $messageKey, 3 ), 1 ); + return array_slice( explode( '_', str_replace( '-', '_', $messageKey ), 3 ), 1 ); } } diff --git a/tests/phpunit/unit/AnchorFormatterTest.php b/tests/phpunit/unit/AnchorFormatterTest.php index d9523e6fd..f710d1996 100644 --- a/tests/phpunit/unit/AnchorFormatterTest.php +++ b/tests/phpunit/unit/AnchorFormatterTest.php @@ -14,7 +14,7 @@ use Wikimedia\TestingAccessWrapper; */ class AnchorFormatterTest extends \MediaWikiUnitTestCase { - public function setUp(): void { + protected function setUp(): void { parent::setUp(); global $wgFragmentMode;