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

Adjust exclude rules filtering to avoid passing null to strlen() to fix PHP 8 deprecation notice #1466

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

ocean90
Copy link
Contributor

@ocean90 ocean90 commented Nov 3, 2023

Fixes #1349.

This fixes a deprecation notice in PHP 8:

PHP Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated in classes/class-log.php on line 217

The value of my exclude rule was:

Array
(
    [connector] =>
    [context] =>
    [action] =>
    [ip_address] => 127.0.0.1
    [author] =>
    [role] =>
)

The fix is to check for null first.

Checklist

  • Project documentation has been updated to reflect the changes in this pull request, if applicable.
  • I have tested the changes in the local development environment (see contributing.md).
  • I have added phpunit tests.

Release Changelog

  • Fix: Avoid deprecation notices in PHP 8.

Release Checklist

  • This pull request is to the master branch.
  • Release version follows semantic versioning. Does it include breaking changes?
  • Update changelog in readme.txt.
  • Bump version in stream.php.
  • Bump Stable tag in readme.txt.
  • Bump version in classes/class-plugin.php.
  • Draft a release on GitHub.

Change [ ] to [x] to mark the items as done.

Copy link
Contributor

@kasparsd kasparsd left a comment

Choose a reason for hiding this comment

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

Thanks for the pull request @ocean90!

It appears that the proposed change also adjusts the business logic -- could we update it to keep the previous logic, please?

$exclude_rules = array_filter(
$exclude,
static function ( $value ) {
return null === $value || strlen( (string) $value ) > 0;
Copy link
Contributor

Choose a reason for hiding this comment

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

This logic now allows null values to be included in the filtered output:

$list = [ 1, 'two', false, null, 0 ];

$rules = array_filter(
    $list,
    function ( $value ) {
        return null === $value || strlen( (string) $value ) > 0;
    }
);

var_dump( $rules );

returns:

array(4) {
  [0]=>
  int(1)
  [1]=>
  string(3) "two"
  [3]=>
  NULL
  [4]=>
  int(0)
}

while we would expect the null value to be removed.

Could we change this to strlen( (string) $value ) > 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think you're reading the previous logic wrong. Previously null was also allowed which is why I added the specific check for null. Removing that would actually be a change in the logic.

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.

PHP 8.1: PHP Deprecated: strlen(): Passing null to parameter #1 ($string) of type string is deprecated
2 participants