diff --git a/src/Cite.php b/src/Cite.php index 77487608e..6c50ae89f 100644 --- a/src/Cite.php +++ b/src/Cite.php @@ -291,9 +291,7 @@ class Cite { ); $arguments = $status->getValue(); // Use the default group, or the references group when inside one. - if ( $arguments['group'] === null ) { - $arguments['group'] = $this->inReferencesGroup ?? self::DEFAULT_GROUP; - } + $arguments['group'] ??= $this->inReferencesGroup ?? self::DEFAULT_GROUP; // @phan-suppress-next-line PhanParamTooFewUnpack No good way to document it. $status->merge( $this->validateRef( $text, ...array_values( $arguments ) ) ); diff --git a/src/Hooks/CiteParserTagHooks.php b/src/Hooks/CiteParserTagHooks.php index 32f9884f0..bb7c3bc00 100644 --- a/src/Hooks/CiteParserTagHooks.php +++ b/src/Hooks/CiteParserTagHooks.php @@ -90,9 +90,7 @@ class CiteParserTagHooks { * @return Cite */ private static function citeForParser( Parser $parser ): Cite { - if ( !isset( $parser->extCite ) ) { - $parser->extCite = new Cite( $parser ); - } + $parser->extCite ??= new Cite( $parser ); return $parser->extCite; } diff --git a/src/ReferenceStack.php b/src/ReferenceStack.php index 30d7d75b8..53480da15 100644 --- a/src/ReferenceStack.php +++ b/src/ReferenceStack.php @@ -111,12 +111,8 @@ class ReferenceStack { ?string $follow, ?string $dir ): ?array { - if ( !isset( $this->refs[$group] ) ) { - $this->refs[$group] = []; - } - if ( !isset( $this->groupRefSequence[$group] ) ) { - $this->groupRefSequence[$group] = 0; - } + $this->refs[$group] ??= []; + $this->groupRefSequence[$group] ??= 0; $ref = [ 'count' => $name ? 0 : -1, @@ -403,11 +399,8 @@ class ReferenceStack { * @param string $text */ public function appendText( string $group, string $name, string $text ) { - if ( isset( $this->refs[$group][$name]['text'] ) ) { - $this->refs[$group][$name]['text'] .= $text; - } else { - $this->refs[$group][$name]['text'] = $text; - } + $this->refs[$group][$name]['text'] ??= ''; + $this->refs[$group][$name]['text'] .= $text; } } diff --git a/src/ReferencesFormatter.php b/src/ReferencesFormatter.php index 06b9288ef..97597a992 100644 --- a/src/ReferencesFormatter.php +++ b/src/ReferencesFormatter.php @@ -264,13 +264,11 @@ class ReferencesFormatter { private function referencesFormatEntryAlternateBacklinkLabel( Parser $parser, int $offset ): string { - if ( $this->backlinkLabels === null ) { - $this->backlinkLabels = preg_split( - '/\s+/', - $this->messageLocalizer->msg( 'cite_references_link_many_format_backlink_labels' ) - ->plain() - ); - } + $this->backlinkLabels ??= preg_split( + '/\s+/', + $this->messageLocalizer->msg( 'cite_references_link_many_format_backlink_labels' ) + ->plain() + ); return $this->backlinkLabels[$offset] ?? $this->errorReporter->plain( $parser, 'cite_error_references_no_backlink_label' ); diff --git a/src/ResourceLoader/CitationToolDefinition.php b/src/ResourceLoader/CitationToolDefinition.php index 9b5f1625b..b6ac23b83 100644 --- a/src/ResourceLoader/CitationToolDefinition.php +++ b/src/ResourceLoader/CitationToolDefinition.php @@ -24,26 +24,22 @@ class CitationToolDefinition { ->plain() ); - if ( $citationDefinition === null ) { - $citationDefinition = json_decode( - $context->msg( 'visualeditor-cite-tool-definition.json' ) - ->inContentLanguage() - ->plain() - ); - } + $citationDefinition ??= json_decode( + $context->msg( 'visualeditor-cite-tool-definition.json' ) + ->inContentLanguage() + ->plain() + ); $citationTools = []; if ( is_array( $citationDefinition ) ) { foreach ( $citationDefinition as $tool ) { - if ( !isset( $tool->title ) ) { - // The following messages are generated here: - // * visualeditor-cite-tool-name-book - // * visualeditor-cite-tool-name-journal - // * visualeditor-cite-tool-name-news - // * visualeditor-cite-tool-name-web - $tool->title = $context->msg( 'visualeditor-cite-tool-name-' . $tool->name ) - ->text(); - } + // The following messages are generated here: + // * visualeditor-cite-tool-name-book + // * visualeditor-cite-tool-name-journal + // * visualeditor-cite-tool-name-news + // * visualeditor-cite-tool-name-web + $tool->title ??= $context->msg( 'visualeditor-cite-tool-name-' . $tool->name ) + ->text(); $citationTools[] = $tool; } } diff --git a/tests/phpunit/unit/CiteParserHooksTest.php b/tests/phpunit/unit/CiteParserHooksTest.php index 7911efacd..dc10cde4b 100644 --- a/tests/phpunit/unit/CiteParserHooksTest.php +++ b/tests/phpunit/unit/CiteParserHooksTest.php @@ -43,7 +43,7 @@ class CiteParserHooksTest extends \MediaWikiUnitTestCase { $citeParserHooks = new CiteParserHooks(); $citeParserHooks->onParserClearState( $parser ); - $this->assertFalse( isset( $parser->extCite ) ); + $this->assertObjectNotHasProperty( 'extCite', $parser ); } /** @@ -56,7 +56,7 @@ class CiteParserHooksTest extends \MediaWikiUnitTestCase { $citeParserHooks = new CiteParserHooks(); $citeParserHooks->onParserCloned( $parser ); - $this->assertFalse( isset( $parser->extCite ) ); + $this->assertObjectNotHasProperty( 'extCite', $parser ); } /** diff --git a/tests/phpunit/unit/ReferencesFormatterTest.php b/tests/phpunit/unit/ReferencesFormatterTest.php index 2d6a6688b..7f5ece973 100644 --- a/tests/phpunit/unit/ReferencesFormatterTest.php +++ b/tests/phpunit/unit/ReferencesFormatterTest.php @@ -370,16 +370,13 @@ class ReferencesFormatterTest extends \MediaWikiUnitTestCase { $mockMessageLocalizer->method( 'msg' ) ->willReturn( $mockMessage ); - $mockErrorReporter = $this->createMock( ErrorReporter::class ); - if ( $expectedLabel === null ) { - $mockErrorReporter->expects( $this->once() )->method( 'plain' ); - } else { - $mockErrorReporter->expects( $this->never() )->method( 'plain' ); - } + $errorReporter = $this->createMock( ErrorReporter::class ); + $errorReporter->expects( $expectedLabel ? $this->never() : $this->once() ) + ->method( 'plain' ); /** @var ReferencesFormatter $formatter */ $formatter = TestingAccessWrapper::newFromObject( new ReferencesFormatter( - $mockErrorReporter, + $errorReporter, $this->createMock( AnchorFormatter::class ), $mockMessageLocalizer ) );