Flags   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 0
loc 50
ccs 17
cts 17
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getIterator() 0 4 1
A add() 0 8 2
A remove() 0 6 2
A hasFlag() 0 4 1
A count() 0 4 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