Skip to content

Commit e975603

Browse files
k4emicdbu
authored andcommitted
Stop condition evaluation on first failure (#112)
* Stop condition evaluation on first failure The `&=` operator will convert the value to an integer (see some examples here: https://stackoverflow.com/questions/17553004/boolean-assignment-operators-in-php) which causes the condition evaluation to not stop on the first failure, as otherwise noted.
1 parent 9fcc4c9 commit e975603

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010

1111
- Support for `nyholm/psr7` version 1.0.
1212

13+
### Fixed
14+
15+
- Fixed condition evaluation
16+
1317
## 1.4.0 - 2018-02-06
1418

1519
### Added

spec/ClassDiscoverySpec.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,16 @@ function it_returns_a_class()
4444

4545
function it_validates_conditions() {
4646
$c0 = ['class' => 'ClassName0', 'condition' => false];
47+
$c0b = ['class' => 'ClassName0', 'condition' => [false, true]];
48+
$c0c = ['class' => 'ClassName0', 'condition' => [true, false]];
49+
$c0d = ['class' => 'ClassName0', 'condition' => [true, false, true]];
50+
$c0e = ['class' => 'ClassName0', 'condition' => [false, function() {
51+
throw new \RuntimeException('This should never be called');
52+
}]];
4753
$c1 = ['class' => 'ClassName1', 'condition' => true];
4854
$c2 = ['class' => 'ClassName2', 'condition' => false];
49-
DiscoveryHelper::setClasses('Foobar', [$c0, $c1, $c2]);
55+
$c3 = ['class' => 'ClassName3', 'condition' => true];
56+
DiscoveryHelper::setClasses('Foobar', [$c0, $c0b, $c0c, $c0d, $c0e, $c1, $c2, $c3]);
5057

5158
$this->find('Foobar')->shouldReturn('ClassName1');
5259
}

src/ClassDiscovery.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -167,14 +167,14 @@ protected static function evaluateCondition($condition)
167167
} elseif (is_bool($condition)) {
168168
return $condition;
169169
} elseif (is_array($condition)) {
170-
$evaluatedCondition = true;
171-
172170
// Immediately stop execution if the condition is false
173-
for ($i = 0; $i < count($condition) && false !== $evaluatedCondition; ++$i) {
174-
$evaluatedCondition &= static::evaluateCondition($condition[$i]);
171+
for ($i = 0; $i < count($condition); ++$i) {
172+
if (false === static::evaluateCondition($condition[$i])) {
173+
return false;
174+
}
175175
}
176176

177-
return $evaluatedCondition;
177+
return true;
178178
}
179179

180180
return false;

0 commit comments

Comments
 (0)