Completed
Push — master ( 067505...e5d322 )
by Nate
02:19
created

Bag   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 12.37 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 1
dl 12
loc 97
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A toArray() 0 4 1
A contains() 0 4 1
A add() 0 6 1
A clear() 0 4 1
A remove() 12 12 2
A getIterator() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
namespace Tebru\Collection;
8
9
use ArrayIterator;
10
11
/**
12
 * Class Bag
13
 *
14
 * A generic [@see CollectionInterface] backed by an array.
15
 *
16
 * @author Nate Brunette <[email protected]>
17
 */
18
class Bag extends AbstractCollection
19
{
20
    /**
21
     * The collection elements
22
     *
23
     * @var array
24
     */
25
    protected $elements = [];
26
27
    /**
28
     * Constructor
29
     *
30
     * @param array $elements
31
     */
32 4
    public function __construct(array $elements = [])
33
    {
34 4
        $this->elements = array_values($elements);
35 4
    }
36
37
    /**
38
     * Returns true if the collection contains element
39
     *
40
     * @param mixed $element
41
     * @return bool
42
     */
43 6
    public function contains($element): bool
44
    {
45 6
        return in_array($element, $this->elements, true);
46
    }
47
48
    /**
49
     * Ensure the element exists in the collection
50
     *
51
     * Returns true if the collection can contain duplicates,
52
     * and false if it cannot.
53
     *
54
     * @param mixed $element
55
     * @return bool
56
     */
57 28
    public function add($element): bool
58
    {
59 28
        $this->elements[] = $element;
60
61 28
        return true;
62
    }
63
64
    /**
65
     * Removes all elements from a collection
66
     *
67
     * @return void
68
     */
69 1
    public function clear(): void
70
    {
71 1
        $this->elements = [];
72 1
    }
73
74
    /**
75
     * Removes object if it exists
76
     *
77
     * Returns true if the element was removed
78
     *
79
     * @param mixed $element
80
     * @return bool
81
     */
82 8 View Code Duplication
    public function remove($element): bool
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
83
    {
84 8
        $index = array_search($element, $this->elements, true);
85
86 8
        if (false === $index) {
87 2
            return false;
88
        }
89
90 7
        array_splice($this->elements, $index, 1);
91
92 7
        return true;
93
    }
94
95
    /**
96
     * Returns an array of all elements in the collection
97
     *
98
     * @return array
99
     */
100 29
    public function toArray(): array
101
    {
102 29
        return $this->elements;
103
    }
104
105
    /**
106
     * Retrieve an external iterator
107
     *
108
     * @return ArrayIterator
109
     */
110 8
    public function getIterator(): ArrayIterator
111
    {
112 8
        return new ArrayIterator($this->elements);
113
    }
114
}
115