Passed
Push — master ( 1ec89a...1818e9 )
by Vincent
06:54 queued 04:35
created

HaveBitwiseFlag   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 15
dl 0
loc 33
ccs 15
cts 15
cp 1
rs 10
c 1
b 0
f 0
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setFlag() 0 9 2
A isFlagSet() 0 3 1
A selectFlag() 0 13 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiStructure;
6
7
trait HaveBitwiseFlag
8
{
9
    private $flags;
10
11 132
    protected function isFlagSet(int $flag): bool
12
    {
13 132
        return (($this->flags & $flag) == $flag);
14
    }
15
16 138
    protected function setFlag(int $flag, bool $value)
17
    {
18 138
        if ($value) {
19 138
            $this->flags |= $flag;
20
        } else {
21 138
            $this->flags &= ~$flag;
22
        }
23
24 138
        return $this;
25
    }
26
27 138
    protected function selectFlag(int $flag, array $flags)
28
    {
29 138
        $all = \array_reduce(
30 138
            $flags,
31
            function ($result, $item) {
32 138
                return $result | $item;
33 138
            },
34
            0
35
        );
36 138
        $this->setFlag($flag, true);
37 138
        $this->setFlag($all & ~$flag, false);
38
39 138
        return $this;
40
    }
41
}
42