Completed
Push — master ( 6db18d...e8104d )
by Nate
02:21
created

AbstractCollection::find()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 4
nc 3
nop 1
crap 3
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
/**
10
 * Class AbstractCollection
11
 *
12
 * Provides a skeletal implementation of the [@see CollectionInterface]
13
 *
14
 * @author Nate Brunette <[email protected]>
15
 */
16
abstract class AbstractCollection implements CollectionInterface
17
{
18
    /**
19
     * Ensure all elements of a collection exists in this collection
20
     *
21
     * Return true if the collection has changed, and false if it hasn't
22
     *
23
     * @param CollectionInterface $collection
24
     * @return bool
25
     */
26 32
    public function addAll(CollectionInterface $collection): bool
27
    {
28 32
        $size = $this->count();
29 32
        foreach ($collection as $element) {
30 32
            $this->add($element);
31
        }
32
33 32
        return $size !== $this->count();
34
    }
35
36
    /**
37
     * Returns true if the collection contains all elements from another collection
38
     *
39
     * @param CollectionInterface $collection
40
     * @return bool
41
     */
42 6
    public function containsAll(CollectionInterface $collection): bool
43
    {
44 6
        $containsAll = true;
45 6
        foreach ($collection as $element) {
46 6
            if (!$this->contains($element)) {
47 3
                $containsAll = false;
48 6
                break;
49
            }
50
        }
51
52 6
        return $containsAll;
53
    }
54
55
    /**
56
     * Returns true if the collection is empty
57
     *
58
     * @return bool
59
     */
60 6
    public function isEmpty(): bool
61
    {
62 6
        return 0 === $this->count();
63
    }
64
65
    /**
66
     * Remove all items in a collection from this collection
67
     *
68
     * Returns true if the collection was modified
69
     *
70
     * @param CollectionInterface $collection
71
     * @return bool
72
     */
73 9
    public function removeAll(CollectionInterface $collection): bool
74
    {
75 9
        $size = $this->count();
76 9
        foreach ($collection as $element) {
77 9
            $this->remove($element);
78
        }
79
80 9
        return $size !== $this->count();
81
    }
82
83
    /**
84
     * Remove all items from this collection that don't exist in specified collection
85
     *
86
     * Returns true if the collection was modified
87
     *
88
     * @param CollectionInterface $collection
89
     * @return bool
90
     */
91 9 View Code Duplication
    public function retainAll(CollectionInterface $collection): bool
1 ignored issue
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...
92
    {
93 9
        $size = $this->count();
94 9
        foreach ($this as $element) {
95 9
            if (!$collection->contains($element)) {
96 9
                $this->remove($element);
97
            }
98
        }
99
100 9
        return $size !== $this->count();
101
    }
102
103
    /**
104
     * Find the first element in collection
105
     *
106
     * The closure will get passed each element.  Returning true will end the
107
     * loop and return that element
108
     *
109
     * @param callable $find
110
     * @return mixed
111
     */
112 6
    public function find(callable $find)
113
    {
114 6
        foreach ($this as $element) {
115 6
            if (true === $find($element)) {
116 6
                return $element;
117
            }
118
        }
119 3
    }
120
121
    /**
122
     * Use a closure to determine existence in the collection
123
     *
124
     * The closure will get passed each element.  Returning true from the
125
     * closure will return true from this method.
126
     *
127
     * @param callable $exists
128
     * @return bool
129
     */
130 3
    public function exists(callable $exists): bool
131
    {
132 3
        foreach ($this as $element) {
133 3
            if (true === $exists($element)) {
134 3
                return true;
135
            }
136
        }
137
138 3
        return false;
139
    }
140
}
141