Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
7 / 7
CRAP
100.00% covered (success)
100.00%
1 / 1
Set
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
7 / 7
8
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 count
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 add
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 addAll
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 union
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 contains
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 values
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace CirrusSearch\Maintenance;
4
5/**
6 * Simple set implementation so it's clear what the array
7 * is being used for when values are only stored as keys.
8 */
9class Set implements \Countable {
10    /** @var true[] */
11    private $elements = [];
12
13    public function __construct( array $elements = [] ) {
14        $this->addAll( $elements );
15    }
16
17    /**
18     * @return int Number of elements in the set
19     */
20    public function count(): int {
21        return count( $this->elements );
22    }
23
24    /**
25     * @param string $element Element to add to set
26     * @return self
27     */
28    public function add( $element ) {
29        $this->elements[$element] = true;
30        return $this;
31    }
32
33    /**
34     * @param string[] $elements Elements to add to set
35     * @return self
36     */
37    public function addAll( array $elements ) {
38        foreach ( $elements as $element ) {
39            $this->add( $element );
40        }
41        return $this;
42    }
43
44    /**
45     * @param Set $other Set to union into this one
46     * @return self
47     */
48    public function union( Set $other ) {
49        $this->elements += $other->elements;
50        return $this;
51    }
52
53    /**
54     * @param string $element Value to test
55     * @return bool True when the set contains $element
56     */
57    public function contains( $element ) {
58        return array_key_exists( $element, $this->elements );
59    }
60
61    /**
62     * @return string[] Elements of the set
63     */
64    public function values() {
65        return array_keys( $this->elements );
66    }
67}