Problem/Motivation

In scope of PHP8.1 compatibility static analysis it was discovered some deprecations in Drupal Core codebase:
1. 60 docroot/core/lib/Drupal/Core/Installer/NormalInstallerServiceProvider.php get_called_class() without argument
2. 33, 50 docroot/core/lib/Drupal/Core/Render/Element/StatusMessages.php get_class() without argument
3. 88 docroot/core/modules/system/src/Plugin/Block/SystemMenuBlock.php get_class() without argument
4. 780 docroot/core/lib/Drupal/Core/Url.php get_class() without argument

Proposed resolution

According to https://wiki.php.net/rfc/deprecations_php_8_1 remove argument-less get_class() and get_called_class() usage in favor of the dedicated self::class, parent::class and static::class syntax.

CommentFileSizeAuthor
#4 3351004-4.patch3.05 KBanchal_gupta

Issue fork drupal-3351004

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

Natallia created an issue. See original summary.

Natallia’s picture

vishal choudhary’s picture

Related to -> 4. 780 docroot/core/lib/Drupal/Core/Url.php get_class() without argument.
Example:
If you want to get the class name of the current object, you need to replace the following code:
1. get_class();

$class = get_class();

Should be replaced with

$class = get_class($this);

2. get_called_class();

$class = get_called_class();

Should be Replaced with

$class = get_called_class(self::class);
anchal_gupta’s picture

StatusFileSize
new3.05 KB

I have uploaded the patch.
Addressed #3
Please review it.

anchal_gupta’s picture

Status: Active » Needs review

Natallia’s picture

As It is implies that functionality of code will not be touched:
The changes 1. get_class() -> get_class($this) and 2. get_called_class() -> get_called_class(self::class) are both not equivalent (e.g. see Example2 from https://www.php.net/manual/en/function.get-class.php)
and the changes in the merge request are taken from proposal here https://wiki.php.net/rfc/deprecations_php_8_1

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Removing credit for #4 as it's expected to test your path before uploading.

Reviewing MR 3741
Searched for get_class() found 5 instances that were covered
Searched for get_called_class() found 1 that was covered
Searched for get_parent_class and found 0

Looks good

Good work!

  • catch committed decf4529 on 10.1.x
    Issue #3351004 by Natallia: Deprecations for PHP 8.1 get_class() and...

  • catch committed d6cdd674 on 10.0.x
    Issue #3351004 by Natallia: Deprecations for PHP 8.1 get_class() and...

  • catch committed dcf7796a on 9.5.x
    Issue #3351004 by Natallia: Deprecations for PHP 8.1 get_class() and...
catch’s picture

Status: Reviewed & tested by the community » Fixed

Committed/pushed to 10.1.x, cherry-picked to 10.0.x and 9.5.x, thanks!

Status: Fixed » Closed (fixed)

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