HaveBitwiseFlag   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setFlag() 0 9 2
A isFlagSet() 0 3 1
A selectFlag() 0 19 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace VGirol\JsonApiStructure;
6
7
/**
8
 * This trait add the ability to deal with flags using bitwise flag
9
 */
10
trait HaveBitwiseFlag
11
{
12
    /**
13
     * Undocumented variable
14
     *
15
     * @var int
16
     */
17
    private $flags;
18
19
    /**
20
     * Undocumented function
21
     *
22
     * @param integer $flag
23
     *
24
     * @return boolean
25
     */
26 138
    protected function isFlagSet(int $flag): bool
27
    {
28 138
        return (($this->flags & $flag) == $flag);
29
    }
30
31
    /**
32
     * Undocumented function
33
     *
34
     * @param integer $flag
35
     * @param boolean $value
36
     *
37
     * @return static
38
     */
39 144
    protected function setFlag(int $flag, bool $value)
40
    {
41 144
        if ($value) {
42 144
            $this->flags |= $flag;
43
        } else {
44 144
            $this->flags &= ~$flag;
45
        }
46
47 144
        return $this;
48
    }
49
50
    /**
51
     * Undocumented function
52
     *
53
     * @param integer $flag
54
     * @param array $flags
55
     *
56
     * @return static
57
     */
58 141
    protected function selectFlag(int $flag, array $flags)
59
    {
60 141
        $all = \array_reduce(
61 141
            $flags,
62
            /**
63
             * @param int $result
64
             * @param int $item
65
             *
66
             * @return int
67
             */
68 141
            function ($result, $item) {
69 141
                return $result | $item;
70 141
            },
71 141
            0
72
        );
73 141
        $this->setFlag($flag, true);
74 141
        $this->setFlag($all & ~$flag, false);
75
76 141
        return $this;
77
    }
78
}
79