Completed
Push — master ( 47cbf3...cc7455 )
by Randy
9s
created

functions.php ➔ format()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 7
nc 4
nop 2
dl 0
loc 14
ccs 0
cts 0
cp 0
crap 30
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Dgame\Ensurance;
4
5
use AssertionError;
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 BooleanEnsurance
22 3
 */
23
function enforce(bool $condition, string $message = null): BooleanEnsurance
24
{
25
    $error = 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...
26
27
    return ensure($condition)->isTrue()->setThrowable($error);
28
}
29
30
/**
31
 * @param string $message
32
 * @param mixed  ...$args
33
 *
34
 * @return string
35
 */
36
function format(string $message, ...$args): string
37
{
38
    if (empty($args)) {
39
        return $message;
40
    }
41
42
    foreach ($args as &$arg) {
43
        if (is_array($arg) || is_object($arg)) {
44
            $arg = print_r($arg, true);
45
        }
46
    }
47
48
    return sprintf($message, ...$args);
49
}
50