Passed
Push — master ( 098479...e4440e )
by Tim
01:55
created

NotInArrayTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 7
c 1
b 0
f 0
dl 0
loc 27
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A notInArray() 0 11 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace SimpleSAML\Assert;
6
7
use InvalidArgumentException;
8
9
use function array_map;
10
use function implode;
11
use function in_array;
12
use function sprintf;
13
14
/**
15
 * @package simplesamlphp/assert
16
 */
17
trait NotInArrayTrait
18
{
19
    /***********************************************************************************
20
     *  NOTE:  Custom assertions may be added below this line.                         *
21
     *         They SHOULD be marked as `protected` to ensure the call is forced       *
22
     *          through __callStatic().                                                *
23
     *         Assertions marked `public` are called directly and will                 *
24
     *          not handle any custom exception passed to it.                          *
25
     ***********************************************************************************/
26
27
28
    /**
29
     * @param mixed $value
30
     * @param array<mixed> $values
31
     * @param string $message
32
     */
33
    protected static function notInArray($value, array $values, string $message = ''): void
34
    {
35
        if (in_array($value, $values, true)) {
36
            $callable = function (mixed $val) {
37
                return self::valueToString($val);
38
            };
39
40
            throw new InvalidArgumentException(sprintf(
41
                $message ?: 'Expected none of: %2$s. Got: %s',
42
                self::valueToString($value),
43
                implode(', ', array_map($callable, $values)),
44
            ));
45
        }
46
    }
47
}
48