Skip to content

Commit 4f9485d

Browse files
committed
Remove all redundant references
1 parent e56b182 commit 4f9485d

File tree

1 file changed

+20
-37
lines changed

1 file changed

+20
-37
lines changed

src/Mysqli/MysqliStatement.php

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ class MysqliStatement implements StatementInterface
3131
* @var array
3232
* @since 2.0.0
3333
*/
34-
protected $bindedValues;
34+
protected $bindedValues = [];
3535

3636
/**
3737
* Mapping between named parameters and position in query.
3838
*
3939
* @var array
4040
* @since 2.0.0
4141
*/
42-
protected $parameterKeyMapping;
42+
protected $parameterKeyMapping = [];
4343

4444
/**
4545
* Mapping array for parameter types.
@@ -301,27 +301,23 @@ public function bindParam($parameter, &$variable, string $dataType = ParameterTy
301301
*/
302302
private function bindValues(array $values)
303303
{
304-
$params = [];
305304
$types = str_repeat('s', \count($values));
306305

307-
if (!empty($this->parameterKeyMapping)) {
308-
foreach ($values as $key => &$value) {
306+
if ($this->parameterKeyMapping !== []) {
307+
$params = [];
308+
foreach ($values as $key => $value) {
309309
$paramKey = $this->parameterKeyMapping[$key];
310310
foreach ($paramKey as $currentKey) {
311-
$params[$currentKey] =& $value;
311+
$params[$currentKey] = $value;
312312
}
313313
}
314314

315315
ksort($params);
316-
} else {
317-
foreach ($values as $key => &$value) {
318-
$params[] =& $value;
319-
}
320-
}
321316

322-
array_unshift($params, $types);
317+
return $this->statement->bind_param($types, ...$params);
318+
}
323319

324-
return \call_user_func_array([$this->statement, 'bind_param'], $params);
320+
return $this->statement->bind_param($types, ...$values);
325321
}
326322

327323
/**
@@ -372,32 +368,30 @@ public function errorInfo()
372368
*/
373369
public function execute(?array $parameters = null)
374370
{
375-
if ($this->bindedValues !== null) {
371+
if ($this->bindedValues !== []) {
376372
$params = [];
377373
$types = [];
378374

379-
if (!empty($this->parameterKeyMapping)) {
380-
foreach ($this->bindedValues as $key => &$value) {
375+
if ($this->parameterKeyMapping !== []) {
376+
foreach ($this->bindedValues as $key => $value) {
381377
$paramKey = $this->parameterKeyMapping[$key];
382378

383379
foreach ($paramKey as $currentKey) {
384-
$params[$currentKey] =& $value;
380+
$params[$currentKey] = $value;
385381
$types[$currentKey] = $this->typesKeyMapping[$key];
386382
}
387383
}
388384
} else {
389-
foreach ($this->bindedValues as $key => &$value) {
390-
$params[] =& $value;
385+
foreach ($this->bindedValues as $key => $value) {
386+
$params[] = $value;
391387
$types[$key] = $this->typesKeyMapping[$key];
392388
}
393389
}
394390

395391
ksort($params);
396392
ksort($types);
397393

398-
array_unshift($params, implode('', $types));
399-
400-
if (!\call_user_func_array([$this->statement, 'bind_param'], $params)) {
394+
if (!$this->statement->bind_param(implode('', $types), ...$params)) {
401395
throw new PrepareStatementFailureException($this->statement->error, $this->statement->errno);
402396
}
403397
} elseif ($parameters !== null) {
@@ -418,15 +412,7 @@ public function execute(?array $parameters = null)
418412
$meta = $this->statement->result_metadata();
419413

420414
if ($meta !== false) {
421-
$columnNames = [];
422-
423-
foreach ($meta->fetch_fields() as $col) {
424-
$columnNames[] = $col->name;
425-
}
426-
427-
$meta->free();
428-
429-
$this->columnNames = $columnNames;
415+
$this->columnNames = array_column($meta->fetch_fields(), 'name');
430416
} else {
431417
$this->columnNames = false;
432418
}
@@ -436,13 +422,10 @@ public function execute(?array $parameters = null)
436422
$this->statement->store_result();
437423

438424
$this->rowBindedValues = array_fill(0, \count($this->columnNames), null);
439-
$refs = [];
440-
441-
foreach ($this->rowBindedValues as $key => &$value) {
442-
$refs[$key] =& $value;
443-
}
425+
// The following is necessary as PHP cannot handle references to properties properly
426+
$refs =& $this->rowBindedValues;
444427

445-
if (!\call_user_func_array([$this->statement, 'bind_result'], $refs)) {
428+
if (!$this->statement->bind_result(...$refs)) {
446429
throw new \RuntimeException($this->statement->error, $this->statement->errno);
447430
}
448431
}

0 commit comments

Comments
 (0)