Fix: Allow Avatar::create() to be called statically#185
Open
Copilot wants to merge 7 commits into
Open
Conversation
Use __call and __callStatic magic methods to support both static and
instance usage of the create() method. When called statically
(Avatar::create('Name')), a new instance is created. When called on an
existing instance ($avatar->create('Name')), it uses the existing
instance preserving its configuration.
Also adds @method PHPDoc annotations to the Facade class for better IDE
support.
Copilot
AI
changed the title
[WIP] Fix non-static method call for avatar creation
Fix: Allow Avatar::create() to be called statically
Jun 10, 2026
Co-authored-by: StyleCI Bot <bot@styleci.io>
There was a problem hiding this comment.
Pull request overview
This PR addresses PHP 8+ fatal errors when consumers call Laravolt\Avatar\Avatar::create() statically by introducing magic-method handling for create() and adding tests/docs to support that usage.
Changes:
- Added
__call()/__callStatic()interception inAvatarto supportcreate()calls (instance and static) via a newcreateAvatar()implementation method. - Added facade
@methodPHPDoc annotations for improved IDE autocompletion. - Added PHPUnit coverage for static
Avatar::create()and fluent chaining after it.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
src/Avatar.php |
Routes create() through magic methods and delegates logic to createAvatar(). |
src/Facade.php |
Adds @method static annotations for common facade calls. |
tests/AvatarPhpTest.php |
Adds tests verifying static Avatar::create() usage and chaining. |
Comments suppressed due to low confidence (1)
src/Avatar.php:176
createAvatar()is an implementation detail introduced to support the magiccreate()interception. Keeping itpublicexpands the library's public API surface (consumers may start calling it directly), which will be harder to change later. Consider making itprotected(it’s still accessible from__call()/__callStatic()and by subclasses).
public function createAvatar(string $name): static
{
$this->name = $name;
$this->initTheme();
return $this;
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+586
to
+593
| #[Test] | ||
| public function it_can_be_called_statically() | ||
| { | ||
| $avatar = \Laravolt\Avatar\Avatar::create('John Doe'); | ||
|
|
||
| $this->assertInstanceOf(\Laravolt\Avatar\Avatar::class, $avatar); | ||
| $this->assertEquals('John Doe', $avatar->getAttribute('name')); | ||
| } |
Comment on lines
+125
to
+141
| /** | ||
| * Handle dynamic method calls on the instance. | ||
| * | ||
| * @return mixed | ||
| */ | ||
| public function __call(string $method, array $args): mixed | ||
| { | ||
| if ($method === 'create') { | ||
| if (empty($args) || ! is_string($args[0])) { | ||
| throw new \InvalidArgumentException('The create() method requires a string name argument.'); | ||
| } | ||
|
|
||
| return $this->createAvatar($args[0]); | ||
| } | ||
|
|
||
| throw new \BadMethodCallException("Method {$method} does not exist."); | ||
| } |
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
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.
Calling
Laravolt\Avatar\Avatar::create()statically (e.g., viause Laravolt\Avatar\Avatar) throws a fatal error in PHP 8+ becausecreate()is an instance method. Users who import the class directly instead of relying on the facade alias hit this.Changes
src/Avatar.php: Movedcreate()logic intocreateAvatar()and added__call/__callStaticmagic methods to interceptcreate()calls. Static calls instantiate a new Avatar; instance calls preserve existing configuration (cache, generator, themes from the service provider).src/Facade.php: Added@method staticPHPDoc annotations for IDE autocompletion.tests/AvatarPhpTest.php: Added tests for static call usage.Usage
Both patterns now work: