deprecated strpos and PHP8.1 Compatibility

Created on 27 February 2023, over 1 year ago
Updated 28 February 2023, over 1 year ago

Problem/Motivation

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /var/www/html/web/core/lib/Drupal/Core/Routing/UrlGenerator.php on line 301

Module have to be patched by replacing this

if (strpos($path, '//') === 0) {
      $path = '/' . ltrim($path, '/');
    }

by

if (strpos($path ?? '', '//') === 0) {
      $path = '/' . ltrim($path, '/');
    }

L306 is concern to :
Deprecated: rawurlencode(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/html/web/core/lib/Drupal/Core/Routing/UrlGenerator.php on line 306

$path = str_replace($this->decodedChars[0], $this->decodedChars[1], rawurlencode($path));

by

$path = str_replace($this->decodedChars[0], $this->decodedChars[1], rawurlencode($path ?? ''));
πŸ› Bug report
Status

Postponed: needs info

Version

9.5

Component
RoutingΒ  β†’

Last updated 2 days ago

Created by

Live updates comments and jobs are added and updated live.
  • PHP 8.1

    The issue particularly affects sites running on PHP version 8.1.0 or later.

Sign in to follow issues

Comments & Activities

  • Issue created by @Nicoc
  • Status changed to Postponed: needs info over 1 year ago
  • πŸ‡ΊπŸ‡ΈUnited States cilefen

    What is passing a null path?

  • This is just a proposition, cause i saw this solution many times with this kind of errors with PHP8.1

    $path = str_replace($this->decodedChars[0], $this->decodedChars[1], rawurlencode($path ?? ''));
    

    in fact, many lines of this have to be patched for php 8.1 .

    all these lines are not compatible with PHP8.1 :

    L301 if (strpos($path, '//') === 0) {
    L306 $path = str_replace($this->decodedChars[0], $this->decodedChars[1], rawurlencode($path));
    L309 if (strpos($path, '/.') !== FALSE) {
    L3016 if ('/..' === substr($path, -3)) {
    
    and more...
    

    The best way for me is to test $path before you use it.

  • First commit to issue fork.
Production build 0.69.0 2024