Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Get rid of unnecessarily strict _doing_it_wrong().
  • Loading branch information
felixarntz committed Nov 10, 2023
commit e251d98747d87b3fef87ca8c73b9caf06682986b
16 changes: 2 additions & 14 deletions src/wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4702,26 +4702,14 @@ function _mce_set_direction( $mce_init ) {
* being made. It does not return true when a REST endpoint is hit as part of another request, e.g. for preloading a
* REST response. See {@see wp_is_rest_endpoint()} for that purpose.
*
* This function must not be called until the {@see 'parse_request'} action.
* This function should not be called until the {@see 'parse_request'} action, as the constant is only defined then,
* even for an actual REST request.
*
* @since 6.5.0
*
* @return bool True if it's a WordPress REST API request, false otherwise.
*/
function wp_is_rest_request(): bool {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Setting a return type slows this function down by about 20%

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sybrew I'm not sure where you get this from. In any case, I am sure the time this function takes is irrelevant in the grand scheme of things, since it's just a constant check.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@felixarntz every time a value is returned, its type is checked. This requires more processing time. Simply allowing to pass the value unchecked executes no additional code, so that's faster.

I'm trying to prevent this from becoming the norm. PHP isn't typed and nothing has been done to optimize type hinting thus far for userland. It's only a waste of resources.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sybrew That's fair. It probably requires a larger discussion, not in a specific ticket like this. I'm not sure what I think about it yet, but it looks like this would be the first return type in core anyway, so I'll just leave it out. Done in 32a27c6

Copy link

@sybrew sybrew Jan 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Crosstalk) There's also no need to crash a user's site because a wrong type is used during development. Just make sure this doesn't happen; it's easily achievable. We have PHPDoc, static code checks, and unit testing to help us with this. We write the code, so we can ascertain the return type without type hinting.

if ( ! defined( 'REST_REQUEST' ) && ! did_action( 'parse_request' ) ) {
_doing_it_wrong(
__FUNCTION__,
sprintf(
/* translators: %s: parse_request */
__( 'Detecting whether the current request is a REST API request is not possible until the %s hook.' ),
'parse_request'
),
'6.5.0'
);
return false;
}

return defined( 'REST_REQUEST' ) && REST_REQUEST;
}

Expand Down
9 changes: 2 additions & 7 deletions src/wp-includes/rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -3405,13 +3405,8 @@ function wp_is_rest_endpoint(): bool {
/* @var WP_REST_Server $wp_rest_server */
global $wp_rest_server;

/*
* Check whether this is a standalone REST request.
* This check is not using the `wp_is_rest_request()` function as it may be called before the constant is actually
* available. Since this is only one way of determining whether a REST endpoint is being handled, there is no need
* to be as strict here as in the check for whether this is an actual standalone REST request.
*/
$is_rest_endpoint = defined( 'REST_REQUEST' ) && REST_REQUEST;
// Check whether this is a standalone REST request.
$is_rest_endpoint = wp_is_rest_request();
if ( ! $is_rest_endpoint ) {
// Otherwise, check whether an internal REST request is currently being handled.
$is_rest_endpoint = isset( $wp_rest_server )
Expand Down