Skip to content

Commit d8743f2

Browse files
Remove all redundant references (#350)
1 parent a567e9b commit d8743f2

1 file changed

Lines changed: 20 additions & 37 deletions

File tree

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,33 +368,31 @@ 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-
400394
try {
401-
\call_user_func_array([$this->statement, 'bind_param'], $params);
395+
$this->statement->bind_param(implode('', $types), ...$params);
402396
} catch (\Exception $e) {
403397
throw new PrepareStatementFailureException($e->getMessage(), $e->getCode(), $e);
404398
}
@@ -420,15 +414,7 @@ public function execute(?array $parameters = null)
420414
$meta = $this->statement->result_metadata();
421415

422416
if ($meta !== false) {
423-
$columnNames = [];
424-
425-
foreach ($meta->fetch_fields() as $col) {
426-
$columnNames[] = $col->name;
427-
}
428-
429-
$meta->free();
430-
431-
$this->columnNames = $columnNames;
417+
$this->columnNames = array_column($meta->fetch_fields(), 'name');
432418
} else {
433419
$this->columnNames = false;
434420
}
@@ -438,13 +424,10 @@ public function execute(?array $parameters = null)
438424
$this->statement->store_result();
439425

440426
$this->rowBindedValues = array_fill(0, \count($this->columnNames), null);
441-
$refs = [];
442-
443-
foreach ($this->rowBindedValues as $key => &$value) {
444-
$refs[$key] =& $value;
445-
}
427+
// The following is necessary as PHP cannot handle references to properties properly
428+
$refs =& $this->rowBindedValues;
446429

447-
\call_user_func_array([$this->statement, 'bind_result'], $refs);
430+
$this->statement->bind_result(...$refs);
448431
}
449432

450433
$this->result = true;

0 commit comments

Comments
 (0)