Assert   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 6
eloc 16
c 1
b 1
f 0
dl 0
loc 33
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A requiredParameters() 0 11 2
A instanceOf() 0 9 2
A getType() 0 3 2
1
<?php
2
declare(strict_types=1);
3
4
namespace OniBus\Utility;
5
6
use InvalidArgumentException;
7
use OniBus\Exception\RequiredParameterMissingException;
8
9
class Assert
10
{
11
    public static function instanceOf(string $expected, $subject, $where)
12
    {
13
        if (!is_a($subject, $expected)) {
14
            throw new InvalidArgumentException(
15
                sprintf(
16
                    "[%s] Expected an instance of (%s). Got (%s).",
17
                    self::getType($where),
18
                    self::getType($subject),
19
                    self::getType($expected)
20
                )
21
            );
22
        }
23
    }
24
25
    public static function requiredParameters(array $required, array $parameters, $where)
26
    {
27
        $missing = array_filter(
28
            $required,
29
            function ($item) use ($parameters) {
30
                return !array_key_exists($item, $parameters);
31
            }
32
        );
33
34
        if (!empty($missing)) {
35
            throw new RequiredParameterMissingException($where, $missing);
36
        }
37
    }
38
39
    protected static function getType($subject): string
40
    {
41
        return is_object($subject) ? get_class($subject) : gettype($subject);
42
    }
43
}
44