AbstractResult::getResultObject()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file provides an implementation of the most common functions needed
5
 * for the database drivers to work.
6
 *
7
 * @package   ElkArte Forum
8
 * @copyright ElkArte Forum contributors
9
 * @license   BSD http://opensource.org/licenses/BSD-3-Clause (see accompanying LICENSE.txt file)
10
 *
11
 * This file contains code covered by:
12
 * copyright: 2011 Simple Machines (http://www.simplemachines.org)
13
 *
14
 * @version 2.0 dev
15
 *
16
 */
17
18
namespace ElkArte\Database;
19
20
use ElkArte\Helper\ValuesContainer;
21
22
/**
23
 * Abstract database class, implements database to control functions
24
 */
25
abstract class AbstractResult
26
{
27
	/** @var ValuesContainer */
28
	protected $details;
29
30
	/**
31
	 * Constructor.
32
	 *
33
	 * @param $result Object
34
	 * @param null $details
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $details is correct as it would always require null to be passed?
Loading history...
35
	 * @param resource $result
36
	 */
37
	public function __construct(protected $result, $details = null)
38
	{
39
		$this->details = $details ?? new ValuesContainer();
40
	}
41
42
	/**
43
	 * The destructor is used to free the results.
44
	 */
45 301
	public function __destruct()
46
	{
47 301
		if (!is_bool($this->result))
0 ignored issues
show
introduced by
The condition is_bool($this->result) is always false.
Loading history...
48 301
		{
49 301
			$this->free_result();
50
		}
51
	}
52
53
	/**
54 301
	 * Free the resultset.
55
	 */
56 301
	abstract public function free_result();
57
58 294
	/**
59
	 * Returns the result object as obtained from the query function
60 301
	 *
61
	 * @deprecated - no longer needed
62
	 */
63
	public function getResultObject()
64
	{
65
		return $this->result;
66
	}
67
68
	/**
69
	 * Returns the value of a "detail"
70 78
	 *
71
	 * @param string $index
72 78
	 * @return mixed
73
	 */
74
	public function getDetail($index)
75
	{
76
		return $this->details[$index] ?? null;
77
	}
78 2
79
	/**
80 2
	 * Update details
81
	 *
82
	 * @param array $details
83
	 */
84
	public function updateDetails($details)
85
	{
86
		foreach ($details as $key => $val)
87
		{
88
			$this->details[$key] = $val;
89
		}
90
	}
91
92
	/**
93
	 * Allows to check if the results obtained are valid.
94
	 */
95
	public function hasResults()
96
	{
97
		return !empty($this->result);
98
	}
99
100
	/**
101
	 * Affected rows from previous operation.
102
	 *
103
	 * @return int
104
	 */
105
	abstract public function affected_rows();
106
107
	/**
108
	 * Fetch a row from the result set given as parameter.
109
	 */
110
	abstract public function fetch_row();
111
112
	/**
113
	 * Fetch all the results at once.
114
	 */
115
	abstract public function fetch_all();
116
117
	/**
118
	 * Get the number of rows in the result.
119
	 *
120
	 * @return int
121
	 */
122
	abstract public function num_rows();
123
124
	/**
125
	 * Get the number of fields in the resultset.
126
	 *
127
	 * @return int
128
	 */
129
	abstract public function num_fields();
130
131
	/**
132
	 * Reset the internal result pointer.
133
	 *
134
	 * @param int $counter
135
	 *
136
	 * @return bool
137
	 */
138
	abstract public function data_seek($counter);
139
140 268
	/**
141
	 * Last inserted id.
142 268
	 *
143
	 * @return int|string
144 268
	 */
145
	abstract public function insert_id();
146 268
147
	/**
148 264
	 * Returns the results calling a callback on each row.
149
	 *
150
	 * The callback is supposed to accept as argument the row of data fetched
151
	 * by the query from the database.
152
	 *
153
	 * @param callable|null|object|string $callback
154
	 * @param array|null
155
	 * @return array
156 268
	 */
157
	public function fetch_callback($callback, $seeds = null)
158
	{
159
		$results = $seeds !== null ? (array) $seeds : array();
160
161
		if (!is_bool($this->result))
0 ignored issues
show
introduced by
The condition is_bool($this->result) is always false.
Loading history...
162
		{
163
			while (($row = $this->fetch_assoc()))
164
			{
165
				$results[] = call_user_func($callback, $row);
0 ignored issues
show
Bug introduced by
It seems like $callback can also be of type null and object; however, parameter $callback of call_user_func() does only seem to accept callable, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

165
				$results[] = call_user_func(/** @scrutinizer ignore-type */ $callback, $row);
Loading history...
166
			}
167
		}
168
		else
169
		{
170
			$results = (bool) $this->result;
171
		}
172
173
		return $results;
174
	}
175
176
	/**
177
	 * Fetch next result as association.
178
	 */
179
	abstract public function fetch_assoc();
180
}
181