Passed
Push — master ( 315e77...e369d0 )
by Jonathan
16:56
created

AssertArrayTrait   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 21
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
eloc 5
dl 0
loc 21
ccs 5
cts 5
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A assertArray() 0 6 3
1
<?php
2
declare(strict_types=1);
3
4
/**
5
 * ReportingCloud PHP SDK
6
 *
7
 * PHP SDK for ReportingCloud Web API. Authored and supported by Text Control GmbH.
8
 *
9
 * @link      https://www.reporting.cloud to learn more about ReportingCloud
10
 * @link      https://github.com/TextControl/txtextcontrol-reportingcloud-php for the canonical source repository
11
 * @license   https://raw.githubusercontent.com/TextControl/txtextcontrol-reportingcloud-php/master/LICENSE.md
12
 * @copyright © 2019 Text Control GmbH
13
 */
14
15
namespace TxTextControl\ReportingCloud\Assert;
16
17
use TxTextControl\ReportingCloud\Exception\InvalidArgumentException;
18
19
/**
20
 * Trait AssertArrayTrait
21
 *
22
 * @package TxTextControl\ReportingCloud
23
 * @author  Jonathan Maron (@JonathanMaron)
24
 */
25
trait AssertArrayTrait
26
{
27
    /**
28
     * @param mixed $value
29
     *
30
     * @return string
31
     */
32
    abstract protected static function valueToString($value): string;
33
34
    /**
35
     * Check value is an array
36
     *
37
     * @param mixed  $value
38
     * @param string $message
39
     */
40 15
    public static function assertArray($value, string $message = ''): void
41
    {
42 15
        if (!is_array($value)) {
43 9
            $format  = $message ?: 'Expected an array. Got: %1$s';
44 9
            $message = sprintf($format, self::valueToString($value));
45 9
            throw new InvalidArgumentException($message);
46
        }
47 6
    }
48
}
49