Completed
Push — master ( cc51b8...3f6140 )
by Nate
02:16
created

AbstractCollection::exists()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 5
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
     * By default this method will use strict comparison checking, passing false
24
     * in will use a double equals (==) instead.
25
     *
26
     * @param CollectionInterface $collection
27
     * @param bool $strict
28
     * @return bool|mixed
29
     */
30 24
    public function addAll(CollectionInterface $collection, bool $strict = true): bool
31
    {
32 24
        $size = $this->count();
33 24
        foreach ($collection as $element) {
34 24
            $this->add($element);
35
        }
36
37 24
        return $size !== $this->count();
38
    }
39
40
    /**
41
     * Returns true if the collection contains all elements from another collection
42
     *
43
     * By default this method will use strict comparison checking, passing false
44
     * in will use a double equals (==) instead.
45
     *
46
     * @param CollectionInterface $collection
47
     * @param bool $strict
48
     * @return bool
49
     */
50 6
    public function containsAll(CollectionInterface $collection, bool $strict = true): bool
51
    {
52 6
        $containsAll = true;
53 6
        foreach ($collection as $element) {
54 6
            if (!$this->contains($element, $strict)) {
55 3
                $containsAll = false;
56 6
                break;
57
            }
58
        }
59
60 6
        return $containsAll;
61
    }
62
63
    /**
64
     * Returns true if the collection is empty
65
     *
66
     * @return bool
67
     */
68 6
    public function isEmpty(): bool
69
    {
70 6
        return 0 === $this->count();
71
    }
72
73
    /**
74
     * Remove all items in a collection from this collection
75
     *
76
     * By default this method will use strict comparison checking, passing false
77
     * in will use a double equals (==) instead.
78
     *
79
     * Returns true if the collection was modified
80
     *
81
     * @param CollectionInterface $collection
82
     * @param bool $strict
83
     * @return bool
84
     */
85 12 View Code Duplication
    public function removeAll(CollectionInterface $collection, bool $strict = true): 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...
86
    {
87 12
        $size = $this->count();
88 12
        foreach ($collection as $element) {
89 12
            $this->remove($element, $strict);
90
        }
91
92 12
        return $size !== $this->count();
93
    }
94
95
    /**
96
     * Remove all items from this collection that don't exist in specified collection
97
     *
98
     * By default this method will use strict comparison checking, passing false
99
     * in will use a double equals (==) instead.
100
     *
101
     * Returns true if the collection was modified
102
     *
103
     * @param CollectionInterface $collection
104
     * @param bool $strict
105
     * @return bool
106
     */
107 12 View Code Duplication
    public function retainAll(CollectionInterface $collection, bool $strict = true): 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...
108
    {
109 12
        $size = $this->count();
110 12
        foreach ($this as $element) {
111 12
            if (!$collection->contains($element, $strict)) {
112 12
                $this->remove($element, $strict);
113
            }
114
        }
115
116 12
        return $size !== $this->count();
117
    }
118
119
    /**
120
     * Use a closure to determine existence in the collection
121
     *
122
     * The closure will get passed each element.  Returning true from the
123
     * closure will return true from this method.
124
     *
125
     * @param callable $exists
126
     * @return bool
127
     */
128 3
    public function exists(callable $exists): bool
129
    {
130 3
        foreach ($this as $element) {
131 3
            if (true === $exists($element)) {
132 3
                return true;
133
            }
134
        }
135
136 3
        return false;
137
    }
138
}
139