Problem/Motivation

the Path_alias module is not compatible with PHP8.1 and need to be patched :
core/modules/path_alias/src/PathProcessor/AliasPathProcessor.php

L54 if (strpos($path, '//') === 0) {

As some modules have already done, replace the code by

L54 if (strpos((string) $path, '//') === 0) {

Comments

Nicoc created an issue. See original summary.

cilefen’s picture

Status: Active » Postponed (maintainer needs more info)
Issue tags: -PHP 8.1 strpos +Needs steps to reproduce

`$path` is documented as having to be a string. What are the steps to reproduce?

   * @param string $path
   *   The path to process, with a leading slash.
nicoc’s picture

after i change the php version to 8.1 from 7.4, clear the cache, go to content page, and bim....

Deprecated: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in /var/www/html/web/core/modules/path_alias/src/PathProcessor/AliasPathProcessor.php on line 54

PHP 8.1 change the game, if a function ask for a string, we have to pass a string, and null is not a string.

The best way is to test the $path variable before use it.

cd : https://php.watch/versions/8.1/internal-func-non-nullable-null-deprecation

cilefen’s picture

Please explain the code path that is sending a null.

nicoc’s picture

I think that's not the point!

The goal is to secure the code, with PHP8.1, only the right type must be passed to a function, that the fact, whatever the way a null came to this function, it is necessary to manage, and anticipate this case, to guarantee to only pass a string type to a function that needs a string type in its declaration. :)

Thank you.

cilefen’s picture

Title: path_alias and PHP8.1 Compatibility » path_alias can be passed unexpected data types for the path

Why not type hint in the function signature?

nicoc’s picture

There may be a misunderstanding.

In core/modules/path_alias/src/PathProcessor/AliasPathProcessor.php

At L54, there is a strpos function, it is a PHP function. This function expects a string in its first parameter.

With PHP8.1 we have to pass a string, and just a string, null is not a string, so you have to secure your code, test the $path variable to be sure it is a string, and nothing other.

So, either test the $path variable before using the strpos function, or cast the $path variable, or do:

strpos($path ?? '', '//')

Version: 9.5.x-dev » 11.x-dev

Drupal core is moving towards using a “main” branch. As an interim step, a new 11.x branch has been opened, as Drupal.org infrastructure cannot currently fully support a branch named main. New developments and disruptive changes should now be targeted for the 11.x branch. For more information, see the Drupal core minor version schedule and the Allowed changes during the Drupal core release cycle.

smustgrave’s picture

So we run all the tests against 8.1, 8.3, and now 8.4 so if this module doesn't work in 8.1 can the summary be updated?

Going to leave in PNMI but if no follow up could be closed in 3+ months.

smustgrave’s picture

Status: Postponed (maintainer needs more info) » Closed (outdated)

Since there's been no follow up going to close out.

Now that this issue is closed, please review the contribution record.

As a contributor, attribute any organization that helped you, or if you volunteered your own time.

Maintainers, please credit people who helped resolve this issue.