Running phpcs --standard=PHPCompatibility reports few issues with PHP compatibility.

Comments

peterkokot created an issue. See original summary.

peterkokot’s picture

StatusFileSize
new2.08 KB
mondrake’s picture

Title: Deprecated curly brace syntax for accessing array elements » PHP 7.4 Deprecated curly brace syntax for accessing array elements
Status: Active » Needs work
Issue tags: +PHP 7.4
Parent issue: » #3086374: Make Drupal 8 & 9 compatible with PHP 7.4
+++ b/core/modules/rest/src/RequestHandler.php
@@ -330,7 +330,7 @@ protected function getLegacyParameters(RouteMatchInterface $route_match, $unseri
     $parameters = [];
     // Filter out all internal parameters starting with "_".
     foreach ($route_parameters as $key => $parameter) {
-      if ($key{0} !== '_') {
+      if ($key[0] !== '_') {
         $parameters[] = $parameter;

This is not sufficient, we also need to cast explicitly $key to string because it could be an int, and in this case the array acccess would fail. See #3086374: Make Drupal 8 & 9 compatible with PHP 7.4.

peterkokot’s picture

StatusFileSize
new1.71 KB

Hello, yes, true. In the next patch file type casting to string has been added and checking if string starts with changed to substr usage...

mondrake’s picture

Status: Needs work » Needs review
mondrake’s picture

Version: 8.7.x-dev » 8.9.x-dev
mondrake’s picture

+++ b/core/modules/rest/src/RequestHandler.php
@@ -330,7 +330,8 @@ protected function getLegacyParameters(RouteMatchInterface $route_match, $unseri
     $parameters = [];
     // Filter out all internal parameters starting with "_".
     foreach ($route_parameters as $key => $parameter) {
-      if ($key{0} !== '_') {
+      $key = (string) $key;
+      if ((substr($key, 0, 1) !== '_')) {
         $parameters[] = $parameter;
       }
     }

Why overwriting $key and not just if ((string) $key[0] !== '_') {?

peterkokot’s picture

Otherwise for integers this notice is emitted: Trying to access array offset on value of type int...

$key could theoretically be also empty string. And then substr is a bit better pick instead of $key[0]. This might also work instead of overwriting the $key:

if ((substr((string) $key, 0, 1) !== '_')) {
mondrake’s picture

Yes, see https://3v4l.org/fEg4g.

Let's do #8 then.

peterkokot’s picture

StatusFileSize
new1.69 KB
mondrake’s picture

Status: Needs review » Reviewed & tested by the community

Looks good to me.

alexpott’s picture

Status: Reviewed & tested by the community » Fixed

Committed eb5e8d5 and pushed to 9.0.x. Thanks! >

Backported to 8.9.x and 8.8.x as low risk and no-change bug fix.

  • alexpott committed eb5e8d5 on 9.0.x
    Issue #3104420 by peterkokot, mondrake: PHP 7.4 Deprecated curly brace...

  • alexpott committed 34218a9 on 8.9.x
    Issue #3104420 by peterkokot, mondrake: PHP 7.4 Deprecated curly brace...

  • alexpott committed a31a15b on 8.8.x
    Issue #3104420 by peterkokot, mondrake: PHP 7.4 Deprecated curly brace...

Status: Fixed » Closed (fixed)

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