Skip to content

Commit

Permalink
#95: deprecate invocation of
Browse files Browse the repository at this point in the history
  • Loading branch information
lstrojny committed Jul 1, 2016
1 parent d8f2696 commit 496f677
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
7 changes: 5 additions & 2 deletions src/Functional/With.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,20 @@
*
* @param mixed $value
* @param callable $callback
* @param bool $invokeValue Set to false to not invoke $value if it is a callable. Will be removed in 2.0
* @return mixed
*/
function with($value, callable $callback)
function with($value, callable $callback, $invokeValue = true)
{
InvalidArgumentException::assertCallback($callback, __FUNCTION__, 2);

if ($value === null) {
return null;
}

if (is_callable($value)) {
if ($invokeValue && is_callable($value)) {
trigger_error('Invoking the value is deprecated and will be removed in 2.0', E_USER_DEPRECATED);

$value = $value();
}

Expand Down
11 changes: 8 additions & 3 deletions tests/Functional/WithTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
namespace Functional\Tests;

use function Functional\with;
use PHPUnit_Framework_Error_Deprecated as DeprecatedError;

class WithTest extends AbstractTestCase
{
Expand All @@ -38,15 +39,17 @@ public function testWithNull()
public function testWithValue()
{
$this->assertSame(
'value',
with('value', function ($value) {
return $value;
2,
with(1, function ($value) {
return $value + 1;
})
);
}

public function testWithCallback()
{
DeprecatedError::$enabled = false;

$this->assertSame(
'value',
with(
Expand All @@ -58,6 +61,8 @@ function ($value) {
}
)
);

DeprecatedError::$enabled = true;
}

public function testPassNonCallable()
Expand Down

0 comments on commit 496f677

Please sign in to comment.