Skip to content

Commit

Permalink
Fix optional() macro __call (#20845)
Browse files Browse the repository at this point in the history
Optional@__call() is always called in place of
__call() pulled in from the Macroable trait.
  • Loading branch information
derekmd authored and taylorotwell committed Aug 30, 2017
1 parent 4276565 commit d3025d0
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/Illuminate/Support/Optional.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

class Optional
{
use Traits\Macroable;
use Traits\Macroable {
__call as macroCall;
}

/**
* The underlying object.
Expand Down Expand Up @@ -49,5 +51,9 @@ public function __call($method, $parameters)
if (is_object($this->value)) {
return $this->value->{$method}(...$parameters);
}

if (static::hasMacro($method)) {
return $this->macroCall($method, $parameters);
}
}
}
26 changes: 26 additions & 0 deletions tests/Support/SupportHelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use PHPUnit\Framework\TestCase;
use Illuminate\Support\Optional;

class SupportHelpersTest extends TestCase
{
Expand Down Expand Up @@ -768,6 +769,31 @@ public function something()
})->something());
}

public function testOptionalIsMacroable()
{
Optional::macro('present', function () {
if (is_object($this->value)) {
return $this->value->present();
}

return new Optional(null);
});

$this->assertNull(optional(null)->present()->something());

$this->assertEquals('$10.00', optional(new class {
public function present()
{
return new class {
public function something()
{
return '$10.00';
}
};
}
})->present()->something());
}

public function testTransform()
{
$this->assertEquals(10, transform(5, function ($value) {
Expand Down

0 comments on commit d3025d0

Please sign in to comment.