-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix phpstan errors #1627
Fix phpstan errors #1627
Conversation
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
I can indeed see the following errors after doing
|
plugins/embed-optimizer/hooks.php
Outdated
wp_trigger_error( __FUNCTION__, esc_html( $message ) ); | ||
$function_name = __FUNCTION__; | ||
$trigger_error = static function ( string $message ) use ( &$function_name ): void { | ||
wp_trigger_error( $function_name, esc_html( $message ) ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch. This was wrong!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right, PHPSTan caught that - __FUNCTION__
is not the same inside a closure.
$trigger_error = static function ( string $message, int $error_level = E_USER_NOTICE ) use ( $this_function ): void { | ||
// Default to E_USER_NOTICE. | ||
if ( ! in_array( $error_level, array( E_USER_NOTICE, E_USER_WARNING, E_USER_ERROR, E_USER_DEPRECATED ), true ) ) { | ||
$error_level = E_USER_NOTICE; | ||
} | ||
wp_trigger_error( $this_function, esc_html( $message ), $error_level ); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's too bad that this doesn't seem to work:
/**
* Triggers error.
*
* @param string $message The message explaining the error.
* @param int $error_level Optional. The designated error type for this error.
*
* @phpstan-param E_USER_DEPRECATED|E_USER_ERROR|E_USER_NOTICE|E_USER_NOTICE $error_level
*/
$trigger_error = static function ( string $message, int $error_level = E_USER_NOTICE ) use ( $this_function ): void {
if ( ! in_array( $error_level, array( E_USER_ERROR, E_USER_NOTICE, E_USER_NOTICE, E_USER_DEPRECATED ), true ) ) {
throw new InvalidArgumentException( 'Invalid error level provided.' );
}
wp_trigger_error( $this_function, esc_html( $message ), $error_level );
};
If I do $trigger_error( 'foo', 181293 )
it doesn't trigger any PHPStan static analysis error.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But you do get a "InvalidArgumentException", right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I mean more this part:
@phpstan-param E_USER_DEPRECATED|E_USER_ERROR|E_USER_NOTICE|E_USER_NOTICE $error_level
I'm not getting any static analysis issues when passing an integer other than those.
Co-authored-by: Weston Ruter <westonruter@google.com>
Unit tests are failing in |
I've opened #1634 to address this. |
Summary
Fixes errors raised when running
composer phpstan
Relevant technical choices