Primitive::fromString()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 6
ccs 4
cts 4
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace Innmind\Compose\Definition\Argument\Type;
5
6
use Innmind\Compose\{
7
    Definition\Argument\Type,
8
    Exception\NotAPrimitiveType,
9
    Exception\ValueNotSupported
10
};
11
use Innmind\Immutable\Str;
12
13
final class Primitive implements Type
14
{
15
    private $function;
16
17 56
    public function __construct(string $primitive)
18
    {
19 56
        if (!function_exists('is_'.$primitive)) {
20 10
            throw new NotAPrimitiveType($primitive);
21
        }
22
23 53
        $this->function = 'is_'.$primitive;
24 53
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 33
    public function accepts($value): bool
30
    {
31 33
        return ($this->function)($value);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 11
    public static function fromString(Str $value): Type
38
    {
39
        try {
40 11
            return new self((string) $value);
41 9
        } catch (NotAPrimitiveType $e) {
42 9
            throw new ValueNotSupported((string) $value, 0, $e);
43
        }
44
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 3
    public function __toString(): string
50
    {
51 3
        return substr($this->function, 3);
52
    }
53
}
54