Skip to content

Add support for Doctrine Collections 3 - #200

Open
X-Coder264 wants to merge 1 commit into
liuggio:masterfrom
X-Coder264:support-collections-3
Open

X-Coder264 wants to merge 1 commit into
liuggio:masterfrom
X-Coder264:support-collections-3

Conversation

@X-Coder264

@X-Coder264 X-Coder264 commented Oct 26, 2026 •

Copy link
Copy Markdown

Fastest currently cannot load TestsQueue with Doctrine Collections 3: TestsQueue::add(): bool is incompatible with the native ArrayCollection::add(): void signature.

This adds Collections 3 support to the current 1.x line, retaining the existing ^1.2 and ^2.0 constraints and PHP ^8.0 requirement. An internal lazy loader selects the trait implementation from the installed parent's native return type:

  • Collections 1/2: add(): bool still returns true and emits exactly the existing deprecation from Doctrine collections compatibility #188
  • Collections 3: add(): void satisfies the parent's native signature

Path wrapping and existing TestSuite objects behave as before. TestsQueue still directly extends ArrayCollection, its private state stays in the same class, and existing subclasses remain compatible when using Collections 1/2. The README explains the return-type requirement for subclasses opting into Collections 3. This does not remove the legacy return value globally or require a Fastest 2.0 release.

The first implementation put two declarations of the same internal trait in an if/else in one file. It inspected the installed ArrayCollection::add() return type and declared either the bool or void variant. This worked at runtime, but upon review it became clear that both PHPStan 1.12.21 and 2.3.2 selected the first declaration (void) without evaluating the condition. Existing callers using the bool result and subclasses overriding add(): bool then failed static analysis even with Collections 1/2 installed. That would regress compatibility for existing 1.x users. The resulting method.childReturnType error in an existing subclass is non-ignorable, so adding an ignore annotation in Fastest would not address the consumer regression.

The replacement puts the two implementations in separate files under compat/. They still declare the same internal trait name, and only the matching file is loaded. Keeping these files outside the PSR-4 directories and the normal analysis roots avoids duplicate classmap entries and lets PHPStan resolve the active signature.

Composer's autoload.files loads a small bootstrap that calls spl_autoload_register() with a callback dedicated to that trait. When the trait is requested, the callback reflects the parent's native return type and requires the appropriate implementation. Registering the callback instead of choosing the variant immediately keeps ArrayCollection unloaded until needed, preserving lazy dependency loading. The callback is prepended to the autoloader stack so it can resolve the trait before Composer, including with an authoritative classmap where neither variant is mapped.

New tests cover construction, wrapping, identity, randomization, filtering, empty paths, native return types and the legacy deprecation. A separate workflow exercises Collections 1.2, 1.8, 2.0, 2.6, 3.0 and 3.1, including authoritative Composer autoloading, runtime and PHPStan checks for callers and subclasses, and the existing runner/Behat commands. The workflow uses PHPUnit 9.6 as a standalone PHAR so its Composer dependency on deep-copy does not prevent installing Collections 1.2; the XML runner and Behat preload that PHAR to access PHPUnit classes.

@X-Coder264
X-Coder264 force-pushed the support-collections-3 branch 3 times, most recently from 3ace93b to c117c58 Compare October 26, 2026 15:23
Select TestsQueue's add signature from ArrayCollection's native return
type. Keep the existing bool return value and deprecation with Collections
1/2, and declare void with Collections 3. Preserve the direct parent class
and private collection state for Fastest 1.x compatibility.

Load separate trait implementations lazily so static analysis sees only
the active signature and Composer classmaps remain unambiguous.

Cover path wrapping, object identity, randomization, filtering, return
values and deprecations. Check caller and subclass contracts at runtime
and with PHPStan across old and new Collections versions, including
authoritative Composer autoloading.
@X-Coder264
X-Coder264 force-pushed the support-collections-3 branch from c117c58 to 1701f91 Compare October 26, 2026 16:35
@toniperic

Copy link
Copy Markdown
Contributor

@DonCallisto any chance we could get this merged and released? 🙏

@DonCallisto

Copy link
Copy Markdown
Collaborator

I'm gonna review it ASAP, pinky promise :)

Comment on lines +7 to +8
// Keep the variants outside PSR-4 directories so classmaps and static analysis
// see only the installed native signature. Register lazily to preserve bootstrap behavior.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This comment is a little bit deceptive as this namespace is loaded through PSR-4. The only evidence that a trait or the other is loaded is via composer.json. I would prefer to have an explicit comment about that.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why a different workflow only for collections? Couldn't this be integrate in the main build.yaml, maybe with a little tweaking in order to run this kind of scenarios in a one-fit-all fashion?
I'm worried about those files to go "out-of-date" with versions and all related things

@DonCallisto

Copy link
Copy Markdown
Collaborator

Sorry i've reviewed it with my other (not personal) account but it's still me 😂

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.

4 participants