Completed
Push — master ( 166cad...47cbf3 )
by Randy
01:33
created

functions.php ➔ assertion()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
ccs 0
cts 0
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Dgame\Ensurance;
4
5
use Dgame\Ensurance\Enforcement\Enforcement;
6
7
/**
8
 * @param $value
9
 *
10
 * @return Ensurance
11
 */
12 57
function ensure($value): Ensurance
13
{
14
    return new Ensurance($value);
15
}
16
17
/**
18
 * @param bool        $condition
19
 * @param string|null $message
20
 *
21
 * @return Enforcement
22 3
 */
23
function enforce(bool $condition, string $message = null): Enforcement
24
{
25
    return new Enforcement($condition, $message);
26
}
27
28
/**
29
 * @param bool        $condition
30
 * @param string|null $message
31
 *
32
 * @throws \AssertionError
33
 */
34
function assertion(bool $condition, string $message = null)
35
{
36
    if (!$condition) {
37
        throw new \AssertionError($message ?? 'Assertion failed');
0 ignored issues
show
Unused Code introduced by
The call to AssertionError::__construct() has too many arguments starting with $message ?? 'Assertion failed'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
38
    }
39
}
40
41
/**
42
 * @param bool        $condition
43
 * @param string|null $message
44
 *
45
 * @throws \AssertionError
46
 * @deprecated Use assertion instead. `assure` is just an alias for `assertion`, since `assertion` is a more meaningful name.
47
 */
48
function assure(bool $condition, string $message = null)
49
{
50
    assertion($condition, $message);
51
}