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

HaveBitwiseFlag::setFlag()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
nc 2
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 2
rs 10
c 1
b 0
f 0
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