Passed
Pull Request — development (#3445)
by Emanuele
06:16
created

AbstractResult::updateDetails()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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

172
				$results[] = call_user_func(/** @scrutinizer ignore-type */ $callback, $row);
Loading history...
173
			}
174
		}
175
		else
176
		{
177
			$results = (bool) $this->result;
178
		}
179
180
		return $results;
181
	}
182
183
	/**
184
	 * Fetch next result as association.
185
	 */
186
	abstract public function fetch_assoc();
187
}
188