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

HaveBitwiseFlag::selectFlag()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
nc 1
nop 2
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 1
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