Completed
Push — master ( 5a4364...64812a )
by Nate
03:15
created

ReflectionPropertySet::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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