ReflectionPropertySet   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A add() 0 10 2
A toArray() 0 3 1
A getIterator() 0 3 1
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
declare(strict_types=1);
8
9
namespace Tebru\Gson\Internal\Data;
10
11
use ArrayIterator;
12
use IteratorAggregate;
13
use ReflectionProperty;
14
15
/**
16
 * Class ReflectionPropertySet
17
 *
18
 * A [@see HashSet] that is keyed by [@see \ReflectionProperty] name
19
 *
20
 * @author Nate Brunette <[email protected]>
21
 */
22
final class ReflectionPropertySet implements IteratorAggregate
23
{
24
    /**
25
     * @var array
26
     */
27
    private $elements = [];
28
29
    /**
30
     * Ensure the element exists in the collection
31
     *
32
     * Returns true if the collection can contain duplicates,
33
     * and false if it cannot.
34
     *
35
     * @param ReflectionProperty $element
36
     * @return bool
37
     */
38 6
    public function add(ReflectionProperty $element): bool
39
    {
40 6
        $key = $element->getName();
41 6
        if (isset($this->elements[$key])) {
42 1
            return false;
43
        }
44
45 6
        $this->elements[$key] = $element;
46
47 6
        return true;
48
    }
49
50
    /**
51
     * Returns an array of all elements in the collection
52
     *
53
     * @return array
54
     */
55 7
    public function toArray(): array
56
    {
57 7
        return array_values($this->elements);
58
    }
59
60
    /**
61
     * Retrieve an external iterator
62
     *
63
     * @return ArrayIterator
64
     */
65 4
    public function getIterator(): ArrayIterator
66
    {
67 4
        return new ArrayIterator($this->toArray());
68
    }
69
}
70