Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Database often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Database, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class Database |
||
8 | { |
||
9 | /** |
||
10 | * fieldExtract. |
||
11 | * |
||
12 | * Extracts the column ID to use for the data field. |
||
13 | * |
||
14 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
15 | * A database is a list of related data in which rows of related |
||
16 | * information are records, and columns of data are fields. The |
||
17 | * first row of the list contains labels for each column. |
||
18 | * @param mixed $field Indicates which column is used in the function. Enter the |
||
19 | * column label enclosed between double quotation marks, such as |
||
20 | * "Age" or "Yield," or a number (without quotation marks) that |
||
21 | * represents the position of the column within the list: 1 for |
||
22 | * the first column, 2 for the second column, and so on. |
||
23 | * |
||
24 | * @return null|string |
||
25 | */ |
||
26 | 10 | private static function fieldExtract($database, $field) |
|
27 | { |
||
28 | 10 | $field = strtoupper(Functions::flattenSingleValue($field)); |
|
29 | 10 | $fieldNames = array_map('strtoupper', array_shift($database)); |
|
30 | |||
31 | 10 | if (is_numeric($field)) { |
|
32 | 8 | $keys = array_keys($fieldNames); |
|
33 | |||
34 | 8 | return $keys[$field - 1]; |
|
35 | } |
||
36 | 10 | $key = array_search($field, $fieldNames); |
|
37 | |||
38 | 10 | return ($key) ? $key : null; |
|
39 | } |
||
40 | |||
41 | /** |
||
42 | * filter. |
||
43 | * |
||
44 | * Parses the selection criteria, extracts the database rows that match those criteria, and |
||
45 | * returns that subset of rows. |
||
46 | * |
||
47 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
48 | * A database is a list of related data in which rows of related |
||
49 | * information are records, and columns of data are fields. The |
||
50 | * first row of the list contains labels for each column. |
||
51 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
52 | * You can use any range for the criteria argument, as long as it |
||
53 | * includes at least one column label and at least one cell below |
||
54 | * the column label in which you specify a condition for the |
||
55 | * column. |
||
56 | * |
||
57 | * @return array of mixed |
||
58 | */ |
||
59 | 10 | private static function filter($database, $criteria) |
|
60 | { |
||
61 | 10 | $fieldNames = array_shift($database); |
|
62 | 10 | $criteriaNames = array_shift($criteria); |
|
63 | |||
64 | // Convert the criteria into a set of AND/OR conditions with [:placeholders] |
||
65 | 10 | $testConditions = $testValues = []; |
|
|
|||
66 | 10 | $testConditionsCount = 0; |
|
67 | 10 | foreach ($criteriaNames as $key => $criteriaName) { |
|
68 | 10 | $testCondition = []; |
|
69 | 10 | $testConditionCount = 0; |
|
70 | 10 | foreach ($criteria as $row => $criterion) { |
|
71 | 10 | if ($criterion[$key] > '') { |
|
72 | 10 | $testCondition[] = '[:' . $criteriaName . ']' . Functions::ifCondition($criterion[$key]); |
|
73 | 10 | ++$testConditionCount; |
|
74 | } |
||
75 | } |
||
76 | 10 | if ($testConditionCount > 1) { |
|
77 | 8 | $testConditions[] = 'OR(' . implode(',', $testCondition) . ')'; |
|
78 | 8 | ++$testConditionsCount; |
|
79 | 6 | } elseif ($testConditionCount == 1) { |
|
80 | 6 | $testConditions[] = $testCondition[0]; |
|
81 | 10 | ++$testConditionsCount; |
|
82 | } |
||
83 | } |
||
84 | |||
85 | 10 | if ($testConditionsCount > 1) { |
|
86 | 6 | $testConditionSet = 'AND(' . implode(',', $testConditions) . ')'; |
|
87 | 8 | } elseif ($testConditionsCount == 1) { |
|
88 | 8 | $testConditionSet = $testConditions[0]; |
|
89 | } |
||
90 | |||
91 | // Loop through each row of the database |
||
92 | 10 | foreach ($database as $dataRow => $dataValues) { |
|
93 | // Substitute actual values from the database row for our [:placeholders] |
||
94 | 10 | $testConditionList = $testConditionSet; |
|
95 | 10 | foreach ($criteriaNames as $key => $criteriaName) { |
|
96 | 10 | $k = array_search($criteriaName, $fieldNames); |
|
97 | 10 | if (isset($dataValues[$k])) { |
|
98 | 10 | $dataValue = $dataValues[$k]; |
|
99 | 10 | $dataValue = (is_string($dataValue)) ? Calculation::wrapResult(strtoupper($dataValue)) : $dataValue; |
|
100 | 10 | $testConditionList = str_replace('[:' . $criteriaName . ']', $dataValue, $testConditionList); |
|
101 | } |
||
102 | } |
||
103 | // evaluate the criteria against the row data |
||
104 | 10 | $result = Calculation::getInstance()->_calculateFormulaValue('=' . $testConditionList); |
|
105 | // If the row failed to meet the criteria, remove it from the database |
||
106 | 10 | if (!$result) { |
|
107 | 10 | unset($database[$dataRow]); |
|
108 | } |
||
109 | } |
||
110 | |||
111 | 10 | return $database; |
|
112 | } |
||
113 | |||
114 | 10 | private static function getFilteredColumn($database, $field, $criteria) |
|
115 | { |
||
116 | // reduce the database to a set of rows that match all the criteria |
||
117 | 10 | $database = self::filter($database, $criteria); |
|
118 | // extract an array of values for the requested column |
||
119 | 10 | $colData = []; |
|
120 | 10 | foreach ($database as $row) { |
|
121 | 10 | $colData[] = $row[$field]; |
|
122 | } |
||
123 | |||
124 | 10 | return $colData; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * DAVERAGE. |
||
129 | * |
||
130 | * Averages the values in a column of a list or database that match conditions you specify. |
||
131 | * |
||
132 | * Excel Function: |
||
133 | * DAVERAGE(database,field,criteria) |
||
134 | * |
||
135 | * @category Database Functions |
||
136 | * |
||
137 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
138 | * A database is a list of related data in which rows of related |
||
139 | * information are records, and columns of data are fields. The |
||
140 | * first row of the list contains labels for each column. |
||
141 | * @param int|string $field Indicates which column is used in the function. Enter the |
||
142 | * column label enclosed between double quotation marks, such as |
||
143 | * "Age" or "Yield," or a number (without quotation marks) that |
||
144 | * represents the position of the column within the list: 1 for |
||
145 | * the first column, 2 for the second column, and so on. |
||
146 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
147 | * You can use any range for the criteria argument, as long as it |
||
148 | * includes at least one column label and at least one cell below |
||
149 | * the column label in which you specify a condition for the |
||
150 | * column. |
||
151 | * |
||
152 | * @return float |
||
153 | */ |
||
154 | 1 | View Code Duplication | public static function DAVERAGE($database, $field, $criteria) |
155 | { |
||
156 | 1 | $field = self::fieldExtract($database, $field); |
|
157 | 1 | if ($field === null) { |
|
158 | return null; |
||
159 | } |
||
160 | |||
161 | // Return |
||
162 | 1 | return Statistical::AVERAGE( |
|
163 | 1 | self::getFilteredColumn($database, $field, $criteria) |
|
164 | ); |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * DCOUNT. |
||
169 | * |
||
170 | * Counts the cells that contain numbers in a column of a list or database that match conditions |
||
171 | * that you specify. |
||
172 | * |
||
173 | * Excel Function: |
||
174 | * DCOUNT(database,[field],criteria) |
||
175 | * |
||
176 | * Excel Function: |
||
177 | * DAVERAGE(database,field,criteria) |
||
178 | * |
||
179 | * @category Database Functions |
||
180 | * |
||
181 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
182 | * A database is a list of related data in which rows of related |
||
183 | * information are records, and columns of data are fields. The |
||
184 | * first row of the list contains labels for each column. |
||
185 | * @param int|string $field Indicates which column is used in the function. Enter the |
||
186 | * column label enclosed between double quotation marks, such as |
||
187 | * "Age" or "Yield," or a number (without quotation marks) that |
||
188 | * represents the position of the column within the list: 1 for |
||
189 | * the first column, 2 for the second column, and so on. |
||
190 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
191 | * You can use any range for the criteria argument, as long as it |
||
192 | * includes at least one column label and at least one cell below |
||
193 | * the column label in which you specify a condition for the |
||
194 | * column. |
||
195 | * |
||
196 | * @return int |
||
197 | * |
||
198 | * @TODO The field argument is optional. If field is omitted, DCOUNT counts all records in the |
||
199 | * database that match the criteria. |
||
200 | */ |
||
201 | 1 | View Code Duplication | public static function DCOUNT($database, $field, $criteria) |
202 | { |
||
203 | 1 | $field = self::fieldExtract($database, $field); |
|
204 | 1 | if ($field === null) { |
|
205 | return null; |
||
206 | } |
||
207 | |||
208 | // Return |
||
209 | 1 | return Statistical::COUNT( |
|
210 | 1 | self::getFilteredColumn($database, $field, $criteria) |
|
211 | ); |
||
212 | } |
||
213 | |||
214 | /** |
||
215 | * DCOUNTA. |
||
216 | * |
||
217 | * Counts the nonblank cells in a column of a list or database that match conditions that you specify. |
||
218 | * |
||
219 | * Excel Function: |
||
220 | * DCOUNTA(database,[field],criteria) |
||
221 | * |
||
222 | * @category Database Functions |
||
223 | * |
||
224 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
225 | * A database is a list of related data in which rows of related |
||
226 | * information are records, and columns of data are fields. The |
||
227 | * first row of the list contains labels for each column. |
||
228 | * @param int|string $field Indicates which column is used in the function. Enter the |
||
229 | * column label enclosed between double quotation marks, such as |
||
230 | * "Age" or "Yield," or a number (without quotation marks) that |
||
231 | * represents the position of the column within the list: 1 for |
||
232 | * the first column, 2 for the second column, and so on. |
||
233 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
234 | * You can use any range for the criteria argument, as long as it |
||
235 | * includes at least one column label and at least one cell below |
||
236 | * the column label in which you specify a condition for the |
||
237 | * column. |
||
238 | * |
||
239 | * @return int |
||
240 | * |
||
241 | * @TODO The field argument is optional. If field is omitted, DCOUNTA counts all records in the |
||
242 | * database that match the criteria. |
||
243 | */ |
||
244 | public static function DCOUNTA($database, $field, $criteria) |
||
264 | |||
265 | /** |
||
266 | * DGET. |
||
267 | * |
||
268 | * Extracts a single value from a column of a list or database that matches conditions that you |
||
269 | * specify. |
||
270 | * |
||
271 | * Excel Function: |
||
272 | * DGET(database,field,criteria) |
||
273 | * |
||
274 | * @category Database Functions |
||
275 | * |
||
276 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
277 | * A database is a list of related data in which rows of related |
||
278 | * information are records, and columns of data are fields. The |
||
279 | * first row of the list contains labels for each column. |
||
280 | * @param int|string $field Indicates which column is used in the function. Enter the |
||
281 | * column label enclosed between double quotation marks, such as |
||
282 | * "Age" or "Yield," or a number (without quotation marks) that |
||
283 | * represents the position of the column within the list: 1 for |
||
284 | * the first column, 2 for the second column, and so on. |
||
285 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
286 | * You can use any range for the criteria argument, as long as it |
||
287 | * includes at least one column label and at least one cell below |
||
288 | * the column label in which you specify a condition for the |
||
289 | * column. |
||
290 | * |
||
291 | * @return mixed |
||
292 | */ |
||
293 | 1 | public static function DGET($database, $field, $criteria) |
|
294 | { |
||
295 | 1 | $field = self::fieldExtract($database, $field); |
|
296 | 1 | if ($field === null) { |
|
297 | return null; |
||
298 | } |
||
299 | |||
300 | // Return |
||
301 | 1 | $colData = self::getFilteredColumn($database, $field, $criteria); |
|
302 | 1 | if (count($colData) > 1) { |
|
303 | return Functions::NAN(); |
||
304 | } |
||
305 | |||
306 | 1 | return $colData[0]; |
|
307 | } |
||
308 | |||
309 | /** |
||
310 | * DMAX. |
||
311 | * |
||
312 | * Returns the largest number in a column of a list or database that matches conditions you that |
||
313 | * specify. |
||
314 | * |
||
315 | * Excel Function: |
||
316 | * DMAX(database,field,criteria) |
||
317 | * |
||
318 | * @category Database Functions |
||
319 | * |
||
320 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
321 | * A database is a list of related data in which rows of related |
||
322 | * information are records, and columns of data are fields. The |
||
323 | * first row of the list contains labels for each column. |
||
324 | * @param int|string $field Indicates which column is used in the function. Enter the |
||
325 | * column label enclosed between double quotation marks, such as |
||
326 | * "Age" or "Yield," or a number (without quotation marks) that |
||
327 | * represents the position of the column within the list: 1 for |
||
328 | * the first column, 2 for the second column, and so on. |
||
329 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
330 | * You can use any range for the criteria argument, as long as it |
||
331 | * includes at least one column label and at least one cell below |
||
332 | * the column label in which you specify a condition for the |
||
333 | * column. |
||
334 | * |
||
335 | * @return float |
||
336 | */ |
||
337 | 1 | View Code Duplication | public static function DMAX($database, $field, $criteria) |
338 | { |
||
339 | 1 | $field = self::fieldExtract($database, $field); |
|
340 | 1 | if ($field === null) { |
|
341 | return null; |
||
342 | } |
||
343 | |||
344 | // Return |
||
345 | 1 | return Statistical::MAX( |
|
346 | 1 | self::getFilteredColumn($database, $field, $criteria) |
|
347 | ); |
||
348 | } |
||
349 | |||
350 | /** |
||
351 | * DMIN. |
||
352 | * |
||
353 | * Returns the smallest number in a column of a list or database that matches conditions you that |
||
354 | * specify. |
||
355 | * |
||
356 | * Excel Function: |
||
357 | * DMIN(database,field,criteria) |
||
358 | * |
||
359 | * @category Database Functions |
||
360 | * |
||
361 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
362 | * A database is a list of related data in which rows of related |
||
363 | * information are records, and columns of data are fields. The |
||
364 | * first row of the list contains labels for each column. |
||
365 | * @param int|string $field Indicates which column is used in the function. Enter the |
||
366 | * column label enclosed between double quotation marks, such as |
||
367 | * "Age" or "Yield," or a number (without quotation marks) that |
||
368 | * represents the position of the column within the list: 1 for |
||
369 | * the first column, 2 for the second column, and so on. |
||
370 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
371 | * You can use any range for the criteria argument, as long as it |
||
372 | * includes at least one column label and at least one cell below |
||
373 | * the column label in which you specify a condition for the |
||
374 | * column. |
||
375 | * |
||
376 | * @return float |
||
377 | */ |
||
378 | 1 | View Code Duplication | public static function DMIN($database, $field, $criteria) |
379 | { |
||
380 | 1 | $field = self::fieldExtract($database, $field); |
|
381 | 1 | if ($field === null) { |
|
382 | return null; |
||
383 | } |
||
384 | |||
385 | // Return |
||
386 | 1 | return Statistical::MIN( |
|
387 | 1 | self::getFilteredColumn($database, $field, $criteria) |
|
388 | ); |
||
389 | } |
||
390 | |||
391 | /** |
||
392 | * DPRODUCT. |
||
393 | * |
||
394 | * Multiplies the values in a column of a list or database that match conditions that you specify. |
||
395 | * |
||
396 | * Excel Function: |
||
397 | * DPRODUCT(database,field,criteria) |
||
398 | * |
||
399 | * @category Database Functions |
||
400 | * |
||
401 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
402 | * A database is a list of related data in which rows of related |
||
403 | * information are records, and columns of data are fields. The |
||
404 | * first row of the list contains labels for each column. |
||
405 | * @param int|string $field Indicates which column is used in the function. Enter the |
||
406 | * column label enclosed between double quotation marks, such as |
||
407 | * "Age" or "Yield," or a number (without quotation marks) that |
||
408 | * represents the position of the column within the list: 1 for |
||
409 | * the first column, 2 for the second column, and so on. |
||
410 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
411 | * You can use any range for the criteria argument, as long as it |
||
412 | * includes at least one column label and at least one cell below |
||
413 | * the column label in which you specify a condition for the |
||
414 | * column. |
||
415 | * |
||
416 | * @return float |
||
417 | */ |
||
418 | 1 | View Code Duplication | public static function DPRODUCT($database, $field, $criteria) |
419 | { |
||
420 | 1 | $field = self::fieldExtract($database, $field); |
|
421 | 1 | if ($field === null) { |
|
422 | return null; |
||
423 | } |
||
424 | |||
425 | // Return |
||
426 | 1 | return MathTrig::PRODUCT( |
|
427 | 1 | self::getFilteredColumn($database, $field, $criteria) |
|
428 | ); |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * DSTDEV. |
||
433 | * |
||
434 | * Estimates the standard deviation of a population based on a sample by using the numbers in a |
||
435 | * column of a list or database that match conditions that you specify. |
||
436 | * |
||
437 | * Excel Function: |
||
438 | * DSTDEV(database,field,criteria) |
||
439 | * |
||
440 | * @category Database Functions |
||
441 | * |
||
442 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
443 | * A database is a list of related data in which rows of related |
||
444 | * information are records, and columns of data are fields. The |
||
445 | * first row of the list contains labels for each column. |
||
446 | * @param int|string $field Indicates which column is used in the function. Enter the |
||
447 | * column label enclosed between double quotation marks, such as |
||
448 | * "Age" or "Yield," or a number (without quotation marks) that |
||
449 | * represents the position of the column within the list: 1 for |
||
450 | * the first column, 2 for the second column, and so on. |
||
451 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
452 | * You can use any range for the criteria argument, as long as it |
||
453 | * includes at least one column label and at least one cell below |
||
454 | * the column label in which you specify a condition for the |
||
455 | * column. |
||
456 | * |
||
457 | * @return float |
||
458 | */ |
||
459 | 1 | View Code Duplication | public static function DSTDEV($database, $field, $criteria) |
460 | { |
||
461 | 1 | $field = self::fieldExtract($database, $field); |
|
462 | 1 | if ($field === null) { |
|
463 | return null; |
||
464 | } |
||
465 | |||
466 | // Return |
||
467 | 1 | return Statistical::STDEV( |
|
468 | 1 | self::getFilteredColumn($database, $field, $criteria) |
|
469 | ); |
||
470 | } |
||
471 | |||
472 | /** |
||
473 | * DSTDEVP. |
||
474 | * |
||
475 | * Calculates the standard deviation of a population based on the entire population by using the |
||
476 | * numbers in a column of a list or database that match conditions that you specify. |
||
477 | * |
||
478 | * Excel Function: |
||
479 | * DSTDEVP(database,field,criteria) |
||
480 | * |
||
481 | * @category Database Functions |
||
482 | * |
||
483 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
484 | * A database is a list of related data in which rows of related |
||
485 | * information are records, and columns of data are fields. The |
||
486 | * first row of the list contains labels for each column. |
||
487 | * @param int|string $field Indicates which column is used in the function. Enter the |
||
488 | * column label enclosed between double quotation marks, such as |
||
489 | * "Age" or "Yield," or a number (without quotation marks) that |
||
490 | * represents the position of the column within the list: 1 for |
||
491 | * the first column, 2 for the second column, and so on. |
||
492 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
493 | * You can use any range for the criteria argument, as long as it |
||
494 | * includes at least one column label and at least one cell below |
||
495 | * the column label in which you specify a condition for the |
||
496 | * column. |
||
497 | * |
||
498 | * @return float |
||
499 | */ |
||
500 | 1 | View Code Duplication | public static function DSTDEVP($database, $field, $criteria) |
501 | { |
||
502 | 1 | $field = self::fieldExtract($database, $field); |
|
503 | 1 | if ($field === null) { |
|
504 | return null; |
||
505 | } |
||
506 | |||
507 | // Return |
||
508 | 1 | return Statistical::STDEVP( |
|
509 | 1 | self::getFilteredColumn($database, $field, $criteria) |
|
510 | ); |
||
511 | } |
||
512 | |||
513 | /** |
||
514 | * DSUM. |
||
515 | * |
||
516 | * Adds the numbers in a column of a list or database that match conditions that you specify. |
||
517 | * |
||
518 | * Excel Function: |
||
519 | * DSUM(database,field,criteria) |
||
520 | * |
||
521 | * @category Database Functions |
||
522 | * |
||
523 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
524 | * A database is a list of related data in which rows of related |
||
525 | * information are records, and columns of data are fields. The |
||
526 | * first row of the list contains labels for each column. |
||
527 | * @param int|string $field Indicates which column is used in the function. Enter the |
||
528 | * column label enclosed between double quotation marks, such as |
||
529 | * "Age" or "Yield," or a number (without quotation marks) that |
||
530 | * represents the position of the column within the list: 1 for |
||
531 | * the first column, 2 for the second column, and so on. |
||
532 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
533 | * You can use any range for the criteria argument, as long as it |
||
534 | * includes at least one column label and at least one cell below |
||
535 | * the column label in which you specify a condition for the |
||
536 | * column. |
||
537 | * |
||
538 | * @return float |
||
539 | */ |
||
540 | View Code Duplication | public static function DSUM($database, $field, $criteria) |
|
552 | |||
553 | /** |
||
554 | * DVAR. |
||
555 | * |
||
556 | * Estimates the variance of a population based on a sample by using the numbers in a column |
||
557 | * of a list or database that match conditions that you specify. |
||
558 | * |
||
559 | * Excel Function: |
||
560 | * DVAR(database,field,criteria) |
||
561 | * |
||
562 | * @category Database Functions |
||
563 | * |
||
564 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
565 | * A database is a list of related data in which rows of related |
||
566 | * information are records, and columns of data are fields. The |
||
567 | * first row of the list contains labels for each column. |
||
568 | * @param int|string $field Indicates which column is used in the function. Enter the |
||
569 | * column label enclosed between double quotation marks, such as |
||
570 | * "Age" or "Yield," or a number (without quotation marks) that |
||
571 | * represents the position of the column within the list: 1 for |
||
572 | * the first column, 2 for the second column, and so on. |
||
573 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
574 | * You can use any range for the criteria argument, as long as it |
||
575 | * includes at least one column label and at least one cell below |
||
576 | * the column label in which you specify a condition for the |
||
577 | * column. |
||
578 | * |
||
579 | * @return float |
||
580 | */ |
||
581 | 1 | View Code Duplication | public static function DVAR($database, $field, $criteria) |
582 | { |
||
583 | 1 | $field = self::fieldExtract($database, $field); |
|
584 | 1 | if ($field === null) { |
|
585 | return null; |
||
586 | } |
||
587 | |||
588 | // Return |
||
589 | 1 | return Statistical::VARFunc( |
|
590 | 1 | self::getFilteredColumn($database, $field, $criteria) |
|
591 | ); |
||
592 | } |
||
593 | |||
594 | /** |
||
595 | * DVARP. |
||
596 | * |
||
597 | * Calculates the variance of a population based on the entire population by using the numbers |
||
598 | * in a column of a list or database that match conditions that you specify. |
||
599 | * |
||
600 | * Excel Function: |
||
601 | * DVARP(database,field,criteria) |
||
602 | * |
||
603 | * @category Database Functions |
||
604 | * |
||
605 | * @param mixed[] $database The range of cells that makes up the list or database. |
||
606 | * A database is a list of related data in which rows of related |
||
607 | * information are records, and columns of data are fields. The |
||
608 | * first row of the list contains labels for each column. |
||
609 | * @param int|string $field Indicates which column is used in the function. Enter the |
||
610 | * column label enclosed between double quotation marks, such as |
||
611 | * "Age" or "Yield," or a number (without quotation marks) that |
||
612 | * represents the position of the column within the list: 1 for |
||
613 | * the first column, 2 for the second column, and so on. |
||
614 | * @param mixed[] $criteria The range of cells that contains the conditions you specify. |
||
615 | * You can use any range for the criteria argument, as long as it |
||
616 | * includes at least one column label and at least one cell below |
||
617 | * the column label in which you specify a condition for the |
||
618 | * column. |
||
619 | * |
||
620 | * @return float |
||
621 | */ |
||
622 | 1 | View Code Duplication | public static function DVARP($database, $field, $criteria) |
634 | } |
||
635 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.