Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

engine/classes/Elgg/Database/Repository.php (3 issues)

1
<?php
2
3
namespace Elgg\Database;
4
5
use Elgg\Database\Clauses\GroupByClause;
6
use Elgg\Database\Clauses\HavingClause;
7
use Elgg\Database\Clauses\JoinClause;
8
use Elgg\Database\Clauses\OrderByClause;
9
use Elgg\Database\Clauses\SelectClause;
10
use Elgg\Database\Clauses\WhereClause;
11
12
/**
13
 * Abstract methods for interfacing with the database
14
 */
15
abstract class Repository implements QueryExecuting {
16
17
	/**
18
	 * @var QueryOptions
19
	 */
20
	protected $options;
21
22
	/**
23
	 * Constructor
24
	 *
25
	 * @param array $options ege* options
26
	 */
27 1247
	public function __construct(array $options = []) {
28 1247
		$this->options = new QueryOptions($options, \ArrayObject::ARRAY_AS_PROPS);
29 1247
	}
30
31
	/**
32
	 * {@inheritdoc}
33
	 */
34 2
	public function __get($name) {
35 2
		if (!isset($this->options->$name)) {
36 2
			return;
37
		}
38
39 1
		$val = &$this->options->$name;
40
41 1
		return $val;
42
	}
43
44
	/**
45
	 * {@inheritdoc}
46
	 */
47 2
	public function __set($name, $value) {
48 2
		$this->options->$name = $value;
49 2
	}
50
51
	/**
52
	 * {@inheritdoc}
53
	 */
54
	public function __unset($name) {
55
		unset($this->options->$name);
56
	}
57
58
	/**
59
	 * {@inheritdoc}
60
	 */
61
	public function __isset($name) {
62
		return isset($this->options->$name);
63
	}
64
65
	/**
66
	 * Constructs a new
67
	 *
68
	 * @param array $options ege* options
69
	 *
70
	 * @return static
71
	 */
72 1247
	public static function with(array $options = null) {
73 1247
		$query = new static($options);
0 ignored issues
show
It seems like $options can also be of type null; however, parameter $options of Elgg\Database\Repository::__construct() does only seem to accept array, 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

73
		$query = new static(/** @scrutinizer ignore-type */ $options);
Loading history...
74
75 1247
		return $query;
76
	}
77
78
	/**
79
	 * Build and execute a new query from an array of legacy options
80
	 *
81
	 * @param array $options Options
82
	 *
83
	 * @return ElggData[]|int|mixed
84
	 */
85 1229
	public static function find(array $options = []) {
86
		try {
87 1229
			return static::with($options)->execute();
88 16
		} catch (\DataFormatException $e) {
89 9
			return elgg_extract('count', $options) ? 0 : false;
90
		}
91
	}
92
93
	/**
94
	 * {@inheritdoc}
95
	 */
96 302
	public function batch($limit = null, $offset = null, $callback = null) {
97
98 302
		$options = $this->options->getArrayCopy();
99
100 302
		$options['limit'] = (int) $limit;
101 302
		$options['offset'] = (int) $offset;
102 302
		$options['callback'] = $callback;
103 302
		unset($options['count'],
104 302
			$options['batch'],
105 302
			$options['batch_size'],
106 302
			$options['batch_inc_offset']
107
		);
108
109 302
		$batch_size = $this->options->batch_size;
110 302
		$batch_inc_offset = $this->options->batch_inc_offset;
111
112 302
		return new \ElggBatch([static::class, 'find'], $options, null, $batch_size, $batch_inc_offset);
113
	}
114
115
	/**
116
	 * {@inheritdoc}
117
	 */
118
	public function filter(\Closure $closure) {
119
		$this->options->where(new WhereClause($closure));
120
121
		return $this;
122
	}
123
124
	/**
125
	 * {@inheritdoc}
126
	 */
127
	public function select($expression) {
128
		$this->options->select(new SelectClause($expression));
129
130
		return $this;
131
	}
132
133
	/**
134
	 * {@inheritdoc}
135
	 */
136
	public function join($joined_table, $joined_alias = null, $x = null, $comparison = null, $y = null, $type = null, $case_sensitive = null) {
137
		$join = new JoinClause($joined_table, $joined_alias, function (QueryBuilder $qb, $joined_alias) use ($x, $comparison, $y, $type, $case_sensitive) {
138
			return $qb->compare("$joined_alias.$x", $comparison, $y, $type, $case_sensitive);
139
		});
140
		$this->options->join($join);
141
142
		return $this;
143
	}
144
145
	/**
146
	 * {@inheritdoc}
147
	 */
148
	public function groupBy($expression) {
149
		$this->options->groupBy(new GroupByClause($expression));
150
151
		return $this;
152
	}
153
154
	/**
155
	 * {@inheritdoc}
156
	 */
157
	public function having($expression) {
158
		$this->options->having(new HavingClause($expression));
159
160
		return $this;
161
	}
162
163
	/**
164
	 * {@inheritdoc}
165
	 */
166
	public function orderBy($expression, $direction) {
167
		$this->options->orderBy(new OrderByClause($expression, $direction));
168
169
		return $this;
170
	}
171
172
	/**
173
	 * Extend query builder with select, group_by, having and order_by clauses from $options
174
	 *
175
	 * @param QueryBuilder $qb          Query builder
176
	 * @param              $table_alias Table alias
0 ignored issues
show
The type Elgg\Database\Table was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
177
	 *
178
	 * @return void
179
	 */
180 1205
	public function expandInto(QueryBuilder $qb, $table_alias = nul) {
0 ignored issues
show
The constant Elgg\Database\nul was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
181 1205
		foreach ($this->options->selects as $select_clause) {
182 26
			$select_clause->prepare($qb, $table_alias);
183
		}
184
185 1205
		foreach ($this->options->group_by as $group_by_clause) {
186 17
			$group_by_clause->prepare($qb, $table_alias);
187
		}
188
189 1205
		foreach ($this->options->having as $having_clause) {
190 5
			$having_clause->prepare($qb, $table_alias);
191
		}
192
193 1205
		if (!empty($this->options->order_by)) {
194 645
			foreach ($this->options->order_by as $order_by_clause) {
195 645
				$order_by_clause->prepare($qb, $table_alias);
196
			}
197
		}
198 1205
	}
199
}
200