Skip to content

Add ActiveRecord::instantiate() method#546

Open
Tigrov wants to merge 5 commits intomasterfrom
add-instantiate
Open

Add ActiveRecord::instantiate() method#546
Tigrov wants to merge 5 commits intomasterfrom
add-instantiate

Conversation

@Tigrov
Copy link
Copy Markdown
Member

@Tigrov Tigrov commented Mar 16, 2026

Q A
Is bugfix? ✔️/❌
New feature? ✔️
Breaks BC? ✔️
Fixed issues

@Tigrov Tigrov added the severity:BC breaking Breaks backwards compatibility label Mar 16, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (2ebdf1b) to head (3573741).

Additional details and impacted files
@@             Coverage Diff             @@
##              master      #546   +/-   ##
===========================================
  Coverage     100.00%   100.00%           
- Complexity       658       660    +2     
===========================================
  Files             43        43           
  Lines           1638      1640    +2     
===========================================
+ Hits            1638      1640    +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@Tigrov Tigrov added the status:code review The pull request needs review. label Mar 16, 2026
@Tigrov Tigrov requested a review from a team March 16, 2026 11:34
@samdark samdark requested a review from Copilot March 16, 2026 12:10
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds a new ActiveRecord::instantiate() static method that uses ReflectionClass::newInstanceWithoutConstructor() to create ActiveRecord instances without invoking the constructor. This removes the previous limitation where ActiveRecord models with required constructor parameters couldn't be used with ActiveRecord::query(), RepositoryTrait, or relations that reference class names.

Changes:

  • Added instantiate() to ActiveRecordInterface and implemented it in AbstractActiveRecord using reflection to bypass the constructor
  • Updated ActiveQuery constructor and EventsTrait::query() to use instantiate() instead of new $modelClass()
  • Updated tests from expecting ArgumentCountError to asserting successful query/relation behavior, and updated documentation to reflect the new behavior

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/ActiveRecordInterface.php Added instantiate(): static method to the interface
src/AbstractActiveRecord.php Default implementation using ReflectionClass::newInstanceWithoutConstructor()
src/ActiveQuery.php Changed constructor to use $modelClass::instantiate() instead of new $modelClass()
src/Trait/EventsTrait.php Changed query() to use instantiate() instead of direct construction
tests/ActiveRecordTest.php Updated tests to verify constructors with required params now work with queries/relations
docs/create-model.md Replaced limitations section with a note about constructor not being called

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread docs/create-model.md Outdated
@Mister-42
Copy link
Copy Markdown

Thanks @Tigrov . I have tested this branch after my issue in #542, and it worked perfectly fine after adding suggested code. Good job

public static function instantiate(): static
{
	return new static();
}

@Tigrov Tigrov added status:ready for merge The pull request is OK to be merged. and removed status:code review The pull request needs review. labels Mar 24, 2026
@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 3, 2026

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 43b4a2c1-cc95-4274-8f9b-127c622028bb

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch add-instantiate

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

severity:BC breaking Breaks backwards compatibility status:ready for merge The pull request is OK to be merged.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants