Skip to content

Commit 9b6e4db

Browse files
committed
PSR-2 coding styles applied
1 parent 2eb1c2b commit 9b6e4db

File tree

11 files changed

+167
-87
lines changed

11 files changed

+167
-87
lines changed

src/Collections/ArrayList.php

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44

55
use PHPCollections\Exceptions\InvalidOperationException;
66

7-
class ArrayList extends BaseCollection
7+
/**
8+
* A list of values of any type
9+
*/
10+
class ArrayList extends BaseCollection
811
{
912
/**
1013
* Add a new element to the collection
@@ -26,33 +29,37 @@ public function add($val)
2629
*/
2730
public function contains($val)
2831
{
29-
foreach ($this->data as $value)
30-
if ($value === $val) return true;
32+
foreach ($this->data as $value) {
33+
if ($value === $val) {
34+
return true;
35+
}
36+
}
3137

3238
return false;
3339
}
3440

3541
/**
3642
* Return all the coincidences found
3743
* for the given callback or null
38-
*
44+
*
3945
* @param callable $callback
4046
* @return PHPCollections\ArrayList|null
4147
*/
4248
public function filter(callable $callback)
4349
{
4450
$matcheds = [];
4551
foreach ($this->data as $value) {
46-
if (call_user_func($callback, $value) === true)
52+
if (call_user_func($callback, $value) === true) {
4753
$matcheds[] = $value;
54+
}
4855
}
4956
return count($matcheds) > 0 ? new $this(array_values($matcheds)) : null;
5057
}
5158

5259
/**
5360
* Search one or more elements in
5461
* the collection
55-
*
62+
*
5663
* @param callable $callback
5764
* @param boolean $shouldStop
5865
* @return PHPCollections\ArrayList|null
@@ -63,7 +70,9 @@ public function find(callable $callback, $shouldStop = false)
6370
foreach ($this->data as $key => $item) {
6471
if ($callback($item, $key) === true) {
6572
$matcheds[] = $item;
66-
if ($shouldStop) break;
73+
if ($shouldStop) {
74+
break;
75+
}
6776
}
6877
}
6978
return count($matcheds) > 0 ? new $this($matcheds) : null;
@@ -77,13 +86,14 @@ public function find(callable $callback, $shouldStop = false)
7786
*/
7887
public function first()
7988
{
80-
if ($this->count() == 0)
89+
if ($this->count() == 0) {
8190
throw new OutOfRangeException("You're trying to get data into an empty collection.");
91+
}
8292
return $this->data[0];
8393
}
8494

8595
/**
86-
* Get the element specified
96+
* Get the element specified
8797
* at the given index
8898
*
8999
* @param int $offset
@@ -102,8 +112,9 @@ public function get($offset)
102112
*/
103113
public function last()
104114
{
105-
if ($this->count() == 0)
115+
if ($this->count() == 0) {
106116
throw new OutOfRangeException("You're trying to get data into an empty collection.");
117+
}
107118
return $this->data[$this->count() - 1];
108119
}
109120

@@ -133,36 +144,38 @@ public function merge(array $data)
133144
}
134145

135146
/**
136-
* Return a random element of
147+
* Return a random element of
137148
* the collection
138149
*
139150
* @throws PHPCollections\Exceptions\InvalidOperationException
140151
* @return mixed
141152
*/
142153
public function rand()
143154
{
144-
if ($this->count() == 0)
155+
if ($this->count() == 0) {
145156
throw new InvalidOperationException('You cannot get a random element from an empty collection.');
157+
}
146158
$randomIndex = array_rand($this->data);
147159
return $this->get($randomIndex);
148160
}
149161

150162
/**
151163
* Return a new collection with the
152164
* reversed values
153-
*
165+
*
154166
* @throws PHPCollections\Exceptions\InvalidOperationException
155167
* @return PHPCollections\ArrayList
156168
*/
157169
public function reverse()
158170
{
159-
if ($this->count() == 0)
171+
if ($this->count() == 0) {
160172
throw new InvalidOperationException('You cannot reverse an empty collection.');
173+
}
161174
return new $this(array_reverse($this->data));
162175
}
163176

164177
/**
165-
* Update the value of the element
178+
* Update the value of the element
166179
* at the given index
167180
*
168181
* @param int $index
@@ -171,9 +184,10 @@ public function reverse()
171184
*/
172185
public function update($index, $value)
173186
{
174-
if (!$this->exists($index))
187+
if (!$this->exists($index)) {
175188
throw new InvalidOperationException('You cannot update a non-existent value');
189+
}
176190
$this->data[$index] = $value;
177191
return $this->data[$index] === $value;
178192
}
179-
}
193+
}

src/Collections/BaseCollection.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,12 @@
88
use JsonSerializable;
99
use ArrayIterator;
1010

11-
abstract class BaseCollection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable {
11+
/**
12+
* The base for iterable, countable
13+
* and arrayable collections
14+
*/
15+
abstract class BaseCollection implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable
16+
{
1217

1318
/**
1419
* The data container
@@ -26,7 +31,7 @@ abstract class BaseCollection implements ArrayAccess, Countable, IteratorAggrega
2631

2732
/**
2833
* BaseCollection constructor
29-
*
34+
*
3035
* @param array $data
3136
*
3237
* @return void
@@ -37,7 +42,7 @@ public function __construct(array $data = [])
3742
}
3843

3944
/**
40-
* Remove all the elements
45+
* Remove all the elements
4146
* of the data array
4247
*
4348
* @return void
@@ -58,7 +63,7 @@ public function count()
5863
}
5964

6065
/**
61-
* Check if the given index
66+
* Check if the given index
6267
* exists in the collection
6368
*
6469
* @param int $offset
@@ -72,7 +77,7 @@ public function exists($offset)
7277
/**
7378
* Return an array iterator for
7479
* the data array
75-
*
80+
*
7681
* @return ArrayIterator
7782
*/
7883
public function getIterator()
@@ -83,7 +88,7 @@ public function getIterator()
8388
/**
8489
* Defines the behavior of the collection
8590
* when json_encode is called
86-
*
91+
*
8792
* @return array
8893
*/
8994
public function jsonSerialize()
@@ -122,10 +127,11 @@ public function offsetGet($offset)
122127
*/
123128
public function offsetSet($offset, $value)
124129
{
125-
if (is_null($offset))
130+
if (is_null($offset)) {
126131
array_push($this->data, $value);
127-
else
132+
} else {
128133
$this->data[$offset] = $value;
134+
}
129135
}
130136

131137
/**

src/Collections/Dictionary.php

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@
55
use InvalidArgumentException;
66
use PHPCollections\Exceptions\InvalidOperationException;
77

8+
/**
9+
* A Pair object collection
10+
* represented by a generic
11+
* type key and value
12+
*/
813
class Dictionary extends BaseCollection
914
{
1015
/**
1116
* The type of the keys
1217
* for this dictionary
13-
*
18+
*
1419
* @var mixed
1520
*/
1621
private $keyType;
@@ -35,8 +40,9 @@ public function __construct($keyType, $valueType, array $data = [])
3540
$this->keyType = $keyType;
3641
$this->valueType = $valueType;
3742

38-
foreach ($data as $key => $value)
43+
foreach ($data as $key => $value) {
3944
$this->data[$key] = new Pair($key, $value);
45+
}
4046
}
4147

4248
/**
@@ -58,7 +64,7 @@ public function add($key, $value)
5864
* attribute, if not throws and Exception
5965
*
6066
* @param mixed $data
61-
* @throws InvalidArgumentException
67+
* @throws InvalidArgumentException
6268
* @return void
6369
*/
6470
private function checkType(array $values)
@@ -85,8 +91,9 @@ public function filter(callable $callback)
8591
{
8692
$matcheds = [];
8793
foreach ($this->data as $key => $value) {
88-
if (call_user_func($callback, $value->getKey(), $value->getValue()) === true)
94+
if (call_user_func($callback, $value->getKey(), $value->getValue()) === true) {
8995
$matcheds[$value->getKey()] = $value->getValue();
96+
}
9097
}
9198
return count($matcheds) > 0 ? new $this($this->keyType, $this->valueType, $matcheds) : null;
9299
}
@@ -116,9 +123,12 @@ public function find(callable $callback)
116123
*/
117124
public function first()
118125
{
119-
if ($this->count() == 0)
126+
if ($this->count() == 0) {
120127
throw new InvalidOperationException('You cannot get the first element of an empty collection');
121-
foreach ($this->data as $key => $value) return $this->get($key);
128+
}
129+
foreach ($this->data as $key => $value) {
130+
return $this->get($key);
131+
}
122132
}
123133

124134
/**
@@ -162,8 +172,9 @@ public function getValueType()
162172
*/
163173
public function last()
164174
{
165-
if ($this->count() == 0)
175+
if ($this->count() == 0) {
166176
throw new InvalidOperationException('You cannot get the last element of an empty collection');
177+
}
167178
$values = array_values($this->toArray());
168179
return $values[$this->count() - 1];
169180
}
@@ -189,8 +200,9 @@ public function map(callable $callback)
189200
*/
190201
public function merge(Dictionary $newDictionary)
191202
{
192-
foreach ($newDictionary->toArray() as $key => $value)
203+
foreach ($newDictionary->toArray() as $key => $value) {
193204
$this->checkType(['key' => $key, 'value' => $value]);
205+
}
194206
return new $this($this->keyType, $this->valueType, array_merge($this->data, $newDictionary->toArray()));
195207
}
196208

@@ -203,14 +215,16 @@ public function merge(Dictionary $newDictionary)
203215
public function remove($key)
204216
{
205217
$exits = $this->offsetExists($key);
206-
if ($exits) $this->offsetUnset($key);
218+
if ($exits) {
219+
$this->offsetUnset($key);
220+
}
207221
return $exits;
208222
}
209223

210224
/**
211225
* Sort collection data by values
212226
* applying a given callback
213-
*
227+
*
214228
* @param callable $callback
215229
* @return bool
216230
*/
@@ -228,8 +242,9 @@ public function sort(callable $callback)
228242
public function toArray()
229243
{
230244
$array = [];
231-
foreach ($this->data as $pair)
245+
foreach ($this->data as $pair) {
232246
$array[$pair->getKey()] = $pair->getValue();
247+
}
233248
return $array;
234249
}
235250

@@ -245,7 +260,7 @@ public function toJson()
245260
}
246261

247262
/**
248-
* Update the value of one Pair
263+
* Update the value of one Pair
249264
* in the collection
250265
*
251266
* @param mixed $key
@@ -255,9 +270,10 @@ public function toJson()
255270
public function update($key, $value)
256271
{
257272
$this->checkType(['key' => $key, 'value' => $value]);
258-
if (!$this->offsetExists($key))
273+
if (!$this->offsetExists($key)) {
259274
throw new InvalidOperationException('You cannot update a non-existent value');
275+
}
260276
$this->data[$key]->setValue($value);
261277
return $this->data[$key]->getValue() === $value;
262278
}
263-
}
279+
}

0 commit comments

Comments
 (0)