Skip to content

Commit 9051325

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 9051325

File tree

5 files changed

+86
-0
lines changed

5 files changed

+86
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
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

Lines changed: 14 additions & 0 deletions
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 mixed $p
665+
* @param mixed $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

Lines changed: 22 additions & 0 deletions
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 mixed $p
684+
* @param mixed $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 mixed $p
695+
* @param mixed $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

Lines changed: 4 additions & 0 deletions
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),
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Webmozart\Assert\Tests\StaticAnalysis;
4+
5+
use Webmozart\Assert\Assert;
6+
7+
/**
8+
* @param mixed $p
9+
* @param mixed $q
10+
*
11+
* @return mixed
12+
*/
13+
function implies($p, $q)
14+
{
15+
Assert::implies($p, $q);
16+
17+
return $p;
18+
}
19+
20+
/**
21+
* @param mixed $p
22+
* @param mixed $q
23+
*
24+
* @return mixed
25+
*/
26+
function nullOrImplies($p, $q)
27+
{
28+
Assert::nullOrImplies($p, $q);
29+
30+
return $p;
31+
}
32+
33+
/**
34+
* @param mixed $p
35+
* @param mixed $q
36+
*
37+
* @return mixed
38+
*/
39+
function allImplies($p, $q)
40+
{
41+
Assert::allImplies($p, $q);
42+
43+
return $p;
44+
}

0 commit comments

Comments
 (0)