Skip to content
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

Bug/#5110 fix php81 warnings #5968

Merged
merged 11 commits into from
Dec 16, 2022
Merged

Bug/#5110 fix php81 warnings #5968

merged 11 commits into from
Dec 16, 2022

Conversation

kuasha420
Copy link
Contributor

@kuasha420 kuasha420 commented Oct 5, 2022

Summary

Addresses issue:

Relevant technical choices

The remaining strpos and str_replace errors are related to how we use add_submenu_page and will be addressed in #5998.

The remaining rtrim errors are caused by load_script_textdomain function and we are consuming the function correctly in our code. It's something that will probably be fixed in Core.

PR Author Checklist

  • My code is tested and passes existing unit tests.
  • My code has an appropriate set of unit tests which all pass.
  • My code is backward-compatible with WordPress 4.7 and PHP 5.6.
  • My code follows the WordPress coding standards.
  • My code has proper inline documentation.
  • I have added a QA Brief on the issue linked above.
  • I have signed the Contributor License Agreement (see https://cla.developers.google.com/).

Do not alter or remove anything below. The following sections will be managed by moderators only.

Code Reviewer Checklist

  • Run the code.
  • Ensure the acceptance criteria are satisfied.
  • Reassess the implementation with the IB.
  • Ensure no unrelated changes are included.
  • Ensure CI checks pass.
  • Check Storybook where applicable.
  • Ensure there is a QA Brief.

Merge Reviewer Checklist

  • Ensure the PR has the correct target branch.
  • Double-check that the PR is okay to be merged.
  • Ensure the corresponding issue has a ZenHub release assigned.
  • Add a changelog message to the issue.

@kuasha420 kuasha420 force-pushed the bug/#5110-fix-php81-warnings branch from d6eba0f to a192c4c Compare October 13, 2022 00:42
Copy link
Collaborator

@asvinb asvinb left a comment

Choose a reason for hiding this comment

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

@kuasha420 I left some comments in the PR. Can you check them out? Additionally, I see some other warnings cropping up which I think we can suppress:
image

Since we are supporting PHP 5, I think maybe we can add the #[ReturnTypeWillChange] attribute: https://php.watch/versions/8.1/ReturnTypeWillChange Curious to hear your thoughts @eugene-manuilov

There is also a warning here: https://github.com/google/site-kit-wp/blob/develop/includes/Modules/Thank_With_Google.php#L325 Maybe we can cast widgets to an array to prevent the warning.

@@ -367,7 +367,10 @@ private function get_screens() {
'render_callback' => function( Context $context ) {
$is_view_only = ! $this->authentication->is_authenticated();

$setup_slug = $context->input()->filter( INPUT_GET, 'slug', FILTER_SANITIZE_STRING );
$setup_slug = $context->input()->filter( INPUT_GET, 'slug' );
Copy link
Collaborator

Choose a reason for hiding this comment

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

@kuasha420 Is there a reason why we can't call htmlspecialchars here itself? 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The value of slug can be empty (null) and it throws another error for calling htmlspecialchars with null if we don't check.

Copy link
Collaborator

Choose a reason for hiding this comment

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

In that case @kuasha420 is it possible to use the Elvis operator here to be consistent with how it's done in other places where you used htmlspecialchars?

@@ -878,18 +878,14 @@ private function get_inline_tracking_data() {
* @return array The inline data to be output.
*/
private function get_inline_data() {
$current_user = wp_get_current_user();
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice!

@@ -277,17 +277,17 @@ protected function handle_provisioning_callback() {
}

// Check for a returned error.
$error = $input->filter( INPUT_GET, 'error', FILTER_SANITIZE_STRING );
$error = htmlspecialchars( $input->filter( INPUT_GET, 'error' ) );
Copy link
Collaborator

Choose a reason for hiding this comment

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

@kuasha420 I think error might be null which in that case PHP will complain about passing null to parameter #1 ($string) of type string is deprecated

@kuasha420
Copy link
Contributor Author

kuasha420 commented Oct 14, 2022

Thank you for the review, @asvinb !

The errors you mention, I am not seeing them in InstaWP. What's your php 8.1 version. Are those development only error?

I have addressed your other comments. Cheers.

@eugene-manuilov
Copy link
Collaborator

eugene-manuilov commented Oct 17, 2022

Since we are supporting PHP 5, I think maybe we can add the #[ReturnTypeWillChange] attribute: https://php.watch/versions/8.1/ReturnTypeWillChange Curious to hear your thoughts @eugene-manuilov

Good question, @asvinb. I lean toward using the ReturnTypeWillChange attribute although I would also like to hear thoughts from @aaemnnosttv.

@@ -367,7 +367,10 @@ private function get_screens() {
'render_callback' => function( Context $context ) {
$is_view_only = ! $this->authentication->is_authenticated();

$setup_slug = $context->input()->filter( INPUT_GET, 'slug', FILTER_SANITIZE_STRING );
$setup_slug = $context->input()->filter( INPUT_GET, 'slug' );
Copy link
Collaborator

Choose a reason for hiding this comment

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

In that case @kuasha420 is it possible to use the Elvis operator here to be consistent with how it's done in other places where you used htmlspecialchars?

@asvinb
Copy link
Collaborator

asvinb commented Oct 18, 2022

@kuasha420 I'am on PHP 8.1.9 where i can see those errors via QueryMonitor.

@kuasha420 kuasha420 force-pushed the bug/#5110-fix-php81-warnings branch from 556789e to 1578f30 Compare November 1, 2022 11:00
@kuasha420
Copy link
Contributor Author

kuasha420 commented Nov 1, 2022

@asvinb Thank you for giving me your PHP version.

However, I am not seeing the same errors in my local or an InstaWP site when using PHP 8.1 and rebased to latest develop. I am seeing different errors which are similar and coming from guzzlehttp package.

Other than that, I've also tried to add the attribute to the methods based off of your screenshot, but not sure if that's the correct approach and the attribute comment also conflicts with phpcs rules. I have not spent much time on it but looks like we need to upgrade our php codensiffer to properly use attributes without adding bunch of phpcs ignores around it.

        // phpcs:disable Squiz.Commenting.InlineComment.WrongStyle,Squiz.Commenting.FunctionComment.WrongStyle, Squiz.PHP.CommentedOutCode.Found
	#[\ReturnTypeWillChange]
	public function offsetUnset( $key ) {

Not sure how to proceed here. Cheers.

cc @eugene-manuilov @aaemnnosttv

@asvinb
Copy link
Collaborator

asvinb commented Nov 2, 2022

@kuasha420 I checked there are no no longer warnings from our code. I'll let @eugene-manuilov @aaemnnosttv check the ReturnTypeWillChange attribute

@eugene-manuilov
Copy link
Collaborator

Other than that, I've also tried to add the attribute to the methods based off of your screenshot, but not sure if that's the correct approach and the attribute comment also conflicts with phpcs rules. I have not spent much time on it but looks like we need to upgrade our php codensiffer to properly use attributes without adding bunch of phpcs ignores around it.

Yes, i think we can allow it in phpcs config file, but i am not sure how to do it. @aaemnnosttv do you know if it is feasible?

@aaemnnosttv
Copy link
Collaborator

@asvinb @eugene-manuilov Can we add the ignore inline with the attribute rather than around it?

@kuasha420 kuasha420 force-pushed the bug/#5110-fix-php81-warnings branch from 04ae767 to 0493f60 Compare November 30, 2022 10:06
@kuasha420
Copy link
Contributor Author

@aaemnnosttv I have updated the phpcs disables with ignores. Feels quite a bit repitetive but that's the best we can do for now. We should explore proper solution about using PHP attributes with phpcs in a follow up issue that I've created here. Assigning to you for a merge review. Cheers.

Copy link
Collaborator

@techanvil techanvil left a comment

Choose a reason for hiding this comment

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

Code looks good with just a couple of comments from me to take a look at.

I've given this another successful smoke test on a PHP 8.1 install, and the remaining work has been suitably captured in followup issues, so, minor comments aside this looks good to go.

public function offsetExists( $key ) {
// phpcs:ignore Squiz.Commenting.InlineComment.WrongStyle,Squiz.PHP.CommentedOutCode.Found
#[\ReturnTypeWillChange]
public function offsetExists( $key ) { // phpcs:ignore Squiz.Commenting.FunctionComment.WrongStyle
Copy link
Collaborator

Choose a reason for hiding this comment

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

When I run phpcs with PHP 8, these lines still raise errors:

image

Loosening up the rule definition allows linting to pass in PHP 8 as well as older versions.

Suggested change
public function offsetExists( $key ) { // phpcs:ignore Squiz.Commenting.FunctionComment.WrongStyle
public function offsetExists( $key ) { // phpcs:ignore Squiz.Commenting.FunctionComment

public function offsetGet( $key ) {
// phpcs:ignore Squiz.Commenting.InlineComment.WrongStyle,Squiz.PHP.CommentedOutCode.Found
#[\ReturnTypeWillChange]
public function offsetGet( $key ) { // phpcs:ignore Squiz.Commenting.FunctionComment.WrongStyle
Copy link
Collaborator

Choose a reason for hiding this comment

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

As above

Suggested change
public function offsetGet( $key ) { // phpcs:ignore Squiz.Commenting.FunctionComment.WrongStyle
public function offsetGet( $key ) { // phpcs:ignore Squiz.Commenting.FunctionComment

public function offsetSet( $key, $value ) {
// phpcs:ignore Squiz.Commenting.InlineComment.WrongStyle,Squiz.PHP.CommentedOutCode.Found
#[\ReturnTypeWillChange]
public function offsetSet( $key, $value ) { // phpcs:ignore Squiz.Commenting.FunctionComment.WrongStyle
Copy link
Collaborator

Choose a reason for hiding this comment

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

As above

Suggested change
public function offsetSet( $key, $value ) { // phpcs:ignore Squiz.Commenting.FunctionComment.WrongStyle
public function offsetSet( $key, $value ) { // phpcs:ignore Squiz.Commenting.FunctionComment

public function offsetUnset( $key ) {
// phpcs:ignore Squiz.Commenting.InlineComment.WrongStyle,Squiz.PHP.CommentedOutCode.Found
#[\ReturnTypeWillChange]
public function offsetUnset( $key ) { // phpcs:ignore Squiz.Commenting.FunctionComment.WrongStyle
Copy link
Collaborator

Choose a reason for hiding this comment

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

As above

Suggested change
public function offsetUnset( $key ) { // phpcs:ignore Squiz.Commenting.FunctionComment.WrongStyle
public function offsetUnset( $key ) { // phpcs:ignore Squiz.Commenting.FunctionComment

Comment on lines 72 to 74

$_GET['dirty'] = '<script>dirt</script>';
$this->assertEquals( 'dirt', $context->input()->filter( INPUT_GET, 'dirty', FILTER_SANITIZE_STRING ) );
$this->assertEquals( '&lt;script&gt;dirt&lt;/script&gt;', htmlspecialchars( $context->input()->filter( INPUT_GET, 'dirty' ) ) );
Copy link
Collaborator

Choose a reason for hiding this comment

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

This test doesn't seem so useful now as it's effectively just testing the standard htmlspecialchars function. I'd suggest it should be removed.

Suggested change
$_GET['dirty'] = '<script>dirt</script>';
$this->assertEquals( 'dirt', $context->input()->filter( INPUT_GET, 'dirty', FILTER_SANITIZE_STRING ) );
$this->assertEquals( '&lt;script&gt;dirt&lt;/script&gt;', htmlspecialchars( $context->input()->filter( INPUT_GET, 'dirty' ) ) );

Copy link
Collaborator

@techanvil techanvil left a comment

Choose a reason for hiding this comment

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

Nice one @kuasha420, LGTM!

@techanvil techanvil merged commit 8beca04 into develop Dec 16, 2022
@techanvil techanvil deleted the bug/#5110-fix-php81-warnings branch December 16, 2022 11:43
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.

5 participants