Error when first time running Analyse Site Code

Created on 17 July 2025, about 2 months ago

Problem/Motivation

On Drupal 11.2.2 install this module, and for first time, running the Analyse Site Code, clicking the update button throws error.

Further check:

The drupal-code-builder package version 4.5.1 throws a fatal PHP error when running on PHP 8.3:

```
Error: Call to a member function getReformattedText() on null in PhpParser\NodeVisitorAbstract@anonymous /var/www/project/drmqtt/vendor/drupal-code-builder/drupal-code-builder/Task/Collect/EventNamesCollector.php:55$36->enterNode() (line 63 of /var/www/project/drmqtt/vendor/drupal-code-builder/drupal-code-builder/Task/Collect/EventNamesCollector.php).
```

This error occurs because the `EventNamesCollector` class attempts to call `getReformattedText()` on a potentially null `DocComment` object, and this method may not exist in newer versions of the nikic/php-parser library (^4.18 || ^5.0) when used with PHP 8.3.

The issue prevents users from:
- Using Module Builder's code generation features
- Accessing the Module Builder admin interface
- Running any drupal-code-builder commands via Drush

Steps to reproduce

1. Install Drupal 11.x with PHP 8.3
2. Install drupal/module_builder module via Composer (which pulls in drupal-code-builder/drupal-code-builder 4.5.1)
3. Enable the module_builder module
4. Attempt to access `/admin/config/development/module-builder`
5. OR run any Module Builder drush command like `drush dcb:fixtures`
6. OR try to use Module Builder's code generation functionality

**Expected result:** Module Builder interface loads and functions work properly
**Actual result:** Fatal PHP error: "Call to a member function getReformattedText() on null"

**Environment:**
- PHP: 8.3.6
- Drupal: 11.2.2
- drupal-code-builder: 4.5.1
- nikic/php-parser: ^4.18 || ^5.0

Proposed resolution

Fix the null reference error in `EventNamesCollector.php` line 63 by:

1. **Add null checking** before calling methods on `getDocComment()`
2. **Use `getText()` instead of `getReformattedText()`** as the latter may not be available in newer php-parser versions
3. **Provide fallback for null cases**

**Proposed code change:**

```php
// Current problematic code (line 63):
$this->events[$class_name . '::' . $constant_node->consts[0]->name->name] = $constant_node->getDocComment()->getReformattedText();

// Fixed code:
$this->events[$class_name . '::' . $constant_node->consts[0]->name->name] = ($constant_node->getDocComment() ? $constant_node->getDocComment()->getText() : "");
```

This change:
- Checks if `getDocComment()` returns a non-null value
- Uses `getText()` method which is more stable across php-parser versions
- Provides empty string fallback for null doc comments

Remaining tasks

- [ ] Apply the proposed fix to `Task/Collect/EventNamesCollector.php`
- [ ] Test the fix with PHP 8.1, 8.2, and 8.3
- [ ] Test with different versions of nikic/php-parser (4.18+ and 5.0+)
- [ ] Verify Module Builder functionality still works correctly after the fix
- [ ] Update any related documentation if needed
- [ ] Consider adding unit tests to prevent similar regressions

User interface changes

None. This is a bug fix that restores existing functionality.

API changes

None. This is an internal fix that maintains the existing API.

Data model changes

None. This fix only affects error handling in the event collection process.

---

**Additional Notes:**

This issue affects PHP 8.3 compatibility and blocks users from utilizing Module Builder's code generation capabilities. The fix is minimal and maintains backward compatibility while resolving the fatal error.

**Related Issues:**
- This may be related to changes in nikic/php-parser library methods between versions
- Similar issues may exist in other parts of the codebase that use `getReformattedText()`

**Workaround:**
Until this is fixed, users can manually apply the patch or temporarily downgrade PHP version.

πŸ› Bug report
Status

Active

Version

4.1

Component

Code

Created by

