Skip to content

Drupal URL and query extender return types - #577

Merged
AJenbo merged 6 commits into
PHPantom-dev:mainfrom
giuseppe-esposito-87:drupal-dr21
Nov 23, 2026
Merged

AJenbo merged 6 commits into
PHPantom-dev:mainfrom
giuseppe-esposito-87:drupal-dr21

Conversation

@giuseppe-esposito-87

Copy link
Copy Markdown
Contributor

🤖 This was written by an AI agent on behalf of @giuseppe-esposito-87 (claude-code/claude-opus-5-5).

Adds DR21: PHPantom gives precise return types to core methods whose docblocks are wider than what the call returns.

$path = $url->toString();                  // string, was string|GeneratedUrl
$meta = $url->toString(TRUE);              // GeneratedUrl
$query = $db->select('node', 'n')
  ->extend(PagerSelectExtender::class)     // PagerSelectExtender, was ExtendableInterface
  ->limit(10);                             // no longer an unknown method

What changes

  • URLs: Url::toString(), UrlGeneratorInterface::generateFromRoute() and UnroutedUrlAssemblerInterface::assemble() return a string, and a GeneratedUrl only when $collect_bubbleable_metadata is TRUE. A bool variable keeps the union.
  • Query extenders: extend() returns the extender class it is given, chains included. A plain string argument keeps ExtendableInterface.
  • Implementations that only say {@inheritdoc}, such as UrlGenerator, Select and SelectExtender, get the same types.
  • Each correction applies only while core still declares the wide type, so a core that narrows it keeps its own.

PHPStan's Drupal extension corrects only Url::toString(); generateFromRoute() and assemble() take the same flag, so they are included. The roadmap names toString() only.

A repeated identical call in one method, such as a second $url->toString(), is still typed wide. That is an engine bug outside Drupal, fixed in a separate PR.

Checked

  • analyze on Drupal core 11.4: 376 reports are gone and 3 are new. One new report is a real mismatch in core: StringDatabaseStorage declares @return Select but returns a PagerSelectExtender. The others are existing reports reworded.
  • analyze on a site with the redirect and flag modules: 421 gone, 5 new, the same kinds.

How to test

Run composer install in examples/drupal, open it with this branch's build, and follow the "Try:" comments:

  • web/modules/custom/acme/src/CoreLinks.php, firstNodePath(): the plain toString() passed to a string parameter is not reported, and $generated-> completes getGeneratedUrl().
  • web/modules/custom/acme/src/NoteCounter.php, firstPageIds(): complete after ->extend(PagerSelectExtender::class)-> to see limit().

Checklist

If applicable:

  • I have updated CHANGELOG.md
  • I have updated the documentation (README.md, docs/, examples/)
  • I have updated the config schema (config-schema.json). Not applicable: this PR adds or changes no setting.
  • I have added/updated tests to cover my changes
  • If I used AI tools, I have reviewed what they produced and can explain
    the changes and why they are needed.
  • I have read through any AI-generated text, especially documentation,
    the changelog, and other user-facing content, and removed or corrected
    anything inaccurate, unclear, or unnecessary.

Core declares extend() on a query to return ExtendableInterface, so
->extend(PagerSelectExtender::class)->limit(10) reported limit() as
unknown. Make the declaring interface's extend() generic over the class
string it takes; the select and extender classes inherit it through
{@inheritdoc}. A string that is not a class literal still gives the
interface, and a core that changes the declaration keeps its own.

Assisted-by: claude-code/claude-opus-5-5
Assisted-by: claude-code/claude-opus-5-5
Url::toString(), generateFromRoute() and assemble() declare
string|GeneratedUrl, but return a GeneratedUrl only when
$collect_bubbleable_metadata is TRUE. Key their return type on that flag.

Assisted-by: claude-code/claude-opus-5-5
Assisted-by: claude-code/claude-opus-5-5
Assisted-by: claude-code/claude-opus-5-5
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

❌ Patch coverage is 96.10390% with 3 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/stub_patches/drupal.rs 95.89% 3 Missing ⚠️

📢 Thoughts on this report? Let us know!

The conditional return on the URL methods built `true` by hand;
PhpType::true_() hands out the interned keyword type instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@AJenbo AJenbo modified the milestones: Sprint 6, Sprint 8 Nov 23, 2026
@AJenbo
AJenbo merged commit 294c458 into PHPantom-dev:main Nov 23, 2026
15 checks passed
@AJenbo

AJenbo commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Thanks

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.

3 participants