LiteralArgument::__construct()   B
last analyzed

Complexity

Conditions 7
Paths 2

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 7

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 11
ccs 7
cts 7
cp 1
rs 8.8333
cc 7
nc 2
nop 2
crap 7
1
<?php
2
3
declare(strict_types=1);
4
5
namespace League\Container\Argument;
6
7
use InvalidArgumentException;
8
9
class LiteralArgument implements LiteralArgumentInterface
10
{
11
    public const TYPE_ARRAY    = 'array';
12
    public const TYPE_BOOL     = 'boolean';
13
    public const TYPE_BOOLEAN  = 'boolean';
14
    public const TYPE_CALLABLE = 'callable';
15
    public const TYPE_DOUBLE   = 'double';
16
    public const TYPE_FLOAT    = 'double';
17
    public const TYPE_INT      = 'integer';
18
    public const TYPE_INTEGER  = 'integer';
19
    public const TYPE_OBJECT   = 'object';
20
    public const TYPE_STRING   = 'string';
21
22
    /**
23
     * @var mixed
24
     */
25
    protected $value;
26
27 27
    public function __construct($value, ?string $type = null)
28
    {
29
        if (
30 27
            null === $type
31 27
            || ($type === self::TYPE_CALLABLE && is_callable($value))
32 27
            || ($type === self::TYPE_OBJECT && is_object($value))
33 27
            || gettype($value) === $type
34
        ) {
35 24
            $this->value = $value;
36
        } else {
37 3
            throw new InvalidArgumentException('Incorrect type for value.');
38
        }
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 18
    public function getValue()
45
    {
46 18
        return $this->value;
47
    }
48
}
49