From 781a33357b1c2aac4741eed89c2646e747dbf520 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Dziewo=C5=84ski?= Date: Sat, 16 Sep 2023 02:31:47 +0200 Subject: [PATCH] Use type hints for properties, remove PHPCS overrides MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MediaWiki's PHPCS plugin requires documentation comments on all properties, unless those properties are typed. This has potential to introduce bugs – in particular, because typed properties without a default value will throw an exception if their value is accessed before it's defined, while previously they defaulted to null. I fixed this when I found it (making them nullable and null by default), but I may have missed some cases. Change-Id: If5b1f4d542ce3e1b69327ee4283f7c3e133a62a0 --- .phpcs.xml | 4 --- includes/CommentParser.php | 13 +++++--- includes/CommentUtils.php | 2 +- includes/ImmutableRange.php | 10 +++--- includes/NodeFilter.php | 3 +- includes/SpecialFindComment.php | 2 +- includes/SubscriptionItem.php | 14 ++++---- includes/ThreadItem/ContentCommentItem.php | 12 ++++--- includes/ThreadItem/ContentThreadItem.php | 37 ++++++++++++---------- includes/TreeWalker.php | 10 +++--- 10 files changed, 56 insertions(+), 51 deletions(-) diff --git a/.phpcs.xml b/.phpcs.xml index f78207357..63cef758d 100644 --- a/.phpcs.xml +++ b/.phpcs.xml @@ -1,10 +1,6 @@ - - - - . diff --git a/includes/CommentParser.php b/includes/CommentParser.php index 47957c4f2..5df2c2e4c 100644 --- a/includes/CommentParser.php +++ b/includes/CommentParser.php @@ -40,13 +40,16 @@ class CommentParser { private LanguageConverterFactory $languageConverterFactory; private TitleParser $titleParser; - private $dateFormat; - private $digits; + /** @var string[] */ + private array $dateFormat; + /** @var string[][] */ + private array $digits; /** @var string[][] */ private $contLangMessages; - private $localTimezone; - private $timezones; - private $specialContributionsName; + private string $localTimezone; + /** @var string[][] */ + private array $timezones; + private string $specialContributionsName; private Element $rootNode; private TitleValue $title; diff --git a/includes/CommentUtils.php b/includes/CommentUtils.php index a4f5e2d23..7d8a6a318 100644 --- a/includes/CommentUtils.php +++ b/includes/CommentUtils.php @@ -20,7 +20,7 @@ class CommentUtils { private function __construct() { } - private static $blockElementTypes = [ + private static array $blockElementTypes = [ 'div', 'p', // Tables 'table', 'tbody', 'thead', 'tfoot', 'caption', 'th', 'tr', 'td', diff --git a/includes/ImmutableRange.php b/includes/ImmutableRange.php index 359a3d149..147f58987 100644 --- a/includes/ImmutableRange.php +++ b/includes/ImmutableRange.php @@ -30,11 +30,11 @@ use Wikimedia\Parsoid\DOM\Text; * @property int $startOffset */ class ImmutableRange { - private $mCommonAncestorContainer; - private $mEndContainer; - private $mEndOffset; - private $mStartContainer; - private $mStartOffset; + private ?Node $mCommonAncestorContainer = null; + private Node $mEndContainer; + private int $mEndOffset; + private Node $mStartContainer; + private int $mStartOffset; /** * Find the common ancestor container of two nodes diff --git a/includes/NodeFilter.php b/includes/NodeFilter.php index b7ce629a3..7d89813a7 100644 --- a/includes/NodeFilter.php +++ b/includes/NodeFilter.php @@ -35,9 +35,10 @@ class NodeFilter { public const SHOW_DOCUMENT_FRAGMENT = 0x400; public const SHOW_NOTATION = 0x800; + /** @var callable */ public $filter; - private $active = false; + private bool $active = false; /** * See https://dom.spec.whatwg.org/#dom-nodefilter-acceptnode diff --git a/includes/SpecialFindComment.php b/includes/SpecialFindComment.php index e74802939..9e881dc04 100644 --- a/includes/SpecialFindComment.php +++ b/includes/SpecialFindComment.php @@ -72,7 +72,7 @@ class SpecialFindComment extends FormSpecialPage { $form->setSubmitTextMsg( 'discussiontools-findcomment-label-search' ); } - private $idOrName; + private string $idOrName; /** * @inheritDoc diff --git a/includes/SubscriptionItem.php b/includes/SubscriptionItem.php index 9ed09b61f..110f455a9 100644 --- a/includes/SubscriptionItem.php +++ b/includes/SubscriptionItem.php @@ -9,12 +9,12 @@ use MediaWiki\User\UserIdentity; * Representation of a subscription to a given topic. */ class SubscriptionItem { - private $itemName; - private $linkTarget; - private $user; - private $state; - private $createdTimestamp; - private $notifiedTimestamp; + private UserIdentity $user; + private string $itemName; + private LinkTarget $linkTarget; + private int $state; + private ?string $createdTimestamp; + private ?string $notifiedTimestamp; /** * @param UserIdentity $user @@ -28,7 +28,7 @@ class SubscriptionItem { public function __construct( UserIdentity $user, string $itemName, - linkTarget $linkTarget, + LinkTarget $linkTarget, int $state, ?string $createdTimestamp, ?string $notifiedTimestamp diff --git a/includes/ThreadItem/ContentCommentItem.php b/includes/ThreadItem/ContentCommentItem.php index fa59fad43..e90b426f1 100644 --- a/includes/ThreadItem/ContentCommentItem.php +++ b/includes/ThreadItem/ContentCommentItem.php @@ -21,11 +21,13 @@ class ContentCommentItem extends ContentThreadItem implements CommentItem { jsonSerialize as protected traitJsonSerialize; } - private $signatureRanges; - private $timestampRanges; - private $timestamp; - private $author; - private $displayName; + /** @var ImmutableRange[] */ + private array $signatureRanges; + /** @var ImmutableRange[] */ + private array $timestampRanges; + private DateTimeImmutable $timestamp; + private string $author; + private ?string $displayName; /** * @param int $level diff --git a/includes/ThreadItem/ContentThreadItem.php b/includes/ThreadItem/ContentThreadItem.php index 5e2d11d78..dd73e44e8 100644 --- a/includes/ThreadItem/ContentThreadItem.php +++ b/includes/ThreadItem/ContentThreadItem.php @@ -19,21 +19,24 @@ use Wikimedia\Parsoid\Utils\DOMUtils; abstract class ContentThreadItem implements JsonSerializable, ThreadItem { use ThreadItemTrait; - protected $type; - protected $range; - protected $rootNode; - protected $level; - protected $parent; - protected $warnings = []; + protected string $type; + protected ImmutableRange $range; + protected Element $rootNode; + protected int $level; + protected ?ContentThreadItem $parent = null; + /** @var string[] */ + protected array $warnings = []; - protected $name = null; - protected $id = null; - protected $replies = []; + protected string $name; + protected string $id; + /** @var ContentThreadItem[] */ + protected array $replies = []; - protected $authors = null; - protected $commentCount; - protected $oldestReply; - protected $latestReply; + /** @var ?array[] */ + protected ?array $authors = null; + protected int $commentCount; + protected ?ContentCommentItem $oldestReply; + protected ?ContentCommentItem $latestReply; /** * @param string $type `heading` or `comment` @@ -487,16 +490,16 @@ abstract class ContentThreadItem implements JsonSerializable, ThreadItem { } /** - * @param string|null $name Thread item name + * @param string $name Thread item name */ - public function setName( ?string $name ): void { + public function setName( string $name ): void { $this->name = $name; } /** - * @param string|null $id Thread ID + * @param string $id Thread ID */ - public function setId( ?string $id ): void { + public function setId( string $id ): void { $this->id = $id; } diff --git a/includes/TreeWalker.php b/includes/TreeWalker.php index 2293de43c..2a372a391 100644 --- a/includes/TreeWalker.php +++ b/includes/TreeWalker.php @@ -16,12 +16,12 @@ use Wikimedia\Parsoid\DOM\Node; */ class TreeWalker { - public $root; - public $whatToShow; - public $currentNode; + public Node $root; + public int $whatToShow; + public Node $currentNode; + /** @var callable */ public $filter; - - private $isActive = false; + private bool $isActive = false; /** * See https://dom.spec.whatwg.org/#interface-treewalker