Skip to content

Commit

Permalink
Merge pull request #37 from artemmolotov/fix-checking-method-in-isSta…
Browse files Browse the repository at this point in the history
…ticCallToNonStaticMethod

Check "static" callable for method existing in CallableResolver
  • Loading branch information
mnapoli authored Jul 30, 2021
2 parents 96fb9c2 + eadd74c commit 5214cbe
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/CallableResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,11 @@ private function isStaticCallToNonStaticMethod($callable): bool
{
if (is_array($callable) && is_string($callable[0])) {
[$class, $method] = $callable;

if (! method_exists($class, $method)) {
return false;
}

$reflection = new ReflectionMethod($class, $method);

return ! $reflection->isStatic();
Expand Down
25 changes: 25 additions & 0 deletions tests/InvokerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,16 @@ public function cannot_invoke_magic_method()
$this->invoker->call([new InvokerTestMagicMethodFixture, 'foo']);
}

/**
* @test
*/
public function cannot_invoke_static_magic_method()
{
$this->expectExceptionMessage('Invoker\Test\InvokerTestStaticMagicMethodFixture::foo() is not a callable. A __call() or __callStatic() method exists but magic methods are not supported.');
$this->expectException(NotCallableException::class);
$this->invoker->call([InvokerTestStaticMagicMethodFixture::class, 'foo']);
}

/**
* @test
*/
Expand Down Expand Up @@ -503,3 +513,18 @@ public function __call(string $name, array $args): string
throw new Exception('Unknown method');
}
}

class InvokerTestStaticMagicMethodFixture
{
/** @var bool */
public static $wasCalled = false;
public static function __callStatic(string $name, array $args): string
{
if ($name === 'foo') {
static::$wasCalled = true;
return 'bar';
}
throw new Exception('Unknown method');
}
}

0 comments on commit 5214cbe

Please sign in to comment.