GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Result::getIterator()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace JamesMoss\Flywheel;
4
5
/**
6
 * Result
7
 *
8
 * A collection of Documents returned from a Query.
9
 *
10
 * This class acts like an array but also has some helper methods for
11
 * manipulating the collection in useful ways.
12
 */
13
class Result implements \IteratorAggregate, \ArrayAccess, \Countable
14
{
15
    protected $documents;
16
    protected $total;
17
18
    /**
19
     * Constructor
20
     *
21
     * @param array   $documents An array of Documents
22
     * @param integer $total     If this result only represents a small slice of
23
     *                           the total (when using limit), this parameter
24
     *                           represents the total number of documents.
25
     */
26 27
    public function __construct($documents, $total)
27
    {
28 27
        $this->documents = array_values($documents);
29 27
        $this->total     = $total;
30 27
    }
31
32
    /**
33
     * Returns the number of documents in this result
34
     *
35
     * @return integer The number of documents
36
     */
37 10
    public function count()
38
    {
39 10
        return count($this->documents);
40
    }
41
42
    /**
43
     * Returns the total number of documents (if using limit in a query).
44
     * Useful for working out pagination.
45
     *
46
     * @return integer The total number of documents
47
     */
48 8
    public function total()
49
    {
50 8
        return $this->total;
51
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 1
    public function getIterator()
57
    {
58 1
        return new \ArrayIterator($this->documents);
59
    }
60
61
    /**
62
     * @inheritdoc
63
     */
64 1
    public function offsetSet($offset, $value)
65
    {
66 1
        throw new \Exception('Cannot set values on Flywheel\Result');
67
    }
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function offsetExists($offset)
73
    {
74
        return isset($this->documents[$offset]);
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80 1
    public function offsetUnset($offset)
81
    {
82 1
        throw new \Exception('Cannot unset values on Flywheel\Result');
83
    }
84
85
    /**
86
     * @inheritdoc
87
     */
88 6
    public function offsetGet($offset)
89
    {
90 6
        return isset($this->documents[$offset]) ? $this->documents[$offset] : null;
91
    }
92
93
    /**
94
     * Gets the first document from the result.
95
     *
96
     * @return mixed The first document, or false if the result is empty.
97
     */
98 11
    public function first()
99
    {
100 11
        return !empty($this->documents) ? $this->documents[0] : false;
101
    }
102
103
    /**
104
     * Gets the last document from the results.
105
     *
106
     * @return mixed The last document, or false if the result is empty.
107
     */
108 1
    public function last()
109
    {
110 1
        return !empty($this->documents) ? $this->documents[count($this->documents) - 1] : false;
111
    }
112
113
    /**
114
     * Get the  value specified by $key of the first object in the result.
115
     *
116
     * @return mixed The value, or false if there are no documents or the key
117
     *               doesnt exist.
118
     */
119 3
    public function value($key)
120
    {
121 3
        $first = $this->first();
122
123 3
        if (!$first) {
124 1
            return false;
125
        }
126
127 2
        return isset($first->{$key}) ? $first->{$key} : false;
128
    }
129
130
    /**
131
     * Returns an array where each value is a single property from each
132
     * document. If the property doesnt exist on the document then it won't
133
     * be in the returned array.
134
     *
135
     * @param string $field The name of the field to pick.
136
     *
137
     * @return array The array of values, one from each document.
138
     */
139 1
    public function pick($field)
140
    {
141 1
        $result = array();
142
143 1
        foreach ($this->documents as $document) {
144 1
            if (isset($document->{$field})) {
145 1
                $result[] = $document->{$field};
146 1
            }
147 1
        }
148
149 1
        return $result;
150
    }
151
152
    /**
153
     * Returns an assoiative array (a hash), where for each document the
154
     * value of one property is the key, and another property is the value.
155
     *
156
     * @param string $keyField   The name of the property to use for the key.
157
     * @param string $valueField Name of the property to use for the value.
158
     *
159
     * @return array An associative array.
160
     */
161 1
    public function hash($keyField, $valueField)
162
    {
163 1
        $result = array();
164
165 1
        foreach ($this->documents as $document) {
166 1
            $result[$document->{$keyField}] = $document->{$valueField};
167 1
        }
168
169 1
        return $result;
170
    }
171
}
172