Completed
Pull Request — master (#2)
by Woody
02:04
created

Identifier   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 5
c 1
b 0
f 1
lcom 0
cbo 0
dl 0
loc 41
ccs 16
cts 16
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 28 4
A value() 0 4 1
1
<?php
2
3
namespace Equip\ValueObject;
4
5
use InvalidArgumentException;
6
7
class Identifier
8
{
9
    /**
10
     * @var int|null
11
     */
12
    private $id;
13
14 12
    public function __construct($id, $is_required = true)
15
    {
16
        static $empty_values = [
17
            null,
18
            '0',
19
            '',
20
            0,
21 12
        ];
22
23 12
        if (in_array($id, $empty_values, true) && !$is_required) {
24 3
            $id = null;
25 3
        } else {
26
            $options = [
27 9
                'flags' => \FILTER_REQUIRE_SCALAR,
28
                'options' => [
29 9
                    'min_range' => 1,
30 9
                ],
31 9
            ];
32
33 9
            if (filter_var($id, \FILTER_VALIDATE_INT, $options) === false) {
34 6
                throw new InvalidArgumentException('Value must be a valid identifier');
35
            }
36
37 3
            $id = (int) $id;
38
        }
39
40 6
        $this->id = $id;
41 6
    }
42
43 6
    public function value()
44
    {
45 6
        return $this->id;
46
    }
47
}
48