Problem/Motivation

The Anonymous Token module is incompatible with Drupal 11.2 due to a PHP fatal error caused by improper property type inheritance in the AnonymousCsrfAccessCheck class.

Steps to reproduce

Install Drupal 11.2 with PHP 8.3
Install and enable the Anonymous Token module
Clear cache using drush cr or through the admin interface
Observe the following fatal error:

PHP Fatal error: Type of Drupal\anonymous_token\Access\AnonymousCsrfAccessCheck::$csrfToken must be Drupal\Core\Access\CsrfTokenGenerator (as in class Drupal\Core\Access\CsrfAccessCheck) in /var/www/html/web/modules/contrib/anonymous_token/src/Access/AnonymousCsrfAccessCheck.php on line 12

Root Cause: The AnonymousCsrfAccessCheck class extends CsrfAccessCheck but attempts to redeclare the inherited $csrfToken property with a different type (AnonymousCsrfTokenGenerator instead of CsrfTokenGenerator). This violates PHP's property inheritance rules, which became stricter in PHP 8.3.

Proposed resolution

Remove the property redeclaration from AnonymousCsrfAccessCheck class - the property is already inherited from the parent class
Ensure AnonymousCsrfTokenGenerator extends CsrfTokenGenerator to maintain type compatibility
Update the constructor to properly inject the anonymous token generator while respecting the parent class's property typing

Code changes needed:
In src/Access/AnonymousCsrfAccessCheck.php:

Remove the protected $csrfToken property declaration
Keep only the constructor that assigns the injected service to the inherited property

In src/Access/AnonymousCsrfTokenGenerator.php:

Ensure the class extends Drupal\Core\Access\CsrfTokenGenerator
Override necessary methods to provide anonymous user functionality

CommentFileSizeAuthor
#5 AfterMR.png63.43 KBanirudhsingh19
#5 BeforeMR.png66.51 KBanirudhsingh19
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

andresalvarez created an issue. See original summary.

andres alvarez’s picture

Status: Active » Needs review
anirudhsingh19’s picture

reviewing it

anirudhsingh19’s picture

Status: Needs review » Reviewed & tested by the community
StatusFileSize
new66.51 KB
new63.43 KB

I was able to reproduce the issue on Drupal 11.2.4 with PHP 8.3.23. After installing the module, the reported error does appear. After applying the MR, the error is no longer present. I’ve attached before and after screenshots, which confirm that the MR resolves the issue. Hence, moving it to RTBC.

baikho’s picture

Status: Reviewed & tested by the community » Needs work

baikho’s picture

Status: Needs work » Fixed

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.

Status: Fixed » Closed (fixed)

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