Primitive   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 39
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A accepts() 0 3 1
A __toString() 0 3 1
A __construct() 0 7 2
A fromString() 0 6 2
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