Skip to content

Commit

Permalink
Add implies as a logical assertion.
Browse files Browse the repository at this point in the history
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)`.
  • Loading branch information
roelvanduijnhoven committed Jan 25, 2021
1 parent 9c89b26 commit 7a4dfaa
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ Method | Description
`range($value, $min, $max, $message = '')` | Check that a value is within a range
`inArray($value, array $values, $message = '')` | Check that a value is one of a list of values
`oneOf($value, array $values, $message = '')` | Check that a value is one of a list of values (alias of `inArray`)
`implies($p, $q, $message = '')` | Check that `$p` logically implies `$q` (i.e. _if_ `$p` is true, than `$q` must be true).


### String Assertions

Expand Down
14 changes: 14 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,20 @@ public static function notFalse($value, $message = '')
}
}

/**
* @psalm-pure
*
* @param bool $p
* @param bool $q
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function implies($p, $q, $message = '')
{
self::true(!$p || $q, $message ?: 'Logical implication $p => $q did not hold.');
}

/**
* @param mixed $value
* @param string $message
Expand Down
22 changes: 22 additions & 0 deletions src/Mixin.php
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,28 @@ public static function nullOrNotFalse($value, $message = '');
*/
public static function allNotFalse($value, $message = '');

/**
* @psalm-pure
*
* @param bool|null $p
* @param bool $q
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function nullOrImplies($p, $q, $message = '');

/**
* @psalm-pure
*
* @param iterable<bool> $p
* @param bool $q
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function allImplies($p, $q, $message = '');

/**
* @param mixed $value
* @param string $message
Expand Down
4 changes: 4 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ public function getTests()
array('range', array(2, 1, 2), true),
array('range', array(0, 1, 2), false),
array('range', array(3, 1, 2), false),
array('implies', array(true, true), true),
array('implies', array(true, false), false),
array('implies', array(false, true), true),
array('implies', array(false, false), true),
array('oneOf', array(1, array(1, 2, 3)), true),
array('oneOf', array(1, array('1', '2', '3')), false),
array('inArray', array(1, array(1, 2, 3)), true),
Expand Down
50 changes: 50 additions & 0 deletions tests/static-analysis/assert-implies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Webmozart\Assert\Tests\StaticAnalysis;

use Webmozart\Assert\Assert;

/**
* @psalm-pure
*
* @param bool $p
* @param bool $q
*
* @return bool
*/
function implies($p, $q)
{
Assert::implies($p, $q);

return $p;
}

/**
* @psalm-pure
*
* @param null|bool $p
* @param bool $q
*
* @return null|bool
*/
function nullOrImplies($p, $q)
{
Assert::nullOrImplies($p, $q);

return $p;
}

/**
* @psalm-pure
*
* @param iterable<bool> $p
* @param bool $q
*
* @return iterable<bool>
*/
function allImplies(iterable $value, $p, $q): iterable
{
Assert::allImplies($value, $p, $q);

return $value;
}

0 comments on commit 7a4dfaa

Please sign in to comment.