diff --git a/composer.json b/composer.json index 322ddedbf..f3e15d565 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,7 @@ "jakub-onderka/php-console-highlighter": "0.3.2", "jakub-onderka/php-parallel-lint": "1.0.0", "mediawiki/mediawiki-codesniffer": "28.0.0", - "mediawiki/mediawiki-phan-config": "0.8.0", + "mediawiki/mediawiki-phan-config": "0.9.0", "mediawiki/minus-x": "0.3.2" }, "scripts": { diff --git a/src/Cite.php b/src/Cite.php index 8f4576ba0..29f54858b 100644 --- a/src/Cite.php +++ b/src/Cite.php @@ -139,7 +139,7 @@ class Cite { /** * @param ?string $text - * @param ?string $group + * @param string $group * @param ?string $name * @param ?string $extends * @param ?string $follow @@ -148,13 +148,16 @@ class Cite { */ private function validateRef( ?string $text, - ?string $group, + string $group, ?string $name, ?string $extends, ?string $follow, ?string $dir ) : StatusValue { - if ( ctype_digit( $name ) || ctype_digit( $follow ) || ctype_digit( $extends ) ) { + if ( ctype_digit( (string)$name ) + || ctype_digit( (string)$extends ) + || ctype_digit( (string)$follow ) + ) { // Numeric names mess up the resulting id's, potentially producing // duplicate id's in the XHTML. The Right Thing To Do // would be to mangle them, but it's not really high-priority @@ -204,7 +207,7 @@ class Cite { } } - if ( preg_match( + if ( $text !== null && preg_match( '/]*+>/i', preg_replace( '#<(\w++)[^>]*+>.*?|#s', '', $text ) ) ) { @@ -230,7 +233,7 @@ class Cite { private function validateRefInReferences( ?string $text, ?string $name, - ?string $group + string $group ): StatusValue { // FIXME: Some assertions make assumptions that rely on earlier tests not failing. // These dependencies need to be explicit so they aren't accidentally broken by @@ -324,7 +327,7 @@ class Cite { ...$error['params'] ); } - } else { + } elseif ( $text !== null ) { $groupRefs = $this->referenceStack->getGroupRefs( $group ); if ( !isset( $groupRefs[$name]['text'] ) ) { $this->referenceStack->appendText( $group, $name, $text ); diff --git a/src/ReferenceStack.php b/src/ReferenceStack.php index 5be9ea22b..d5f1e4082 100644 --- a/src/ReferenceStack.php +++ b/src/ReferenceStack.php @@ -135,7 +135,7 @@ class ReferenceStack { $this->groupRefSequence[$group] = 0; } - if ( $this->refs[$group][$follow] ?? false ) { + if ( $follow && isset( $this->refs[$group][$follow] ) ) { // We know the parent note already, so just perform the "follow" and bail out // TODO: Separate `pushRef` from these side-effects. $this->appendText( $group, $follow, ' ' . $text ); @@ -238,6 +238,7 @@ class ReferenceStack { } } + // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset "key" is guaranteed to be set $this->refCallStack[] = [ $action, $ref['key'], $group, $name, $extends, $text, $argv ]; return $ref; } @@ -395,11 +396,11 @@ class ReferenceStack { /** * Return all references for a group. * - * @param ?string $group + * @param string $group * @return array[] */ - public function getGroupRefs( ?string $group ) : array { - return $this->refs[$group ?? Cite::DEFAULT_GROUP] ?? []; + public function getGroupRefs( string $group ) : array { + return $this->refs[$group] ?? []; } /** diff --git a/src/ReferencesFormatter.php b/src/ReferencesFormatter.php index 1319cb9bb..b3bb7d95a 100644 --- a/src/ReferencesFormatter.php +++ b/src/ReferencesFormatter.php @@ -266,7 +266,7 @@ class ReferencesFormatter { return $this->messageLocalizer->localizeDigits( $base ) . $this->messageLocalizer->formatNum( '.' . - str_pad( $offset, strlen( $max ), '0', STR_PAD_LEFT ) + str_pad( (string)$offset, strlen( (string)$max ), '0', STR_PAD_LEFT ) ); } diff --git a/tests/phpunit/unit/CiteUnitTest.php b/tests/phpunit/unit/CiteUnitTest.php index 4ca9c45c9..c91dbd374 100644 --- a/tests/phpunit/unit/CiteUnitTest.php +++ b/tests/phpunit/unit/CiteUnitTest.php @@ -68,7 +68,7 @@ class CiteUnitTest extends \MediaWikiUnitTestCase { 'isSectionPreview' => false, 'text' => null, 'name' => '1', - 'group' => null, + 'group' => '', 'follow' => null, 'extends' => null, 'dir' => null, @@ -80,7 +80,7 @@ class CiteUnitTest extends \MediaWikiUnitTestCase { 'isSectionPreview' => false, 'text' => 't', 'name' => null, - 'group' => null, + 'group' => '', 'follow' => '1', 'extends' => null, 'dir' => null, @@ -92,7 +92,7 @@ class CiteUnitTest extends \MediaWikiUnitTestCase { 'isSectionPreview' => false, 'text' => 't', 'name' => null, - 'group' => null, + 'group' => '', 'follow' => null, 'extends' => '1', 'dir' => null, @@ -104,7 +104,7 @@ class CiteUnitTest extends \MediaWikiUnitTestCase { 'isSectionPreview' => false, 'text' => 't', 'name' => 'n', - 'group' => null, + 'group' => '', 'follow' => 'f', 'extends' => null, 'dir' => null, @@ -116,7 +116,7 @@ class CiteUnitTest extends \MediaWikiUnitTestCase { 'isSectionPreview' => false, 'text' => 't', 'name' => null, - 'group' => null, + 'group' => '', 'follow' => 'f', 'extends' => 'e', 'dir' => null, @@ -129,7 +129,7 @@ class CiteUnitTest extends \MediaWikiUnitTestCase { 'isSectionPreview' => false, 'text' => 't', 'name' => null, - 'group' => null, + 'group' => '', 'follow' => null, 'extends' => null, 'dir' => null, @@ -141,7 +141,7 @@ class CiteUnitTest extends \MediaWikiUnitTestCase { 'isSectionPreview' => false, 'text' => '', 'name' => null, - 'group' => null, + 'group' => '', 'follow' => null, 'extends' => null, 'dir' => null, @@ -153,7 +153,7 @@ class CiteUnitTest extends \MediaWikiUnitTestCase { 'isSectionPreview' => false, 'text' => null, 'name' => null, - 'group' => null, + 'group' => '', 'follow' => null, 'extends' => null, 'dir' => null, @@ -165,7 +165,7 @@ class CiteUnitTest extends \MediaWikiUnitTestCase { 'isSectionPreview' => false, 'text' => 'Foo ', 'name' => 'n', - 'group' => null, + 'group' => '', 'follow' => null, 'extends' => null, 'dir' => null, @@ -251,7 +251,7 @@ class CiteUnitTest extends \MediaWikiUnitTestCase { 'isSectionPreview' => false, 'text' => 'not empty', 'name' => 'n', - 'group' => null, + 'group' => '', 'follow' => null, 'extends' => null, 'dir' => 'RTL', @@ -263,7 +263,7 @@ class CiteUnitTest extends \MediaWikiUnitTestCase { 'isSectionPreview' => false, 'text' => 'not empty', 'name' => 'n', - 'group' => null, + 'group' => '', 'follow' => null, 'extends' => null, 'dir' => 'foobar',