Passed
Pull Request — master (#85)
by Sergei
03:10
created

validateMethodArguments()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Factory\Definition;
6
7
use Yiisoft\Factory\Exception\InvalidConfigException;
8
use function get_class;
9
use function gettype;
10
use function is_array;
11
use function is_object;
12
use function is_string;
13
14
final class ArrayDefinitionValidator
15
{
16
    /**
17
     * @param mixed $class
18
     *
19
     * @throws InvalidConfigException
20
     */
21 32
    public static function validateClassName($class): void
22
    {
23 32
        if (!is_string($class)) {
24 1
            throw new InvalidConfigException(sprintf('Invalid definition: invalid class name "%s".', (string)$class));
25
        }
26
27 31
        if ($class === '') {
28 1
            throw new InvalidConfigException('Invalid definition: empty class name.');
29
        }
30 30
    }
31
32
    /**
33
     * @param mixed $arguments
34
     *
35
     * @psalm-assert array $arguments
36
     *
37
     * @throws InvalidConfigException
38
     */
39 9
    public static function validateConstructorArguments($arguments): void
40
    {
41 9
        if (!is_array($arguments)) {
42 1
            throw new InvalidConfigException(
43 1
                sprintf(
44 1
                    'Invalid definition: incorrect constructor arguments. Expected array, got %s.',
45 1
                    self::getType($arguments)
46
                )
47
            );
48
        }
49 8
    }
50
51
    /**
52
     * @param mixed $arguments
53
     *
54
     * @psalm-assert array $arguments
55
     *
56
     * @throws InvalidConfigException
57
     */
58 13
    public static function validateMethodArguments($arguments): void
59
    {
60 13
        if (!is_array($arguments)) {
61 1
            throw new InvalidConfigException(
62 1
                sprintf('Invalid definition: incorrect method arguments. Expected array, got %s.', self::getType($arguments))
63
            );
64
        }
65 12
    }
66
67
    /**
68
     * @param mixed $value
69
     */
70 2
    private static function getType($value): string
71
    {
72 2
        return is_object($value) ? get_class($value) : gettype($value);
73
    }
74
}
75