Issues (1697)

sources/ElkArte/Database/AbstractResult.php (3 issues)

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 Beta 1
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
	 */
36
	public function __construct(protected $result, $details = null)
37
	{
38
		$this->details = $details ?? new ValuesContainer();
39
	}
40
41
	/**
42
	 * The destructor is used to free the results.
43
	 */
44
	public function __destruct()
45 301
	{
46
		if (!is_bool($this->result))
47 301
		{
48 301
			$this->free_result();
49 301
		}
50
	}
51
52
	/**
53
	 * Free the resultset.
54 301
	 */
55
	abstract public function free_result();
56 301
57
	/**
58 294
	 * Returns the result object as obtained from the query function
59
	 *
60 301
	 * @deprecated - no longer needed
61
	 */
62
	public function getResultObject()
63
	{
64
		return $this->result;
65
	}
66
67
	/**
68
	 * Returns the value of a "detail"
69
	 *
70 78
	 * @param string $index
71
	 * @return mixed
72 78
	 */
73
	public function getDetail($index)
74
	{
75
		return $this->details[$index] ?? null;
76
	}
77
78 2
	/**
79
	 * Update details
80 2
	 *
81
	 * @param array $details
82
	 */
83
	public function updateDetails($details): void
84
	{
85
		foreach ($details as $key => $val)
86
		{
87
			$this->details[$key] = $val;
88
		}
89
	}
90
91
	/**
92
	 * Allows to check if the results obtained are valid.
93
	 */
94
	public function hasResults(): bool
95
	{
96
		return !empty($this->result);
97
	}
98
99
	/**
100
	 * Affected rows from previous operation.
101
	 *
102
	 * @return int
103
	 */
104
	abstract public function affected_rows();
105
106
	/**
107
	 * Fetch a row from the result set given as parameter.
108
	 */
109
	abstract public function fetch_row();
110
111
	/**
112
	 * Fetch all the results at once.
113
	 */
114
	abstract public function fetch_all();
115
116
	/**
117
	 * Get the number of rows in the result.
118
	 *
119
	 * @return int
120
	 */
121
	abstract public function num_rows();
122
123
	/**
124
	 * Get the number of fields in the resultset.
125
	 *
126
	 * @return int
127
	 */
128
	abstract public function num_fields();
129
130
	/**
131
	 * Reset the internal result pointer.
132
	 *
133
	 * @param int $counter
134
	 *
135
	 * @return bool
136
	 */
137
	abstract public function data_seek($counter);
138
139
	/**
140 268
	 * Last inserted id.
141
	 *
142 268
	 * @return int|string
143
	 */
144 268
	abstract public function insert_id();
145
146 268
	/**
147
	 * Returns the results calling a callback on each row.
148 264
	 *
149
	 * The callback is supposed to accept as argument the row of data fetched
150
	 * by the query from the database.
151
	 *
152
	 * @param callable|null|object|string $callback
153
	 * @param array|null $seeds
154
	 * @return array
155
	 */
156 268
	public function fetch_callback($callback, $seeds = null)
157
	{
158
		$results = $seeds !== null ? (array) $seeds : [];
159
160
		if (!is_bool($this->result))
161
		{
162
			while (($row = $this->fetch_assoc()))
163
			{
164
				$results[] = call_user_func($callback, $row);
0 ignored issues
show
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

164
				$results[] = call_user_func(/** @scrutinizer ignore-type */ $callback, $row);
Loading history...
165
			}
166
		}
167
		else
168
		{
169
			$results = (bool) $this->result;
170
		}
171
172
		return $results;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $results also could return the type boolean which is incompatible with the documented return type array.
Loading history...
173
	}
174
175
	/**
176
	 * Fetch next result as association.
177
	 */
178
	abstract public function fetch_assoc();
179
}
180