ReflectionPropertySet::toArray()   A
last analyzed

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 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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