Skip to content

Fix: Allow Avatar::create() to be called statically#185

Open
Copilot wants to merge 7 commits into
masterfrom
copilot/fix-avatar-create-method
Open

Fix: Allow Avatar::create() to be called statically#185
Copilot wants to merge 7 commits into
masterfrom
copilot/fix-avatar-create-method

Conversation

Copilot AI commented Jun 10, 2026

Copy link
Copy Markdown

Calling Laravolt\Avatar\Avatar::create() statically (e.g., via use Laravolt\Avatar\Avatar) throws a fatal error in PHP 8+ because create() is an instance method. Users who import the class directly instead of relying on the facade alias hit this.

Changes

  • src/Avatar.php: Moved create() logic into createAvatar() and added __call/__callStatic magic methods to intercept create() calls. Static calls instantiate a new Avatar; instance calls preserve existing configuration (cache, generator, themes from the service provider).
  • src/Facade.php: Added @method static PHPDoc annotations for IDE autocompletion.
  • tests/AvatarPhpTest.php: Added tests for static call usage.

Usage

Both patterns now work:

// Static call (previously broken)
Avatar::create('John Doe')->toBase64();

// Instance call (unchanged behavior)
$avatar = new Avatar($config, $cache);
$avatar->create('John Doe')->toBase64();

// Facade (unchanged, still works via service container)
\Avatar::create('John Doe')->toBase64();

Copilot AI added 2 commits June 10, 2026 18:55
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
Copilot AI requested a review from qisthidev June 10, 2026 18:56
@qisthidev qisthidev marked this pull request as ready for review June 11, 2026 02:08
Copilot AI review requested due to automatic review settings June 11, 2026 02:08
Co-authored-by: StyleCI Bot <bot@styleci.io>

Copilot AI left a comment

Copy link
Copy Markdown

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 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 in Avatar to support create() calls (instance and static) via a new createAvatar() implementation method.
  • Added facade @method PHPDoc 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 magic create() interception. Keeping it public expands the library's public API surface (consumers may start calling it directly), which will be harder to change later. Consider making it protected (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 thread src/Avatar.php Outdated
Comment thread src/Avatar.php Outdated
Comment thread src/Facade.php
Comment thread tests/AvatarPhpTest.php
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 thread src/Avatar.php
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.");
}
qisthidev and others added 3 commits June 11, 2026 10:42
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>
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.

Non-static method Laravolt\Avatar\Avatar::create() cannot be called statically

4 participants