Fix preg_match() usage when looking for extension type

Created on 7 July 2022, over 2 years ago
Updated 3 July 2023, over 1 year ago

Problem/Motivation

https://www.php.net/manual/en/splfileobject.fgets.php may return False so it brings errors in case no type defined

TypeError thrown in /var/www/html/vendor/mglaman/phpstan-drupal/src/Drupal/ExtensionDiscovery.php on line 380 while loading bootstrap file /var/www/html/vendor/mglaman/phpstan-drupal/drupal-autoloader.php: preg_match(): Argument #2 ($subject) must be of type string, null given

Steps to reproduce

https://www.drupal.org/pift-ci-job/2420734 β†’ which is using fork of core file https://github.com/mglaman/phpstan-drupal/pull/442

Proposed resolution

- don't call preg_match() when no string is read from file

Remaining tasks

- agree on fix
- review/commit

User interface changes

no

API changes

no

Data model changes

no

Release notes snippet

no

πŸ› Bug report
Status

Needs work

Version

10.0 ✨

Component
BaseΒ  β†’

Last updated 1 day ago

Created by

πŸ‡«πŸ‡·France andypost

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.

  • Quick fix

    Very small and simple change. Preferred over Quickfix.

Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡ΈUnited States tedfordgif

    It definitely looks like an error would have been thrown and logged: SplFileObject->fgets() calls spl_filesystem_file_read_ex() with silent == false, which will throw a PHP exception if the file can't be read.

    That's the only place in the PHP source I see "Cannot read from file", so if we found that in logs it would be pretty clear.

    As to how that can happen in ExtensionDiscovery::scanDirectory(), I'd look for race conditions if we're running tests in parallel, or some other filesystem failure (network filesystem, running out of disk space, ...).

    @alexpott the assumption would seem to be that having a reference to an open SplFileObject (with a recent !eof check, no less) means you can read it without errors. That seems like a reasonable assumption to me, and matches most of the usage examples I've seen. However, perhaps we should consider adding a call to SplFileInfo->isReadable() before calling openFile(), and maybe logging our own error if not. There may be some minor performance implication if isReadable() and openFile() don't share a cache.

    I also find the differences between SplFileObject::eof() and ::valid interesting, although the latter is billed as !eof.

Production build 0.71.5 2024