Skip to content

Commit 7a4dfaa

Browse files
Add implies as a logical assertion.
This assertion can be used to easily assert stuff like: > Assert::implies($hasDog, $leashAttached); Which will assert that $leashAttached is true, if $hasDog is true. This is a shorthand for `Assert::true(!$hasDog || $leashAttached)`.
1 parent 9c89b26 commit 7a4dfaa

File tree

5 files changed

+92
-0
lines changed

5 files changed

+92
-0
lines changed

README.md

+2
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ Method | Description
132132
`range($value, $min, $max, $message = '')` | Check that a value is within a range
133133
`inArray($value, array $values, $message = '')` | Check that a value is one of a list of values
134134
`oneOf($value, array $values, $message = '')` | Check that a value is one of a list of values (alias of `inArray`)
135+
`implies($p, $q, $message = '')` | Check that `$p` logically implies `$q` (i.e. _if_ `$p` is true, than `$q` must be true).
136+
135137

136138
### String Assertions
137139

src/Assert.php

+14
Original file line numberDiff line numberDiff line change
@@ -658,6 +658,20 @@ public static function notFalse($value, $message = '')
658658
}
659659
}
660660

661+
/**
662+
* @psalm-pure
663+
*
664+
* @param bool $p
665+
* @param bool $q
666+
* @param string $message
667+
*
668+
* @throws InvalidArgumentException
669+
*/
670+
public static function implies($p, $q, $message = '')
671+
{
672+
self::true(!$p || $q, $message ?: 'Logical implication $p => $q did not hold.');
673+
}
674+
661675
/**
662676
* @param mixed $value
663677
* @param string $message

src/Mixin.php

+22
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,28 @@ public static function nullOrNotFalse($value, $message = '');
677677
*/
678678
public static function allNotFalse($value, $message = '');
679679

680+
/**
681+
* @psalm-pure
682+
*
683+
* @param bool|null $p
684+
* @param bool $q
685+
* @param string $message
686+
*
687+
* @throws InvalidArgumentException
688+
*/
689+
public static function nullOrImplies($p, $q, $message = '');
690+
691+
/**
692+
* @psalm-pure
693+
*
694+
* @param iterable<bool> $p
695+
* @param bool $q
696+
* @param string $message
697+
*
698+
* @throws InvalidArgumentException
699+
*/
700+
public static function allImplies($p, $q, $message = '');
701+
680702
/**
681703
* @param mixed $value
682704
* @param string $message

tests/AssertTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,10 @@ public function getTests()
226226
array('range', array(2, 1, 2), true),
227227
array('range', array(0, 1, 2), false),
228228
array('range', array(3, 1, 2), false),
229+
array('implies', array(true, true), true),
230+
array('implies', array(true, false), false),
231+
array('implies', array(false, true), true),
232+
array('implies', array(false, false), true),
229233
array('oneOf', array(1, array(1, 2, 3)), true),
230234
array('oneOf', array(1, array('1', '2', '3')), false),
231235
array('inArray', array(1, array(1, 2, 3)), true),
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Webmozart\Assert\Tests\StaticAnalysis;
4+
5+
use Webmozart\Assert\Assert;
6+
7+
/**
8+
* @psalm-pure
9+
*
10+
* @param bool $p
11+
* @param bool $q
12+
*
13+
* @return bool
14+
*/
15+
function implies($p, $q)
16+
{
17+
Assert::implies($p, $q);
18+
19+
return $p;
20+
}
21+
22+
/**
23+
* @psalm-pure
24+
*
25+
* @param null|bool $p
26+
* @param bool $q
27+
*
28+
* @return null|bool
29+
*/
30+
function nullOrImplies($p, $q)
31+
{
32+
Assert::nullOrImplies($p, $q);
33+
34+
return $p;
35+
}
36+
37+
/**
38+
* @psalm-pure
39+
*
40+
* @param iterable<bool> $p
41+
* @param bool $q
42+
*
43+
* @return iterable<bool>
44+
*/
45+
function allImplies(iterable $value, $p, $q): iterable
46+
{
47+
Assert::allImplies($value, $p, $q);
48+
49+
return $value;
50+
}

0 commit comments

Comments
 (0)