Test Failed
Pull Request — master (#85)
by Sergei
02:15
created

ArrayDefinitionValidator::validateClassName()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 8
rs 10
cc 3
nc 3
nop 1
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
    public static function validateClassName($class): void
22
    {
23
        if (!is_string($class)) {
24
            throw new InvalidConfigException(sprintf('Invalid definition: invalid class name "%s".', (string)$class));
25
        }
26
27
        if ($class === '') {
28
            throw new InvalidConfigException('Invalid definition: empty class name.');
29
        }
30
    }
31
32
    /**
33
     * @param mixed $arguments
34
     *
35
     * @psalm-assert array $arguments
36
     *
37
     * @throws InvalidConfigException
38
     */
39
    public static function validateConstructorArguments($arguments): void
40
    {
41
        if (!is_array($arguments)) {
42
            throw new InvalidConfigException(
43
                sprintf(
44
                    'Invalid definition: incorrect constructor arguments. Expected array, got %s.',
45
                    self::getType($arguments)
46
                )
47
            );
48
        }
49
    }
50
51
    /**
52
     * @param mixed $arguments
53
     *
54
     * @psalm-assert array $arguments
55
     *
56
     * @throws InvalidConfigException
57
     */
58
    public static function validateMethodArguments($arguments): void
59
    {
60
        if (!is_array($arguments)) {
61
            throw new InvalidConfigException(
62
                sprintf('Invalid definition: incorrect method arguments. Expected array, got %s.', self::getType($arguments))
63
            );
64
        }
65
    }
66
67
    /**
68
     * @param mixed $value
69
     */
70
    private static function getType($value): string
71
    {
72
        return is_object($value) ? get_class($value) : gettype($value);
73
    }
74
}
75