Problem/Motivation

Passing NULL to the $replace parameter of str_replace() and preg_replace() triggers a deprecation in PHP 8.1

Steps to reproduce

$replace: See https://3v4l.org/sikQR

Proposed resolution

Change NULL to an empty string

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Comments

alexpott created an issue. See original summary.

alexpott’s picture

Status: Active » Needs review
StatusFileSize
new4.99 KB
alexpott’s picture

Title: Passing NULL to the $replace parameter of str_replace() and preg_replace() triggers a deprecation in PHP 8.1 » Passing NULL to the $replace or $subject parameter of str_replace() and preg_replace() triggers a deprecation in PHP 8.1
Issue summary: View changes
StatusFileSize
new6.91 KB
new1.92 KB

Broadening this to include all cases where a _replace() function is triggering a deprecation.

alexpott’s picture

StatusFileSize
new982 bytes
new5.95 KB
  1. +++ b/core/modules/field/src/Plugin/migrate/process/d7/FieldBundle.php
    @@ -102,7 +102,7 @@ public function transform($value, MigrateExecutableInterface $migrate_executable
    -      $value = preg_replace('/comment_node_/', NULL, $bundle);
    +      $value = str_replace('comment_node_', '', $bundle);
    

    Changed to using str_replace because there's no reason to use the more expensive preg_replace.

  2. +++ b/core/modules/migrate/src/Plugin/migrate/destination/EntityConfigBase.php
    @@ -205,7 +205,7 @@ protected function updateEntity(EntityInterface $entity, Row $row) {
    -      $config_override->set(str_replace(Row::PROPERTY_SEPARATOR, '.', $row->getDestinationProperty('property')), $row->getDestinationProperty('translation'));
    +      $config_override->set(str_replace(Row::PROPERTY_SEPARATOR, '.', (string) $row->getDestinationProperty('property')), $row->getDestinationProperty('translation'));
    

    I think this one needs it's own issue because calling set() with an empty key doesn't make much sense.

daffie’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

catch’s picture

+++ b/core/lib/Drupal/Core/Extension/ModuleDependencyMessageTrait.php
@@ -39,7 +39,7 @@ public function checkDependencyMessage(array $modules, $dependency, Dependency $
 
       // Check if the module is incompatible with the dependency constraints.
-      $version = str_replace(\Drupal::CORE_COMPATIBILITY . '-', '', $modules[$dependency]->info['version']);
+      $version = str_replace(\Drupal::CORE_COMPATIBILITY . '-', '', $modules[$dependency]->info['version'] ?? '');
       if (!$dependency_object->isCompatible($version)) {

If $modules->$dependency->info['version'] is NULL, why try to replace anything?

alexpott’s picture

StatusFileSize
new982 bytes
new4.99 KB

Let's remove that change from this one and re-open #3239287: Fix \Drupal\Core\Extension\ModuleDependencyMessageTrait to not cause deprecations in PHP 8.1 and explore a bit more of what is going on in ModuleDependencyMessageTrait

Leaving at rtbc because only de-scoping something.

alexpott’s picture

Title: Passing NULL to the $replace or $subject parameter of str_replace() and preg_replace() triggers a deprecation in PHP 8.1 » Passing NULL to the $replace parameter of str_replace() and preg_replace() triggers a deprecation in PHP 8.1
Issue summary: View changes

We're now only dealing with the $replace parameter.

  • catch committed f680e8e on 9.3.x
    Issue #3239295 by alexpott: Passing NULL to the $replace parameter of...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed f680e8e and pushed to 9.3.x. Thanks!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.