Conditions | 2 |
Paths | 2 |
Total Lines | 51 |
Code Lines | 16 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
160 | protected function loadPreferences(): void |
||
161 | { |
||
162 | /** |
||
163 | * OK, this is a bit of a complicated query. |
||
164 | * It's designed to get all the preferences defined for a user in a specified domain, falling back to globally |
||
165 | * defined preferences if a local preference isn't set. As such, this query is the *heart* of how global |
||
166 | * preferences work. |
||
167 | * |
||
168 | * Starting with the WHERE, we filter rows: |
||
169 | * a) where the row's domain is the domain we're looking for |
||
170 | * b) where the row's domain is null, thus it's a global setting |
||
171 | * c) if we don't have a domain we're looking for, fall back to global only |
||
172 | * |
||
173 | * The MAX(...) OVER(...) is a window function, *not* an aggregate. It basically takes the max of all selected |
||
174 | * rows' domain columns, grouped by the preference column. Since any number N < null, this highlights all the |
||
175 | * correct settings (local has precedence over global) such that prefpart == domain. |
||
176 | * |
||
177 | * -1 is used to represent null in the COALESCE() calls, since domain.id is an unsigned int hence -1 is an |
||
178 | * impossible value |
||
179 | */ |
||
180 | $sql = /** @lang SQL */ |
||
181 | <<<'EOF' |
||
182 | WITH allprefs AS ( |
||
183 | SELECT up.domain, up.preference, MAX(up.domain) OVER (PARTITION BY up.preference) AS prefpart, up.value, CASE WHEN up.domain IS NULL THEN 1 END AS isglobal |
||
184 | FROM userpreference up |
||
185 | WHERE COALESCE(up.domain, :domainc, -1) = COALESCE(:domain, -1) |
||
186 | AND up.user = :user |
||
187 | ) |
||
188 | SELECT p.preference, p.value, coalesce(p.isglobal, 0) as isglobal |
||
189 | FROM allprefs p |
||
190 | WHERE COALESCE(p.prefpart, -1) = COALESCE(p.domain, -1); |
||
191 | EOF; |
||
192 | $statement = $this->database->prepare($sql); |
||
193 | |||
194 | $statement->execute([ |
||
195 | ':domain' => $this->domain, |
||
196 | ':domainc' => $this->domain, |
||
197 | ':user' => $this->user, |
||
198 | ]); |
||
199 | |||
200 | $rawPrefs = $statement->fetchAll(PDO::FETCH_ASSOC); |
||
201 | $prefs = []; |
||
202 | |||
203 | foreach ($rawPrefs as $p) { |
||
204 | $prefs[$p['preference']] = [ |
||
205 | 'value' => $p['value'], |
||
206 | 'global' => $p['isglobal'] == 1, |
||
207 | ]; |
||
208 | } |
||
209 | |||
210 | $this->cachedPreferences = $prefs; |
||
211 | } |
||
213 |