Skip to content

Modernise PHP code: MW 1.43 namespaces, native types, readonly#97

Merged
alistair3149 merged 4 commits into
masterfrom
modernise-php-types-and-namespaces
May 8, 2026
Merged

Modernise PHP code: MW 1.43 namespaces, native types, readonly#97
alistair3149 merged 4 commits into
masterfrom
modernise-php-types-and-namespaces

Conversation

@alistair3149
Copy link
Copy Markdown
Member

Summary

Conservative pass on the PHP source to use:

  • MW 1.43 namespaced classes for Parser, IncludableSpecialPage, Message, WebRequest (was: bare top-level imports). Side benefit: resolves three pre-existing psalm UndefinedClass errors.
  • Native property types in NetworkConfig and ParserFunctionNetworkPresenter (was: @var docblocks). NetworkConfig properties are now readonly.
  • Native return types on NetworkFunction::handleParserFunctionCall(): array|string and SpecialNetworkPresenter::getReturnValue(): string (was: docblock-only).
  • Constructor property promotion + readonly on NetworkFunction and NetworkUseCase.
  • Test type fixSpyNetworkPresenter::$viewModel is now ?ResponseModel = null to match its already-nullable getter (real bug, not just style).
  • Test method return types: void added to all test methods in NetworkUseCaseTest and NetworkFunctionIntegrationTest.

PHP floor and MediaWiki floor are unchanged.

Deviation from spec

The spec proposed SpecialNetwork::execute( ?string $subPage ) to drop the phpcs:ignore. That can't actually happen — SpecialPage::execute( $subPage ) in MW core has no native type, so adding ?string is an LSP-narrowing that both phpstan and psalm reject. Replaced the phpcs:ignore with @inheritDoc instead, which satisfies the missing-docblock check without restating the parent.

Out of scope (intentional)

  • Replacing MediaWikiServices::getInstance() in NetworkConfig with proper DI.
  • Replacing the public-set RequestModel/ResponseModel DTOs with constructor-based readonly objects.
  • Removing the Extension::getFactory() static indirection.
  • Resolving the psalm-suppress annotations in SpecialNetwork.php.

Test plan

Local verification (full quality suite green on combined HEAD against the dev wiki):

  • composer phpunit → 19/19 tests pass
  • vendor/bin/phpcs -p -s → 14/14 files clean
  • vendor/bin/phpstan analyse → No errors
  • vendor/bin/psalm → No errors found

CI (matrix from the recent CI-update PR):

  • All five PHPUnit rows pass (REL1_43+8.1, REL1_43+8.2, REL1_44+8.3, REL1_45+8.4); master+8.5 reaches the test phase
  • Static Analysis (REL1_43+8.3) passes
  • Code style (REL1_43+8.3) passes

Switch four top-level imports (Parser, IncludableSpecialPage,
Message, WebRequest) to their MW 1.43 namespaced equivalents
under MediaWiki\Parser, MediaWiki\SpecialPage, MediaWiki\Message,
MediaWiki\Request. No behaviour change.

Side benefit: resolves three pre-existing psalm UndefinedClass
errors that flagged Parser and IncludableSpecialPage at the
top-level namespace.
Replace @var docblocks with native PHP type declarations.
NetworkConfig's properties become readonly — they are set once
in the constructor (which computes them from MW main config)
and only read thereafter.

The one remaining docblock is the @var string[] hint on
NetworkConfig::$excludedNamespaces, kept because native PHP
arrays don't carry element-type information.
- NetworkFunction::handleParserFunctionCall() declares native
  array|string return type (PHP 8.0 union)
- SpecialNetworkPresenter::getReturnValue() declares native string
  return type
- SpecialNetwork::execute() keeps untyped $subPage (parent
  SpecialPage::execute is also untyped, so a narrower native
  type would be an LSP-narrowing violation that psalm and
  phpstan reject). Replaces phpcs:ignore with @inheritdoc,
  which satisfies the missing-docblock check without restating
  what the parent already documents.
- NetworkFunction and NetworkUseCase use PHP 8.0 constructor
  promotion with readonly, removing the separate property
  declaration + assignment boilerplate
- SpyNetworkPresenter::$viewModel is now ?ResponseModel = null,
  matching its nullable getter (real type bug fix — the property
  is genuinely unset before buildGraph() is called)
- All test methods in NetworkUseCaseTest and
  NetworkFunctionIntegrationTest get explicit : void return types
@alistair3149 alistair3149 marked this pull request as ready for review May 8, 2026 19:03
@alistair3149 alistair3149 merged commit 195737b into master May 8, 2026
7 checks passed
@alistair3149 alistair3149 deleted the modernise-php-types-and-namespaces branch May 8, 2026 19:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant