refactor: trait relations through private helpers so host apps can alias#123
Merged
Conversation
Host applications that already expose `challenges()`, `streaks()`,
`experience()`, or `experienceHistory()` on their User model previously
could not adopt this package without a method-name collision. PHP's
`use Trait { method as alias; }` syntax handles the collision, but the
trait's own internals were still self-calling `$this->challenges()`
etc., which after aliasing resolves to the host's method and breaks
every flow.
Each trait now exposes its relation through a private helper that all
internal callers use, leaving the public method as a thin façade safe
to alias away:
- HasChallenges -> challengesRelation()
- HasStreaks -> streaksRelation()
- GiveExperience -> experienceRelation(), experienceHistoryRelation()
GiveExperience also previously relied on the `$this->experience`
magic property in several places, which Eloquent resolves via the
public method name and so breaks under aliasing as well. A new
`loadedExperience()` helper resolves the model once, caches it under
the canonical `experience` relation slot, and is used by addPoints,
deductPoints, setPoints, levelUp, getLevel, and getPoints. The
PointsIncreased dispatch path now receives the resolved model
explicitly instead of reading it back off the user.
Adds tests/Fixtures/AliasingUser that aliases every relation method
and throws from a host-defined stub, plus tests/Concerns/
TraitAliasingTest exercising enrollInChallenge, recordStreak,
addPoints, levelUp, and the inverse paths end-to-end against it.
Rector's TablePropertyToTableAttributeRector converted the fixture to use the #[Table(name: 'users')] attribute. That attribute is honored on Laravel 13 but silently ignored on Laravel 12.x, so the model falls back to its class-name default (`aliasing_users`), which has no matching table and breaks CI's prefer-lowest job.
522764b to
981b413
Compare
3 tasks
cjmellor
added a commit
that referenced
this pull request
May 24, 2026
[2.x] revert: drop trait method aliasing (#123)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A reviewer hit a wall trying to add
HasChallengesto their user model because they already defined their ownchallenges()relation. The PHP escape hatch for that collision is trait aliasing:…but our traits were self-calling
$this->challenges(),$this->streaks(),$this->experience(),$this->experienceHistory()internally. After the alias, those calls land on the host's method and every internal flow breaks.This PR makes each trait route its own internals through a private helper, leaving the public method as a thin façade that's safe to alias out of the way.
HasChallenges→challengesRelation()HasStreaks→streaksRelation()GiveExperience→experienceRelation(),experienceHistoryRelation()GiveExperiencealso leaned on the$this->experiencemagic property inaddPoints,deductPoints,setPoints,levelUp,getLevel,getPoints, and thePointsIncreaseddispatch path. Eloquent resolves that property by invoking the public method name, so aliasing broke it too. A newloadedExperience()helper resolves the model once, caches it under the canonicalexperiencerelation slot viasetRelation(), and is used by all of those callsites. The dispatch path now receives the resolved model explicitly instead of reading it back off$this.Pattern mirrors
HasAchievements::achievementsRelation()from #122 / commit a000bf4.Scope notes
main, not offfeat/configurable-table-names, so the package's beta testers on that branch aren't disturbed.HasAchievementsalready has its private helper landing via feat: configurable table names #122, so it's deliberately untouched here.HasTiers::getTier()calls$this->experience()— that's a cross-trait public-API dependency onGiveExperience, which is correct and outside this PR's scope.