Skip to content

Commit 5db5f55

Browse files
author
Mark Challoner
committed
Throw InvalidReturnException if IfElse condition returns a non-boolean. Address CR requests.
1 parent 7352454 commit 5db5f55

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,8 @@ $data = [
583583

584584
Delegates to one expression or another depending on whether the specified condition strictly evaluates to true.
585585

586+
If the condition does not return a boolean an InvalidReturnException will be thrown.
587+
586588
#### Signature
587589

588590
```php
@@ -645,8 +647,6 @@ Test if all items in $data are even.
645647

646648
Delegates to one expression or another depending on whether the specified condition maps to null.
647649

648-
Deprecated. Use IfElse and Exists strategies instead.
649-
650650
#### Signature
651651

652652
```php

src/InvalidReturnException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace ScriptFUSION\Mapper;
3+
4+
/**
5+
* The exception that is thrown when an invalid return value from a callback is specified.
6+
*/
7+
class InvalidReturnException extends \RuntimeException
8+
{
9+
// Intentionally empty.
10+
}

src/Strategy/IfElse.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?php
22
namespace ScriptFUSION\Mapper\Strategy;
33

4+
use ScriptFUSION\Mapper\InvalidReturnException;
45
use ScriptFUSION\Mapper\Mapping;
56

67
/**
@@ -25,8 +26,8 @@ class IfElse extends Delegate
2526
*/
2627
public function __construct(callable $condition, $if, $else = null)
2728
{
28-
$this->condition = $condition;
2929
parent::__construct($if);
30+
$this->condition = $condition;
3031
$this->else = $else;
3132
}
3233

@@ -37,11 +38,19 @@ public function __construct(callable $condition, $if, $else = null)
3738
* @param mixed $data
3839
* @param mixed $context
3940
*
41+
* @throws InvalidReturnException
42+
*
4043
* @return mixed
4144
*/
4245
public function __invoke($data, $context = null)
4346
{
44-
if (call_user_func($this->condition, $data, $context) === true) {
47+
$return = call_user_func($this->condition, $data, $context);
48+
49+
if (!is_bool($return)) {
50+
throw new InvalidReturnException('Invalid return from condition: must be of type boolean.');
51+
}
52+
53+
if ($return === true) {
4554
return parent::__invoke($data, $context);
4655
}
4756

src/Strategy/IfExists.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,12 @@ class IfExists extends IfElse
2020
*/
2121
public function __construct(Strategy $condition, $if, $else = null)
2222
{
23-
parent::__construct(function ($data, $context = null) use ($condition) {
24-
return $this->delegate($condition, $data, $context) !== null;
25-
}, $if, $else);
23+
parent::__construct(
24+
function ($data, $context = null) use ($condition) {
25+
return $this->delegate($condition, $data, $context) !== null;
26+
},
27+
$if,
28+
$else
29+
);
2630
}
2731
}

test/Integration/Mapper/Strategy/IfElseTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace ScriptFUSIONTest\Integration\Mapper\Strategy;
33

44
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
5+
use ScriptFUSION\Mapper\InvalidReturnException;
56
use ScriptFUSION\Mapper\Mapper;
67
use ScriptFUSION\Mapper\Strategy\Exists;
78
use ScriptFUSION\Mapper\Strategy\IfElse;
@@ -38,8 +39,10 @@ public function testOnlyIf()
3839
self::assertNull($ifElse([]));
3940
}
4041

41-
public function testNonStrict()
42+
public function testStrictness()
4243
{
44+
$this->setExpectedException(InvalidReturnException::class);
45+
4346
$ifElse = (new IfElse(
4447
function () {
4548
return 1;
@@ -48,6 +51,6 @@ function () {
4851
'bar'
4952
))->setMapper(new Mapper);
5053

51-
self::assertSame('bar', $ifElse([]));
54+
$ifElse([]);
5255
}
5356
}

0 commit comments

Comments
 (0)