Completed
Branch develop (bcdff2)
by Michael
04:41 queued 01:33
created

AbstractActiveRecordSearch::search()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 28
ccs 0
cts 21
cp 0
rs 8.8571
cc 3
eloc 16
nc 3
nop 1
crap 12
1
<?php
2
3
/**
4
 * This file is part of the miBadger package.
5
 *
6
 * @author Michael Webbers <[email protected]>
7
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
8
 * @version 1.0.0
9
 */
10
11
namespace miBadger\ActiveRecord;
12
13
/**
14
 * The abstract active record search class.
15
 *
16
 * @since 1.0.0
17
 */
18
abstract class AbstractActiveRecordSearch extends AbstractActiveRecord
19
{
20
	/**
21
	 * {@inheritdoc}
22
	 */
23
	public function search($options = [])
24
	{
25
		$name = $this->getActiveRecordName();
0 ignored issues
show
Unused Code introduced by
$name is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
26
		$data = $this->getActiveRecordData();
0 ignored issues
show
Unused Code introduced by
$data is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
27
28
		$pdoStatement = $this->getPdo()->prepare($this->getSearchQuery($options));
29
		array_walk_recursive($options, function (&$value) use ($pdoStatement) {
30
			static $index = 1;
31
32
			$pdoStatement->bindParam($index++, $value);
33
		});
34
35
		$pdoStatement->execute();
36
		$result = [];
37
38
		while ($row = $pdoStatement->fetch()) {
39
			$x = new static($this->getPdo());
40
			$x->id = intval($row['id']);
0 ignored issues
show
Bug introduced by
The property id cannot be accessed from this context as it is declared private in class miBadger\ActiveRecord\AbstractActiveRecord.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
41
42
			foreach ($x->getActiveRecordData() as $key => &$value) {
0 ignored issues
show
Bug introduced by
The expression $x->getActiveRecordData() cannot be used as a reference.

Let?s assume that you have the following foreach statement:

foreach ($array as &$itemValue) { }

$itemValue is assigned by reference. This is possible because the expression (in the example $array) can be used as a reference target.

However, if we were to replace $array with something different like the result of a function call as in

foreach (getArray() as &$itemValue) { }

then assigning by reference is not possible anymore as there is no target that could be modified.

Available Fixes

1. Do not assign by reference
foreach (getArray() as $itemValue) { }
2. Assign to a local variable first
$array = getArray();
foreach ($array as &$itemValue) {}
3. Return a reference
function &getArray() { $array = array(); return $array; }

foreach (getArray() as &$itemValue) { }
Loading history...
43
				$value = $row[$key];
44
			}
45
46
			$result[] = $x;
47
		}
48
49
		return $result;
50
	}
51
52
	/**
53
	 * Returns the search query with the given options.
54
	 *
55
	 * @param arrray $options = []
0 ignored issues
show
Documentation introduced by
Should the type for parameter $options not be arrray|array?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
56
	 * @return string the search query with the given options.
57
	 */
58
	private function getSearchQuery($options = [])
59
	{
60
		$columns = array_keys($this->getActiveRecordData());
61
		$values = [];
62
63
		foreach ($options as $key => $value) {
64
			if (!in_array($key, $columns)) {
65
				throw new ActiveRecordException('Invalid option key.');
66
			}
67
68
			if (is_int($value)) {
69
				$values[] = $key . ' = ?';
70
			} elseif (is_string($value)) {
71
				$values[] = $key . ' LIKE ?';
72
			} elseif(is_array($value) && !empty($value)) {
73
				$values[] = $key . ' IN(' . implode(',', array_fill(0, count($value), '?')) . ')';
74
			} else {
75
				throw new ActiveRecordException('Invalid option value.');
76
			}
77
		}
78
79
		return sprintf('SELECT * FROM %s %s %s', $this->getActiveRecordName(), empty($values) ? '' : 'WHERE', implode(' AND ', $values));
80
	}
81
}
82