πŸ‡²πŸ‡ΎMalaysia mimpro

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

  • Issue created by @mimpro
  • πŸ‡ΊπŸ‡ΈUnited States ultimike Florida, USA

    Why is this "Closed (won't fix)"? This is a real issue with PHP 8.3 and there appears to be a patch to fix it. What am I missing?

    Without support for PHP 8.3, this module isn't really compatible with Drupal 11.

    -mike

  • πŸ‡¬πŸ‡§United Kingdom joachim

    I don't think it's a PHP 8.3 problem, as that's what I have locally.

    There's an overload of information in this issue summary. Was it generated with AI?

  • πŸ‡ΊπŸ‡ΈUnited States ultimike Florida, USA

    I get this error when running Drupal 11.2 locally with PHP 8.3 (via DDEV) and the most recent version release version of this module. I can't speak to the original poster or the issue summary.

    -mike

  • πŸ‡¬πŸ‡§United Kingdom joachim

    Which version of nikic/php-parser do you have installed?

  • πŸ‡ΊπŸ‡ΈUnited States ultimike Florida, USA
    $ ddev composer show nikic/php-parser
    name     : nikic/php-parser
    descrip. : A PHP parser written in PHP
    keywords : parser, php
    versions : * v5.6.0
    released : 2025-07-27, this week
    type     : library
    license  : BSD 3-Clause "New" or "Revised" License (BSD-3-Clause) (OSI approved) https://spdx.org/licenses/BSD-3-Clause.html#licenseText
    homepage : 
    source   : [git] https://github.com/nikic/PHP-Parser.git 221b0d0fdf1369c71047ad1d18bb5880017bbc56
    dist     : [zip] https://api.github.com/repos/nikic/PHP-Parser/zipball/221b0d0fdf1369c71047ad1d18bb5880017bbc56 221b0d0fdf1369c71047ad1d18bb5880017bbc56
    path     : /var/www/html/vendor/nikic/php-parser
    names    : nikic/php-parser
  • πŸ‡¬πŸ‡§United Kingdom joachim

    Ok so the problem is this:

    $constant_node->getDocComment()->getReformattedText();
    

    It looks like an event constant is missing a doc comment.

    Could you debug to see which one it is?

    This is looking more and more like AI garbage:

    This error occurs because the `EventNamesCollector` class attempts to call `getReformattedText()` on a potentially null `DocComment` object, and this method may not exist in newer versions of the nikic/php-parser library (^4.18 || ^5.0) when used with PHP 8.3.

    @mimpro do not use AI in any of my issue queues again.

  • πŸ‡¬πŸ‡§United Kingdom joachim

    This was nothing to do with PHP 8.3, or with the version of nikic/php-parser.

    It's because there's an event with no docblock.

    @ultimike could you find which one it is and file a bug report for it on Drupal core (or whichever module) please?

    The AI-generated summary is total garbage, and led us down the wrong path.

    > 5. OR run any Module Builder drush command like `drush dcb:fixtures`

    This is garbage. The dcb:fixtures command is for development use only for updating test fixtures. It probably won't show the bug as it uses a small subset of code to analyse.

    > 6. OR try to use Module Builder's code generation functionality

    I repeat -- do NOT use AI in my issue queues.

    This is garbage. You can't use code generation until you've run analysis!

  • πŸ‡¬πŸ‡§United Kingdom joachim

    Released 4.5.3 with the fix.

  • πŸ‡ΊπŸ‡ΈUnited States ultimike Florida, USA

    Thanks, @joachim!

    In case anyone stumbles upon this issue in the future, the fix was in the drupal-code-builder/drupal-code-builder dependency. Here's the fix: https://github.com/drupal-code-builder/drupal-code-builder/commit/fe307b...

    -mike

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

  • πŸ‡²πŸ‡ΎMalaysia mimpro

    Thanks joachim. I was desperately try to get help, i afraid that my words will messing more on the problem and that is why i asked ai to do the formatting after i tried to get around. After posting, i changed my my mind so i want to delete this. But i couldnt find delete button and that is why i changed it to won't fix. Anyway you guys did seriously get into the solution and my respect for you guys. Thanks

Production build 0.71.5 2024