Skip to content

Avoid passing null to trim()#1698

Merged
amieiro merged 4 commits into
developfrom
fix/trim-null
Sep 26, 2023
Merged

Avoid passing null to trim()#1698
amieiro merged 4 commits into
developfrom
fix/trim-null

Conversation

@swissspidy

Copy link
Copy Markdown
Member

Problem

I got some PHP deprecation notices:

PHP Deprecated:  trim(): Passing null to parameter #1 ($string) of type string is deprecated in /path/to/gp-includes/thing.php on line 689

Solution

Set variable to an empty string if null.

To-do

  • Task 1 (Replace with your task description)

Testing Instructions

Screenshots or screencast

@swissspidy swissspidy added the [Type] Bug An existing feature is broken. label Sep 23, 2023
@amieiro

amieiro commented Sep 25, 2023

Copy link
Copy Markdown
Member

Hi @swissspidy

Using PHP8.2 (I think we have the same warning with PHP8.1) I was able to reproduce the first issue in the thing.php file

PHP Deprecated: trim(): Passing null to parameter #1 ($string) of type string is deprecated in /local-path/glotpress/wp-content/plugins/GlotPress/gp-includes/thing.php on line 689

But I couldn't reproduce the problem in the original.php file. Could you explain how to reproduce it? Thank you

akirk
akirk previously approved these changes Sep 25, 2023

@akirk akirk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you!

@swissspidy

Copy link
Copy Markdown
Member Author

But I couldn't reproduce the problem in the original.php file. Could you explain how to reproduce it? Thank you

All those errors occurred after setting up GlotPress on a fresh install and then creating a new project and importing originals + translations. I actually just found a third one this way.

@pedro-mendonca

Copy link
Copy Markdown
Member

We could find even more if we add PHPStan testing :)

@amieiro amieiro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I was able to reproduce these 3 problems when I create a new project, import its originals and did one translation:

PHP Deprecated:  trim(): Passing null to parameter #1 ($string) of type string is deprecated in /wordpress/glotpress2/wp-content/plugins/GlotPress/gp-includes/thing.php on line 689

PHP Deprecated:  mb_strlen(): Passing null to parameter #1 ($string) of type string is deprecated in /Users/amieiro/code/wordpress/glotpress2/wp-content/plugins/GlotPress/gp-includes/things/original.php on line 215

PHP Deprecated:  preg_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated in /wordpress/glotpress2/wp-content/plugins/GlotPress/gp-templates/helper-functions.php on line 416

After apply this PR, I didn't get the errors, so it is ok for me.

@amieiro amieiro merged commit 7e98b14 into develop Sep 26, 2023
@amieiro amieiro deleted the fix/trim-null branch September 26, 2023 09:16
Comment on lines +214 to +216
if ( ! $entry->context ) {
$entry->context = '';
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This introduced a bug, see #1724. This should better be solved by checking for is_string() before calling mb_strlen().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Type] Bug An existing feature is broken.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants