Completed
Push — master ( af77e8...7d9d07 )
by Zack
11s
created

Collection::all()   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 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 12 and the first side effect is on line 6.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
namespace GV;
3
4
/** If this file is called directly, abort. */
5
if ( ! defined( 'GRAVITYVIEW_DIR' ) ) {
6
	die();
7
}
8
9
/**
10
 * A generic Collection base class.
11
 */
12
class Collection {
13
	/**
14
	 * @var array Main storage for objects in this collection.
15
	 */
16
	private $storage = array();
17
18
	/**
19
	 * Add an object to this collection.
20
	 *
21
	 * @param mixed $value The object to be added.
22
	 *
23
	 * @api
24
	 * @since future
25
	 * @return void
26
	 */
27 2
	public function add( $value ) {
28 2
		$this->storage []= $value;
0 ignored issues
show
introduced by
Expected 1 space before "="; 0 found
Loading history...
29 2
	}
30
31
	/**
32
	 * Clear this collection.
33
	 *
34
	 * @api
35
	 * @since future
36
	 * @return void
37
	 */
38 1
	public function clear() {
39 1
		$this->count() && ( $this->storage = array() );
40 1
	}
41
42
	/**
43
	 * Merge another collection into here.
44
	 *
45
	 * @param \GV\Collection $collection The collection to be merged.
46
	 *
47
	 * @api
48
	 * @since future
49
	 * @return void
50
	 */
51 1
	public function merge( \GV\Collection $collection ) {
52 1
		array_map( array( $this, 'add'), $collection->all() );
0 ignored issues
show
introduced by
No space before closing parenthesis of array is bad style
Loading history...
53 1
	}
54
55
	/**
56
	 * Returns all the objects in this collection as an an array.
57
	 *
58
	 * @api
59
	 * @since future
60
	 * @return array The objects in this collection.
61
	 */
62 2
	public function all() {
63 2
		return $this->storage;
64
	}
65
66
	/**
67
	 * Get the last added object.
68
	 *
69
	 * @api
70
	 * @since future
71
	 * @return mixed|null The last item in here, or null if there are none.
72
	 */
73 1
	public function last() {
74 1
		return end( $this->storage );
75
	}
76
77
	/**
78
	 * Returns the count of the objects in this collection.
79
	 *
80
	 * @api
81
	 * @since future
82
	 * @return int The size of this collection.
83
	 */
84 3
	public function count() {
85 3
		return count( $this->storage );
86
	}
87
}
88