Skip to content

Add implies as logical assertion #225

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 mixed $p
* @param mixed $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 mixed $p
* @param mixed $q
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function nullOrImplies($p, $q, $message = '');

/**
* @psalm-pure
*
* @param mixed $p
* @param mixed $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
44 changes: 44 additions & 0 deletions tests/static-analysis/assert-implies.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Webmozart\Assert\Tests\StaticAnalysis;

use Webmozart\Assert\Assert;

/**
* @param mixed $p
* @param mixed $q
*
* @return mixed
*/
function implies($p, $q)
{
Assert::implies($p, $q);

return $p;
}

/**
* @param mixed $p
* @param mixed $q
*
* @return mixed
*/
function nullOrImplies($p, $q)
{
Assert::nullOrImplies($p, $q);

return $p;
}

/**
* @param mixed $p
* @param mixed $q
*
* @return mixed
*/
function allImplies($p, $q)
{
Assert::allImplies($p, $q);

return $p;
}