From f148c6507878fb80923cf5074f3bc3d3cb960643 Mon Sep 17 00:00:00 2001 From: Adam Wight Date: Fri, 22 Dec 2023 23:46:54 +0100 Subject: [PATCH] Encapsulate ref: pushRef returns an object This patch affects a few methods which use the output of pushRef. Bug: T353451 Change-Id: I10b3fe89406c11cdaede92f18a4b96586ecaf5a0 --- src/FootnoteMarkFormatter.php | 18 +++++++++--------- src/ReferenceStack.php | 6 +++--- .../integration/FootnoteMarkFormatterTest.php | 2 ++ tests/phpunit/unit/ReferenceStackTest.php | 6 ++---- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/src/FootnoteMarkFormatter.php b/src/FootnoteMarkFormatter.php index fab4d1037..f33df2282 100644 --- a/src/FootnoteMarkFormatter.php +++ b/src/FootnoteMarkFormatter.php @@ -36,26 +36,26 @@ class FootnoteMarkFormatter { * @suppress SecurityCheck-DoubleEscaped * @param Parser $parser * @param string $group - * @param array $ref Dictionary with ReferenceStack ref format + * @param ReferenceStackItem $ref * * @return string */ - public function linkRef( Parser $parser, string $group, array $ref ): string { - $label = $this->getLinkLabel( $parser, $group, $ref['number'] ); + public function linkRef( Parser $parser, string $group, ReferenceStackItem $ref ): string { + $label = $this->getLinkLabel( $parser, $group, $ref->number ); if ( $label === null ) { - $label = $this->messageLocalizer->localizeDigits( $ref['number'] ); + $label = $this->messageLocalizer->localizeDigits( (string)$ref->number ); if ( $group !== Cite::DEFAULT_GROUP ) { $label = "$group $label"; } } - if ( isset( $ref['extendsIndex'] ) ) { - $label .= '.' . $this->messageLocalizer->localizeDigits( $ref['extendsIndex'] ); + if ( isset( $ref->extendsIndex ) ) { + $label .= '.' . $this->messageLocalizer->localizeDigits( (string)$ref->extendsIndex ); } - $key = $ref['name'] ?? $ref['key']; + $key = $ref->name ?? $ref->key; // TODO: Use count without decrementing. - $count = $ref['name'] ? $ref['key'] . '-' . ( $ref['count'] - 1 ) : null; - $subkey = $ref['name'] ? '-' . $ref['key'] : null; + $count = $ref->name ? $ref->key . '-' . ( $ref->count - 1 ) : null; + $subkey = $ref->name ? '-' . $ref->key : null; return $parser->recursiveTagParse( $this->messageLocalizer->msg( diff --git a/src/ReferenceStack.php b/src/ReferenceStack.php index 11ffd09e7..b0d94e7ab 100644 --- a/src/ReferenceStack.php +++ b/src/ReferenceStack.php @@ -68,7 +68,7 @@ class ReferenceStack { * @param ?string $follow Guaranteed to not be a numeric string * @param ?string $dir ref direction * - * @return ?array ref structure, or null if no footnote marker should be rendered + * @return ?ReferenceStackItem ref structure, or null if no footnote marker should be rendered */ public function pushRef( StripState $stripState, @@ -79,7 +79,7 @@ class ReferenceStack { ?string $extends, ?string $follow, ?string $dir - ): ?array { + ): ?ReferenceStackItem { $this->refs[$group] ??= []; $this->groupRefSequence[$group] ??= 0; @@ -183,7 +183,7 @@ class ReferenceStack { } $this->refCallStack[] = [ $action, $ref->key, $group, $name, $extends, $text, $argv ]; - return (array)$ref; + return $ref; } /** diff --git a/tests/phpunit/integration/FootnoteMarkFormatterTest.php b/tests/phpunit/integration/FootnoteMarkFormatterTest.php index 535220154..4befc930a 100644 --- a/tests/phpunit/integration/FootnoteMarkFormatterTest.php +++ b/tests/phpunit/integration/FootnoteMarkFormatterTest.php @@ -6,6 +6,7 @@ use Cite\AnchorFormatter; use Cite\ErrorReporter; use Cite\FootnoteMarkFormatter; use Cite\ReferenceMessageLocalizer; +use Cite\Tests\TestUtils; use Message; use Parser; use Wikimedia\TestingAccessWrapper; @@ -52,6 +53,7 @@ class FootnoteMarkFormatterTest extends \MediaWikiIntegrationTestCase { $messageLocalizer ); + $ref = TestUtils::refFromArray( $ref ); $output = $formatter->linkRef( $mockParser, $group, $ref ); $this->assertSame( $expectedOutput, $output ); } diff --git a/tests/phpunit/unit/ReferenceStackTest.php b/tests/phpunit/unit/ReferenceStackTest.php index 343959a5f..bca51dcac 100644 --- a/tests/phpunit/unit/ReferenceStackTest.php +++ b/tests/phpunit/unit/ReferenceStackTest.php @@ -43,10 +43,8 @@ class ReferenceStackTest extends \MediaWikiUnitTestCase { $this->assertArrayHasKey( $i, $expectedOutputs, 'Bad test, not enough expected outputs in fixture.' ); - $expectedOutput = $expectedOutputs[$i] - ? (array)TestUtils::refFromArray( $expectedOutputs[$i] ) - : null; - $this->assertEquals( $expectedOutput, $result ); + $expectedRef = TestUtils::refFromArray( $expectedOutputs[$i] ); + $this->assertEquals( $expectedRef, $result ); } $finalRefs = TestUtils::refGroupsFromArray( $finalRefs );