Skip to content

Commit

Permalink
Fix call undefined method
Browse files Browse the repository at this point in the history
  • Loading branch information
ricardogobbosouza committed Mar 16, 2024
1 parent d54ebc5 commit 9995c9b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/Money.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cknow\Money;

use BadMethodCallException;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\Jsonable;
use Illuminate\Contracts\Support\Renderable;
Expand Down Expand Up @@ -190,15 +191,21 @@ public function __toString()
*
* @param string $method
* @return \Cknow\Money\Money|\Cknow\Money\Money[]|mixed
*
* @throws \BadMethodCallException
*/
public function __call($method, array $arguments)
{
if (static::hasMacro($method)) {
return $this->macroCall($method, $arguments);
}

if (! method_exists($this->money, $method)) {
return $this;
if (!method_exists($this->money, $method)) {
throw new BadMethodCallException(sprintf(
'Call to undefined method %s::%s()',
static::class,
$method
));
}

$result = call_user_func_array([$this->money, $method], static::getArguments($arguments));
Expand All @@ -210,7 +217,7 @@ public function __call($method, array $arguments)
'absolute', 'negative',
];

if (! in_array($method, $methods)) {
if (!in_array($method, $methods)) {
return $result;
}

Expand Down Expand Up @@ -268,7 +275,7 @@ private static function getArguments(array $arguments = [])
*/
private static function convertResult($result)
{
if (! is_array($result)) {
if (!is_array($result)) {
return static::convert($result);
}

Expand Down Expand Up @@ -305,7 +312,7 @@ private static function resolveCalculator()
];

foreach ($calculators as $calculator) {
if (! class_exists($calculator)) {
if (!class_exists($calculator)) {
continue;
}

Expand Down
6 changes: 5 additions & 1 deletion tests/MoneyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Cknow\Money\Tests;

use BadMethodCallException;
use Cknow\Money\Money;
use Money\Currency;

Expand Down Expand Up @@ -191,7 +192,10 @@ public function testAllocateTo()

public function testCallUndefinedMethod()
{
static::assertEquals(Money::USD(15), Money::USD(15)->undefined());
$this->expectException(BadMethodCallException::class);
$this->expectExceptionMessage('Call to undefined method ' . Money::class . '::undefined()');

Money::USD(15)->undefined();
}

public function testGetters()
Expand Down

0 comments on commit 9995c9b

Please sign in to comment.