Completed
Push — master ( 8cc10c...1afeda )
by Zoltán
07:18 queued 43s
created

AbstractCollection::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php namespace BuildR\Collection\Collection;
2
3
use BuildR\Collection\Interfaces\CollectionInterface;
4
use BuildR\Foundation\Object\ArrayConvertibleInterface;
5
6
/**
7
 * Abstract collection implementation
8
 *
9
 * BuildR PHP Framework
10
 *
11
 * @author Zoltán Borsos <[email protected]>
12
 * @package Collection
13
 * @subpackage Collection
14
 *
15
 * @copyright    Copyright 2015, Zoltán Borsos.
16
 * @license      https://github.com/Zolli/BuildR/blob/master/LICENSE.md
17
 * @link         https://github.com/Zolli/BuildR
18
 */
19
abstract class AbstractCollection implements CollectionInterface {
20
21
    /**
22
     * @type array
23
     */
24
    protected $data = [];
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 11
    public function toArray() {
30 11
        return (array) $this->data;
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 1
    public function clear() {
37 1
        $this->data = [];
38 1
    }
39
40
    /**
41
     * {@inheritdoc}
42
     */
43 23
    public function contains($element) {
44 23
        return (array_search($element, $this->data, TRUE) === FALSE) ? FALSE : TRUE;
45
    }
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 10 View Code Duplication
    public function containsAll($elements) {
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...
51 10
        $elements = $this->collectionToArray($elements);
52
53 10
        $result = TRUE;
54
55 10
        foreach($elements as $item) {
56 10
            if($this->contains($item) === FALSE) {
57 5
                $result = FALSE;
58
59 5
                break;
60
            }
61 10
        }
62
63 10
        return $result;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 6
    public function containsAny($elements) {
70 6
        $elements = $this->collectionToArray($elements);
71
72 6
        foreach($elements as $item) {
73 6
            if($this->contains($item) === TRUE) {
74 5
                return TRUE;
75
            }
76 6
        }
77
78 6
        return FALSE;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84 6
    public function equals(CollectionInterface $collection) {
85
        //First check the size, if this not equals the tow collection
86
        //not be identical
87 6
        if($collection->size() !== $this->size()) {
88 1
            return FALSE;
89
        }
90
91
        //Use strict comparison to check arrays are equals
92
        //(Values and orders)
93 5
        return $collection->toArray() === $this->data;
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 1
    public function isEmpty() {
100 1
        return empty($this->data);
101
    }
102
103
    /**
104
     * {@inheritdoc}
105
     */
106 14
    public function size() {
107 14
        return (is_array($this->data)) ? count($this->data) : 0;
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     *
113
     * @codeCoverageIgnore
114
     */
115
    public function current() {
116
        return $this->data[$this->position];
0 ignored issues
show
Bug introduced by
The property position does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
117
    }
118
119
    /**
120
     * {@inheritdoc}
121
     *
122
     * @codeCoverageIgnore
123
     */
124
    public function next() {
125
        return next($this->data);
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     *
131
     * @codeCoverageIgnore
132
     */
133
    public function key() {
134
        return key($this->data);
135
    }
136
137
    /**
138
     * {@inheritdoc}
139
     *
140
     * @codeCoverageIgnore
141
     */
142
    public function valid() {
143
        return key($this->data) !== NULL;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     *
149
     * @codeCoverageIgnore
150
     */
151
    public function rewind() {
152
        return reset($this->data);
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158 8
    public function count() {
159 8
        return $this->size();
160
    }
161
162
    /**
163
     * @param array|\BuildR\Collection\Interfaces\CollectionInterface $elements
164
     *
165
     * @return array
166
     */
167 21
    protected function collectionToArray($elements) {
168 21
        if($elements instanceof CollectionInterface) {
169 1
            $elements = $elements->toArray();
170 1
        }
171
172 21
        return $elements;
173
    }
174
175
}
176