Flags::hasFlag()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
namespace Hyphper\Frame;
4
5
class Flags implements \IteratorAggregate, \Countable
6
{
7
    protected $valid_flags = [];
8
    protected $flags = [];
9
10 77
    public function __construct(int ...$valid_flags)
11
    {
12 77
        $this->valid_flags = array_combine($valid_flags, $valid_flags);
13 77
    }
14
15 26
    public function getIterator(): array
16
    {
17 26
        return $this->flags;
18
    }
19
20 37
    public function add($flag)
21
    {
22 37
        if (isset($this->valid_flags[$flag])) {
23 35
            return $this->flags[$flag] = $flag;
24
        }
25
26 3
        throw new \Hyphper\Frame\Exception\InvalidFlagException($flag, $this->valid_flags);
0 ignored issues
show
Documentation introduced by
$this->valid_flags is of type array, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
27
    }
28
29 1
    public function remove($flag)
30
    {
31 1
        if (isset($this->flags[$flag])) {
32 1
            unset($this->flags[$flag]);
33
        }
34 1
    }
35
36 26
    public function hasFlag($flag)
37
    {
38 26
        return isset($this->flags[$flag]);
39
    }
40
41
    /**
42
     * Count elements of an object
43
     * @link http://php.net/manual/en/countable.count.php
44
     * @return int The custom count as an integer.
45
     * </p>
46
     * <p>
47
     * The return value is cast to an integer.
48
     * @since 5.1.0
49
     */
50 2
    public function count()
51
    {
52 2
        return count($this->flags);
53
    }
54
}
55