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    private $elements = [];
11
12    public function __construct( array $elements = [] ) {
13        $this->addAll( $elements );
14    }
15
16    /**
17     * @return int Number of elements in the set
18     */
19    public function count(): int {
20        return count( $this->elements );
21    }
22
23    /**
24     * @param string $element Element to add to set
25     * @return self
26     */
27    public function add( $element ) {
28        $this->elements[$element] = true;
29        return $this;
30    }
31
32    /**
33     * @param string[] $elements Elements to add to set
34     * @return self
35     */
36    public function addAll( array $elements ) {
37        foreach ( $elements as $element ) {
38            $this->add( $element );
39        }
40        return $this;
41    }
42
43    /**
44     * @param Set $other Set to union into this one
45     * @return self
46     */
47    public function union( Set $other ) {
48        $this->elements += $other->elements;
49        return $this;
50    }
51
52    /**
53     * @param string $element Value to test
54     * @return bool True when the set contains $element
55     */
56    public function contains( $element ) {
57        return array_key_exists( $element, $this->elements );
58    }
59
60    /**
61     * @return string[] Elements of the set
62     */
63    public function values() {
64        return array_keys( $this->elements );
65    }
66}