Make WordPress Core

Opened 9 months ago

Closed 9 months ago

#64699 closed defect (bug) (fixed)

Code Modernization: Replace void in PHPDoc union return types with null in link-template.php

Reported by: apermo Owned by: westonruter
Priority: normal Milestone: 7.0
Component: General Version: 4.3
Severity: normal Keywords: has-patch
Cc: Focuses:

Description

Following up on #64694 which fixed paginate_links(), this ticket addresses 13 instances in link-template.php where @return annotations incorrectly use void as part of a union type (e.g., @return string|void).

The |void pattern in this file was introduced in WordPress 4.3 via [32598] — "Fix doc blocks to link-template.php".

Why this matters

In PHP's type system, void means "the function does not return a value" and cannot be part of a union type (this is a compile error in PHP 8.0+). When a function uses bare return; or falls off the end without returning, the actual runtime value is null. Therefore @return Type|void should be @return Type|null.

This affects:

  • Static analysis tools (PHPStan, Psalm)
  • IDE autocompletion and type inference
  • Developer expectations about return values

Backward Compatibility

This is a safe, non-breaking change. As demonstrated by @westonruter in the ​#64694 PR review, return; and return null; are identical at runtime across every PHP version from 4.3.0 through 8.5.3: ​https://3v4l.org/3KQC8

Proposed Changes

For each instance:

  1. Update the @return annotation to replace void with null
  2. Change bare return; statements to return null;
  3. Update description text to say "null" instead of "void" / "nothing"

Affected Functions

Function Line Current Recommendation
edit_term_link() 1134 string|void string|null
get_delete_post_link() 1555 string|void string|null
get_edit_comment_link() 1603 string|void string|null
get_edit_bookmark_link() 1677 string|void string|null
get_adjacent_post_rel_link() 2060 string|void string|null
get_next_posts_page_link() 2518 string|void string|null
next_posts() 2543 string|void string|null
get_next_posts_link() 2566 string|void string|null
get_previous_posts_page_link() 2627 string|void string|null
previous_posts() 2649 string|void string|null
get_previous_posts_link() 2669 string|void string|null
get_next_comments_link() 3127 string|void string|null
get_previous_comments_link() 3199 string|void string|null

See #64694 for prior art.

Change History (7)

This ticket was mentioned in ​PR #11004 on ​WordPress/wordpress-develop by ​@apermo.


9 months ago
#1

  • Keywords has-patch added

Update 13 functions in link-template.php to use null instead of void in @return union types, and change bare return; statements to return null;.

In PHP's type system, void cannot be part of a union type. A bare return; actually returns null at runtime, so @return Type|null is the correct annotation.

Trac ticket: https://core.trac.wordpress.org/ticket/64699

## Use of AI Tools

Used AI for research, documentation and for the replacements. Everything was reviewed by myself and @xateman before opening this PR.

This ticket was mentioned in ​PR #11007 on ​WordPress/wordpress-develop by ​@pratiknawkar94.


9 months ago
#2

Trac ticket:

## Use of AI Tools

​@mukesh27 commented on ​PR #11007:


9 months ago
#3

Closing in favour of #11004

#4 @apermo
9 months ago

Props: @xate

Co-Authored and Reviewed

#5 @westonruter
9 months ago

  • Milestone Awaiting Review → 7.0
  • Owner set to westonruter
  • Status new → reviewing

​@westonruter commented on ​PR #11004:


9 months ago
#6

@noruzzamans Are you using AI to write ​https://github.com/WordPress/wordpress-develop/pull/11004#pullrequestreview-3844935325 and ​https://github.com/WordPress/wordpress-develop/pull/11022#pullrequestreview-3844853137? You should disclose this, if so, for example the model and how you prompted it. AI reviews can be helpful, but not if they don't add value; in the worst case, they can just add noise. (The em-dashes in your comment are a tell that AI was used.) Thanks!

#7 @westonruter
9 months ago

  • Resolution → fixed
  • Status reviewing → closed

In 61766:

Code Quality: Replace void with null in union return types in link-template.php.

Developed in ​https://github.com/WordPress/wordpress-develop/pull/11004

Follow-up to r61719, r61716, r32598.

Props apermo, xate, westonruter, noruzzaman.
See #64694, #64238.
Fixes #64699.

Note: See TracTickets for help on using tickets.