StringCollection   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 12
dl 0
loc 53
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 3
A add() 0 4 2
A remove() 0 6 2
A has() 0 3 1
1
<?php
2
/**
3
 * This file is part of the theroadbunch/bouncer package.
4
 *
5
 * (c) Dan McAdams <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace RoadBunch\String;
12
13
14
/**
15
 * Class StringCollection
16
 *
17
 * @author  Dan McAdams
18
 * @package RoadBunch\String
19
 */
20
class StringCollection implements StringCollectionInterface
21
{
22
    protected $elements = [];
23
24
    /**
25
     * StringCollection constructor.
26
     *
27
     * @param string[] $elements
28
     */
29 17
    public function __construct(array $elements = [])
30
    {
31 17
        foreach ($elements as $element) {
32 12
            if (!is_string($element)) {
33 1
                throw new \InvalidArgumentException('All elements must be strings');
34
            }
35 12
            $this->add($element);
36
        }
37 17
    }
38
39
    /**
40
     * Add an element, duplicates will be ignored
41
     *
42
     * @param string $element
43
     */
44 13
    public function add(string $element): void
45
    {
46 13
        if (!in_array($element, $this->elements)) {
47 13
            $this->elements[] = $element;
48
        }
49 13
    }
50
51
    /**
52
     * Check to see if an element exists in the lists
53
     *
54
     * @param string $element
55
     * @return bool
56
     */
57 11
    public function has(string $element): bool
58
    {
59 11
        return in_array($element, $this->elements);
60
    }
61
62
    /**
63
     * Remove an element from the list
64
     *
65
     * @param string $element
66
     */
67 4
    public function remove(string $element): void
68
    {
69 4
        $index = array_search($element, $this->elements);
70
71 4
        if ($index !== false) {
72 3
            unset($this->elements[$index]);
73
        }
74 4
    }
75
}
76