Skip to content

Fix: Prevent autoloading non class-like files during discovery to avoid "FatalError: Cannot redeclare function" #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 14, 2025

Conversation

zdearo
Copy link
Contributor

@zdearo zdearo commented Aug 14, 2025

Description Summary

  • This PR resolves some points discussed in Cannot install, duplicate function declaration #67
  • Add a lightweight guard (fileHasClassLike) before calling class_exists during app discovery.
  • Skip files that do not contain a class/interface/trait/enum to prevent unintended autoloading of helpers or function-only files.

Motivation

  • class_exists triggered Composer’s autoload for helper files that only declare functions, which could lead to double-inclusion and a Fatal error: Cannot redeclare function.
  • Discovery only needs class-like types (Models, Controllers, Enums), so loading helpers is unnecessary and risky.

Changes

  • Introduce fileHasClassLike($path) with:

    • static in-memory cache per path;
    • cheap keyword pre-check to avoid tokenization when possible;
    • token-based detection (T_CLASS, T_INTERFACE, T_TRAIT, T_ENUM) to avoid false positives.
  • Use this guard in the discovery loop to skip non class-like files before invoking class_exists.

Fixes #67

introduce `fileHasClassLike` to ensure only class-like files are processed during installation
@zdearo zdearo changed the title Prevent autoloading non class-like files during discovery to avoid "FatalError: Cannot redeclare function" Fix: Prevent autoloading non class-like files during discovery to avoid "FatalError: Cannot redeclare function" Aug 14, 2025
@pushpak1300 pushpak1300 requested review from ashleyhindle and removed request for ashleyhindle August 14, 2025 09:09
Copy link
Collaborator

@ashleyhindle ashleyhindle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wonderful, thanks for finding & fixing this @zdearo, super helpful! 🫶

@ashleyhindle ashleyhindle merged commit 4363702 into laravel:main Aug 14, 2025
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cannot install, duplicate function declaration
2 participants