Passed
Push — master ( 9d7ef7...af08db )
by Andrea Marco
01:42 queued 11s
created

HasFlags::hasFlags()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 1
c 1
b 0
f 1
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Cerbero\Dto\Traits;
4
5
use Cerbero\Dto\Dto;
6
use Cerbero\Dto\DtoFlagsHandler;
7
8
use const Cerbero\Dto\ARRAY_DEFAULT_TO_EMPTY_ARRAY;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\ARRAY_DEFAULT_TO_EMPTY_ARRAY was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
9
use const Cerbero\Dto\BOOL_DEFAULT_TO_FALSE;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\BOOL_DEFAULT_TO_FALSE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
10
use const Cerbero\Dto\CAST_PRIMITIVES;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\CAST_PRIMITIVES was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
11
use const Cerbero\Dto\MUTABLE;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\MUTABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
12
use const Cerbero\Dto\NONE;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\NONE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
13
use const Cerbero\Dto\NOT_NULLABLE;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\NOT_NULLABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
14
use const Cerbero\Dto\NULLABLE;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\NULLABLE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
15
use const Cerbero\Dto\NULLABLE_DEFAULT_TO_NULL;
0 ignored issues
show
Bug introduced by
The constant Cerbero\Dto\NULLABLE_DEFAULT_TO_NULL was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
16
17
/**
18
 * Trait to interact with flags.
19
 *
20
 */
21
trait HasFlags
22
{
23
    /**
24
     * The default flags.
25
     *
26
     * @var int
27
     */
28
    protected static $defaultFlags = NONE;
29
30
    /**
31
     * The actual flags.
32
     *
33
     * @var int
34
     */
35
    protected $flags;
36
37
    /**
38
     * Retrieve the default flags
39
     *
40
     * @return int
41
     */
42 243
    public static function getDefaultFlags(): int
43
    {
44 243
        return static::$defaultFlags;
45
    }
46
47
    /**
48
     * Retrieve the DTO flags
49
     *
50
     * @return int
51
     */
52 240
    public function getFlags(): int
53
    {
54 240
        return $this->flags;
55
    }
56
57
    /**
58
     * Determine whether the DTO flags include the given flags
59
     *
60
     * @param int $flags
61
     * @return bool
62
     */
63 3
    public function hasFlags(int $flags): bool
64
    {
65 3
        return ($this->getFlags() & $flags) === $flags;
66
    }
67
68
    /**
69
     * Set the DTO flags
70
     *
71
     * @param int $flags
72
     * @return Dto
73
     */
74 21
    public function setFlags(int $flags): Dto
75
    {
76 21
        $currentFlags = $this->getFlags();
77
78 21
        if (!($currentFlags & MUTABLE)) {
79 12
            return static::make($this->toArray(), $flags);
0 ignored issues
show
Bug introduced by
It seems like toArray() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
            return static::make($this->/** @scrutinizer ignore-call */ toArray(), $flags);
Loading history...
80
        }
81
82 9
        $this->flags = $flags;
83
84 9
        if (($currentFlags | $flags) & $this->getFlagsAffectingValues()) {
85 6
            $this->mapData($this->toArray());
0 ignored issues
show
Bug introduced by
It seems like mapData() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
            $this->/** @scrutinizer ignore-call */ 
86
                   mapData($this->toArray());
Loading history...
86
        }
87
88 9
        return $this;
89
    }
90
91
    /**
92
     * Retrieve the flags that affect the values of a DTO
93
     *
94
     * @return int
95
     */
96 9
    protected function getFlagsAffectingValues(): int
97
    {
98 9
        return $this->getFlagsForDefaults() | NULLABLE | NOT_NULLABLE | CAST_PRIMITIVES;
99
    }
100
101
    /**
102
     * Retrieve the flags that determine default values
103
     *
104
     * @return int
105
     */
106 21
    protected function getFlagsForDefaults(): int
107
    {
108 21
        return NULLABLE_DEFAULT_TO_NULL | BOOL_DEFAULT_TO_FALSE | ARRAY_DEFAULT_TO_EMPTY_ARRAY;
109
    }
110
111
    /**
112
     * Add the given flags to the DTO flags
113
     *
114
     * @param int $flags
115
     * @return Dto
116
     */
117 6
    public function addFlags(int $flags): Dto
118
    {
119 6
        $mergedFlags = $this->mergeFlags($this->getFlags(), $flags);
120
121 6
        return $this->setFlags($mergedFlags);
122
    }
123
124
    /**
125
     * Retrieve the merged flags
126
     *
127
     * @param int $initialFlags
128
     * @param int $flagsToMerge
129
     * @return int
130
     * @throws \Cerbero\Dto\Exceptions\IncompatibleDtoFlagsException
131
     */
132 240
    protected function mergeFlags(int $initialFlags, int $flagsToMerge): int
133
    {
134 240
        return (new DtoFlagsHandler())->merge($initialFlags, $flagsToMerge);
135
    }
136
137
    /**
138
     * Remove the given flags from the DTO flags
139
     *
140
     * @param int $flags
141
     * @return Dto
142
     */
143 6
    public function removeFlags(int $flags): Dto
144
    {
145 6
        $currentFlags = $this->getFlags();
146 6
        $remainingFlags = $currentFlags ^ ($currentFlags & $flags);
147
148 6
        return $this->setFlags($remainingFlags);
149
    }
150
151
    /**
152
     * Retrieve the DTO flags excluding the flags for default values
153
     *
154
     * @return int
155
     */
156 12
    protected function getFlagsWithoutDefaults(): int
157
    {
158 12
        $flags = $this->getFlags();
159
160 12
        return $flags ^ ($flags & $this->getFlagsForDefaults());
161
    }
162
}
163