Completed
Push — generators ( 866370...5c61bd )
by Sam
16:42 queued 08:19
created

Query::first()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\ORM\Connect;
4
5
use SilverStripe\Core\Convert;
6
use Iterator;
7
8
/**
9
 * Abstract query-result class.
10
 * Once again, this should be subclassed by an actual database implementation.  It will only
11
 * ever be constructed by a subclass of SS_Database.  The result of a database query - an iteratable object
12
 * that's returned by DB::SS_Query
13
 *
14
 * Primarily, the SS_Query class takes care of the iterator plumbing, letting the subclasses focusing
15
 * on providing the specific data-access methods that are required: {@link nextRecord()}, {@link numRecords()}
16
 * and {@link seek()}
17
 */
18
abstract class Query implements \IteratorAggregate
19
{
20
21
    /**
22
     * Return an array containing all the values from a specific column. If no column is set, then the first will be
23
     * returned
24
     *
25
     * @param string $column
26
     * @return array
27
     */
28
    public function column($column = null)
29
    {
30
        $result = [];
31
32
        foreach ($this as $record) {
33
            if ($column) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $column of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
34
                $result[] = $record[$column];
35
            } else {
36
                $result[] = $record[key($record)];
37
            }
38
        }
39
40
        return $result;
41
    }
42
43
    /**
44
     * Return an array containing all values in the leftmost column, where the keys are the
45
     * same as the values.
46
     *
47
     * @return array
48
     */
49 View Code Duplication
    public function keyedColumn()
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...
50
    {
51
        $column = [];
52
53
        foreach ($this as $record) {
54
            $val = $record[key($record)];
55
            $column[$val] = $val;
56
        }
57
        return $column;
58
    }
59
60
    /**
61
     * Return a map from the first column to the second column.
62
     *
63
     * @return array
64
     */
65 View Code Duplication
    public function map()
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...
66
    {
67
        $column = array();
68
        foreach ($this as $record) {
69
            $key = reset($record);
70
            $val = next($record);
71
            $column[$key] = $val;
72
        }
73
        return $column;
74
    }
75
76
    /**
77
     * Returns the first record in the result
78
     *
79
     * @return array
80
     */
81
    public function record()
82
    {
83
        return $this->getIterator()->current();
0 ignored issues
show
Bug introduced by
The method current cannot be called on $this->getIterator() (of type array).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
84
    }
85
86
    /**
87
     * @deprecated Use record() instead
88
     * @return array
89
     */
90
    public function first()
91
    {
92
        return $this->record();
93
    }
94
95
    /**
96
     * Returns the first column of the first record.
97
     *
98
     * @return string
99
     */
100
    public function value()
101
    {
102
        $record = $this->record();
103
        if ($record) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $record of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
104
            return $record[key($record)];
105
        }
106
        return null;
107
    }
108
109
    /**
110
     * Return an HTML table containing the full result-set
111
     *
112
     * @return string
113
     */
114
    public function table()
115
    {
116
        $first = true;
117
        $result = "<table>\n";
118
119
        foreach ($this as $record) {
120
            if ($first) {
121
                $result .= "<tr>";
122
                foreach ($record as $k => $v) {
123
                    $result .= "<th>" . Convert::raw2xml($k) . "</th> ";
124
                }
125
                $result .= "</tr> \n";
126
            }
127
128
            $result .= "<tr>";
129
            foreach ($record as $k => $v) {
130
                $result .= "<td>" . Convert::raw2xml($v) . "</td> ";
131
            }
132
            $result .= "</tr> \n";
133
134
            $first = false;
135
        }
136
        $result .= "</table>\n";
137
138
        if ($first) {
139
            return "No records found";
140
        }
141
        return $result;
142
    }
143
144
    /**
145
     * Return the next record in the query result.
146
     *
147
     * @return array
148
     */
149
    abstract public function getIterator();
150
151
    /**
152
     * Return the total number of items in the query result.
153
     *
154
     * @return int
155
     */
156
    abstract public function numRecords();
157
}
158