Completed
Push — develop ( 685e29...09d456 )
by Adrien
14:14
created

Database::fieldExtract()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 14
ccs 0
cts 11
cp 0
crap 12
rs 9.4285
1
<?php
2
3
namespace PhpSpreadsheet\Calculation;
4
5
/**
6
 * Copyright (c) 2006 - 2016 PhpSpreadsheet
7
 *
8
 * This library is free software; you can redistribute it and/or
9
 * modify it under the terms of the GNU Lesser General Public
10
 * License as published by the Free Software Foundation; either
11
 * version 2.1 of the License, or (at your option) any later version.
12
 *
13
 * This library is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
 * Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Lesser General Public
19
 * License along with this library; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
 *
22
 * @category    PhpSpreadsheet
23
 * @copyright    Copyright (c) 2006 - 2016 PhpSpreadsheet (https://github.com/PHPOffice/PhpSpreadsheet)
24
 * @license        http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt    LGPL
25
 * @version        ##VERSION##, ##DATE##
26
 */
27
class Database
28
{
29
    /**
30
     * fieldExtract
31
     *
32
     * Extracts the column ID to use for the data field.
33
     *
34
     * @param    mixed[]        $database        The range of cells that makes up the list or database.
35
     *                                        A database is a list of related data in which rows of related
36
     *                                        information are records, and columns of data are fields. The
37
     *                                        first row of the list contains labels for each column.
38
     * @param    mixed        $field            Indicates which column is used in the function. Enter the
39
     *                                        column label enclosed between double quotation marks, such as
40
     *                                        "Age" or "Yield," or a number (without quotation marks) that
41
     *                                        represents the position of the column within the list: 1 for
42
     *                                        the first column, 2 for the second column, and so on.
43
     * @return    string|null
44
     */
45
    private static function fieldExtract($database, $field)
46
    {
47
        $field = strtoupper(Functions::flattenSingleValue($field));
48
        $fieldNames = array_map('strtoupper', array_shift($database));
49
50
        if (is_numeric($field)) {
51
            $keys = array_keys($fieldNames);
52
53
            return $keys[$field - 1];
54
        }
55
        $key = array_search($field, $fieldNames);
56
57
        return ($key) ? $key : null;
58
    }
59
60
    /**
61
     * filter
62
     *
63
     * Parses the selection criteria, extracts the database rows that match those criteria, and
64
     * returns that subset of rows.
65
     *
66
     * @param    mixed[]        $database        The range of cells that makes up the list or database.
67
     *                                        A database is a list of related data in which rows of related
68
     *                                        information are records, and columns of data are fields. The
69
     *                                        first row of the list contains labels for each column.
70
     * @param    mixed[]        $criteria        The range of cells that contains the conditions you specify.
71
     *                                        You can use any range for the criteria argument, as long as it
72
     *                                        includes at least one column label and at least one cell below
73
     *                                        the column label in which you specify a condition for the
74
     *                                        column.
75
     * @return    array of mixed
76
     */
77
    private static function filter($database, $criteria)
78
    {
79
        $fieldNames = array_shift($database);
80
        $criteriaNames = array_shift($criteria);
81
82
        //    Convert the criteria into a set of AND/OR conditions with [:placeholders]
83
        $testConditions = $testValues = [];
0 ignored issues
show
Unused Code introduced by
$testValues 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...
84
        $testConditionsCount = 0;
85
        foreach ($criteriaNames as $key => $criteriaName) {
86
            $testCondition = [];
87
            $testConditionCount = 0;
88
            foreach ($criteria as $row => $criterion) {
89
                if ($criterion[$key] > '') {
90
                    $testCondition[] = '[:' . $criteriaName . ']' . Functions::ifCondition($criterion[$key]);
91
                    ++$testConditionCount;
92
                }
93
            }
94
            if ($testConditionCount > 1) {
95
                $testConditions[] = 'OR(' . implode(',', $testCondition) . ')';
96
                ++$testConditionsCount;
97
            } elseif ($testConditionCount == 1) {
98
                $testConditions[] = $testCondition[0];
99
                ++$testConditionsCount;
100
            }
101
        }
102
103
        if ($testConditionsCount > 1) {
104
            $testConditionSet = 'AND(' . implode(',', $testConditions) . ')';
105
        } elseif ($testConditionsCount == 1) {
106
            $testConditionSet = $testConditions[0];
107
        }
108
109
        //    Loop through each row of the database
110
        foreach ($database as $dataRow => $dataValues) {
111
            //    Substitute actual values from the database row for our [:placeholders]
112
            $testConditionList = $testConditionSet;
0 ignored issues
show
Bug introduced by
The variable $testConditionSet does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
113
            foreach ($criteriaNames as $key => $criteriaName) {
114
                $k = array_search($criteriaName, $fieldNames);
115
                if (isset($dataValues[$k])) {
116
                    $dataValue = $dataValues[$k];
117
                    $dataValue = (is_string($dataValue)) ? \PhpSpreadsheet\Calculation::wrapResult(strtoupper($dataValue)) : $dataValue;
118
                    $testConditionList = str_replace('[:' . $criteriaName . ']', $dataValue, $testConditionList);
119
                }
120
            }
121
            //    evaluate the criteria against the row data
122
            $result = \PhpSpreadsheet\Calculation::getInstance()->_calculateFormulaValue('=' . $testConditionList);
123
            //    If the row failed to meet the criteria, remove it from the database
124
            if (!$result) {
125
                unset($database[$dataRow]);
126
            }
127
        }
128
129
        return $database;
130
    }
131
132
    private static function getFilteredColumn($database, $field, $criteria)
133
    {
134
        //    reduce the database to a set of rows that match all the criteria
135
        $database = self::filter($database, $criteria);
136
        //    extract an array of values for the requested column
137
        $colData = [];
138
        foreach ($database as $row) {
139
            $colData[] = $row[$field];
140
        }
141
142
        return $colData;
143
    }
144
145
    /**
146
     * DAVERAGE
147
     *
148
     * Averages the values in a column of a list or database that match conditions you specify.
149
     *
150
     * Excel Function:
151
     *        DAVERAGE(database,field,criteria)
152
     *
153
     * @category Database Functions
154
     * @param    mixed[]            $database    The range of cells that makes up the list or database.
155
     *                                        A database is a list of related data in which rows of related
156
     *                                        information are records, and columns of data are fields. The
157
     *                                        first row of the list contains labels for each column.
158
     * @param    string|int    $field        Indicates which column is used in the function. Enter the
159
     *                                        column label enclosed between double quotation marks, such as
160
     *                                        "Age" or "Yield," or a number (without quotation marks) that
161
     *                                        represents the position of the column within the list: 1 for
162
     *                                        the first column, 2 for the second column, and so on.
163
     * @param    mixed[]            $criteria    The range of cells that contains the conditions you specify.
164
     *                                        You can use any range for the criteria argument, as long as it
165
     *                                        includes at least one column label and at least one cell below
166
     *                                        the column label in which you specify a condition for the
167
     *                                        column.
168
     * @return    float
169
     */
170
    public static function DAVERAGE($database, $field, $criteria)
171
    {
172
        $field = self::fieldExtract($database, $field);
173
        if (is_null($field)) {
174
            return null;
175
        }
176
177
        // Return
178
        return Statistical::AVERAGE(
179
            self::getFilteredColumn($database, $field, $criteria)
180
        );
181
    }
182
183
    /**
184
     * DCOUNT
185
     *
186
     * Counts the cells that contain numbers in a column of a list or database that match conditions
187
     * that you specify.
188
     *
189
     * Excel Function:
190
     *        DCOUNT(database,[field],criteria)
191
     *
192
     * Excel Function:
193
     *        DAVERAGE(database,field,criteria)
194
     *
195
     * @category Database Functions
196
     * @param    mixed[]            $database    The range of cells that makes up the list or database.
197
     *                                        A database is a list of related data in which rows of related
198
     *                                        information are records, and columns of data are fields. The
199
     *                                        first row of the list contains labels for each column.
200
     * @param    string|int    $field        Indicates which column is used in the function. Enter the
201
     *                                        column label enclosed between double quotation marks, such as
202
     *                                        "Age" or "Yield," or a number (without quotation marks) that
203
     *                                        represents the position of the column within the list: 1 for
204
     *                                        the first column, 2 for the second column, and so on.
205
     * @param    mixed[]            $criteria    The range of cells that contains the conditions you specify.
206
     *                                        You can use any range for the criteria argument, as long as it
207
     *                                        includes at least one column label and at least one cell below
208
     *                                        the column label in which you specify a condition for the
209
     *                                        column.
210
     * @return    int
211
     *
212
     * @TODO    The field argument is optional. If field is omitted, DCOUNT counts all records in the
213
     *            database that match the criteria.
214
     */
215 View Code Duplication
    public static function DCOUNT($database, $field, $criteria)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
216
    {
217
        $field = self::fieldExtract($database, $field);
218
        if (is_null($field)) {
219
            return null;
220
        }
221
222
        // Return
223
        return Statistical::COUNT(
224
            self::getFilteredColumn($database, $field, $criteria)
225
        );
226
    }
227
228
    /**
229
     * DCOUNTA
230
     *
231
     * Counts the nonblank cells in a column of a list or database that match conditions that you specify.
232
     *
233
     * Excel Function:
234
     *        DCOUNTA(database,[field],criteria)
235
     *
236
     * @category Database Functions
237
     * @param    mixed[]            $database    The range of cells that makes up the list or database.
238
     *                                        A database is a list of related data in which rows of related
239
     *                                        information are records, and columns of data are fields. The
240
     *                                        first row of the list contains labels for each column.
241
     * @param    string|int    $field        Indicates which column is used in the function. Enter the
242
     *                                        column label enclosed between double quotation marks, such as
243
     *                                        "Age" or "Yield," or a number (without quotation marks) that
244
     *                                        represents the position of the column within the list: 1 for
245
     *                                        the first column, 2 for the second column, and so on.
246
     * @param    mixed[]            $criteria    The range of cells that contains the conditions you specify.
247
     *                                        You can use any range for the criteria argument, as long as it
248
     *                                        includes at least one column label and at least one cell below
249
     *                                        the column label in which you specify a condition for the
250
     *                                        column.
251
     * @return    int
252
     *
253
     * @TODO    The field argument is optional. If field is omitted, DCOUNTA counts all records in the
254
     *            database that match the criteria.
255
     */
256
    public static function DCOUNTA($database, $field, $criteria)
257
    {
258
        $field = self::fieldExtract($database, $field);
259
        if (is_null($field)) {
260
            return null;
261
        }
262
263
        //    reduce the database to a set of rows that match all the criteria
264
        $database = self::filter($database, $criteria);
265
        //    extract an array of values for the requested column
266
        $colData = [];
267
        foreach ($database as $row) {
268
            $colData[] = $row[$field];
269
        }
270
271
        // Return
272
        return Statistical::COUNTA(
273
            self::getFilteredColumn($database, $field, $criteria)
274
        );
275
    }
276
277
    /**
278
     * DGET
279
     *
280
     * Extracts a single value from a column of a list or database that matches conditions that you
281
     * specify.
282
     *
283
     * Excel Function:
284
     *        DGET(database,field,criteria)
285
     *
286
     * @category Database Functions
287
     * @param    mixed[]            $database    The range of cells that makes up the list or database.
288
     *                                        A database is a list of related data in which rows of related
289
     *                                        information are records, and columns of data are fields. The
290
     *                                        first row of the list contains labels for each column.
291
     * @param    string|int    $field        Indicates which column is used in the function. Enter the
292
     *                                        column label enclosed between double quotation marks, such as
293
     *                                        "Age" or "Yield," or a number (without quotation marks) that
294
     *                                        represents the position of the column within the list: 1 for
295
     *                                        the first column, 2 for the second column, and so on.
296
     * @param    mixed[]            $criteria    The range of cells that contains the conditions you specify.
297
     *                                        You can use any range for the criteria argument, as long as it
298
     *                                        includes at least one column label and at least one cell below
299
     *                                        the column label in which you specify a condition for the
300
     *                                        column.
301
     * @return    mixed
302
     */
303
    public static function DGET($database, $field, $criteria)
304
    {
305
        $field = self::fieldExtract($database, $field);
306
        if (is_null($field)) {
307
            return null;
308
        }
309
310
        // Return
311
        $colData = self::getFilteredColumn($database, $field, $criteria);
312
        if (count($colData) > 1) {
313
            return Functions::NAN();
314
        }
315
316
        return $colData[0];
317
    }
318
319
    /**
320
     * DMAX
321
     *
322
     * Returns the largest number in a column of a list or database that matches conditions you that
323
     * specify.
324
     *
325
     * Excel Function:
326
     *        DMAX(database,field,criteria)
327
     *
328
     * @category Database Functions
329
     * @param    mixed[]            $database    The range of cells that makes up the list or database.
330
     *                                        A database is a list of related data in which rows of related
331
     *                                        information are records, and columns of data are fields. The
332
     *                                        first row of the list contains labels for each column.
333
     * @param    string|int    $field        Indicates which column is used in the function. Enter the
334
     *                                        column label enclosed between double quotation marks, such as
335
     *                                        "Age" or "Yield," or a number (without quotation marks) that
336
     *                                        represents the position of the column within the list: 1 for
337
     *                                        the first column, 2 for the second column, and so on.
338
     * @param    mixed[]            $criteria    The range of cells that contains the conditions you specify.
339
     *                                        You can use any range for the criteria argument, as long as it
340
     *                                        includes at least one column label and at least one cell below
341
     *                                        the column label in which you specify a condition for the
342
     *                                        column.
343
     * @return    float
344
     */
345 View Code Duplication
    public static function DMAX($database, $field, $criteria)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
346
    {
347
        $field = self::fieldExtract($database, $field);
348
        if (is_null($field)) {
349
            return null;
350
        }
351
352
        // Return
353
        return Statistical::MAX(
354
            self::getFilteredColumn($database, $field, $criteria)
355
        );
356
    }
357
358
    /**
359
     * DMIN
360
     *
361
     * Returns the smallest number in a column of a list or database that matches conditions you that
362
     * specify.
363
     *
364
     * Excel Function:
365
     *        DMIN(database,field,criteria)
366
     *
367
     * @category Database Functions
368
     * @param    mixed[]            $database    The range of cells that makes up the list or database.
369
     *                                        A database is a list of related data in which rows of related
370
     *                                        information are records, and columns of data are fields. The
371
     *                                        first row of the list contains labels for each column.
372
     * @param    string|int    $field        Indicates which column is used in the function. Enter the
373
     *                                        column label enclosed between double quotation marks, such as
374
     *                                        "Age" or "Yield," or a number (without quotation marks) that
375
     *                                        represents the position of the column within the list: 1 for
376
     *                                        the first column, 2 for the second column, and so on.
377
     * @param    mixed[]            $criteria    The range of cells that contains the conditions you specify.
378
     *                                        You can use any range for the criteria argument, as long as it
379
     *                                        includes at least one column label and at least one cell below
380
     *                                        the column label in which you specify a condition for the
381
     *                                        column.
382
     * @return    float
383
     */
384 View Code Duplication
    public static function DMIN($database, $field, $criteria)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
385
    {
386
        $field = self::fieldExtract($database, $field);
387
        if (is_null($field)) {
388
            return null;
389
        }
390
391
        // Return
392
        return Statistical::MIN(
393
            self::getFilteredColumn($database, $field, $criteria)
394
        );
395
    }
396
397
    /**
398
     * DPRODUCT
399
     *
400
     * Multiplies the values in a column of a list or database that match conditions that you specify.
401
     *
402
     * Excel Function:
403
     *        DPRODUCT(database,field,criteria)
404
     *
405
     * @category Database Functions
406
     * @param    mixed[]            $database    The range of cells that makes up the list or database.
407
     *                                        A database is a list of related data in which rows of related
408
     *                                        information are records, and columns of data are fields. The
409
     *                                        first row of the list contains labels for each column.
410
     * @param    string|int    $field        Indicates which column is used in the function. Enter the
411
     *                                        column label enclosed between double quotation marks, such as
412
     *                                        "Age" or "Yield," or a number (without quotation marks) that
413
     *                                        represents the position of the column within the list: 1 for
414
     *                                        the first column, 2 for the second column, and so on.
415
     * @param    mixed[]            $criteria    The range of cells that contains the conditions you specify.
416
     *                                        You can use any range for the criteria argument, as long as it
417
     *                                        includes at least one column label and at least one cell below
418
     *                                        the column label in which you specify a condition for the
419
     *                                        column.
420
     * @return    float
421
     */
422 View Code Duplication
    public static function DPRODUCT($database, $field, $criteria)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
423
    {
424
        $field = self::fieldExtract($database, $field);
425
        if (is_null($field)) {
426
            return null;
427
        }
428
429
        // Return
430
        return MathTrig::PRODUCT(
431
            self::getFilteredColumn($database, $field, $criteria)
432
        );
433
    }
434
435
    /**
436
     * DSTDEV
437
     *
438
     * Estimates the standard deviation of a population based on a sample by using the numbers in a
439
     * column of a list or database that match conditions that you specify.
440
     *
441
     * Excel Function:
442
     *        DSTDEV(database,field,criteria)
443
     *
444
     * @category Database Functions
445
     * @param    mixed[]            $database    The range of cells that makes up the list or database.
446
     *                                        A database is a list of related data in which rows of related
447
     *                                        information are records, and columns of data are fields. The
448
     *                                        first row of the list contains labels for each column.
449
     * @param    string|int    $field        Indicates which column is used in the function. Enter the
450
     *                                        column label enclosed between double quotation marks, such as
451
     *                                        "Age" or "Yield," or a number (without quotation marks) that
452
     *                                        represents the position of the column within the list: 1 for
453
     *                                        the first column, 2 for the second column, and so on.
454
     * @param    mixed[]            $criteria    The range of cells that contains the conditions you specify.
455
     *                                        You can use any range for the criteria argument, as long as it
456
     *                                        includes at least one column label and at least one cell below
457
     *                                        the column label in which you specify a condition for the
458
     *                                        column.
459
     * @return    float
460
     */
461
    public static function DSTDEV($database, $field, $criteria)
462
    {
463
        $field = self::fieldExtract($database, $field);
464
        if (is_null($field)) {
465
            return null;
466
        }
467
468
        // Return
469
        return Statistical::STDEV(
470
            self::getFilteredColumn($database, $field, $criteria)
471
        );
472
    }
473
474
    /**
475
     * DSTDEVP
476
     *
477
     * Calculates the standard deviation of a population based on the entire population by using the
478
     * numbers in a column of a list or database that match conditions that you specify.
479
     *
480
     * Excel Function:
481
     *        DSTDEVP(database,field,criteria)
482
     *
483
     * @category Database Functions
484
     * @param    mixed[]            $database    The range of cells that makes up the list or database.
485
     *                                        A database is a list of related data in which rows of related
486
     *                                        information are records, and columns of data are fields. The
487
     *                                        first row of the list contains labels for each column.
488
     * @param    string|int    $field        Indicates which column is used in the function. Enter the
489
     *                                        column label enclosed between double quotation marks, such as
490
     *                                        "Age" or "Yield," or a number (without quotation marks) that
491
     *                                        represents the position of the column within the list: 1 for
492
     *                                        the first column, 2 for the second column, and so on.
493
     * @param    mixed[]            $criteria    The range of cells that contains the conditions you specify.
494
     *                                        You can use any range for the criteria argument, as long as it
495
     *                                        includes at least one column label and at least one cell below
496
     *                                        the column label in which you specify a condition for the
497
     *                                        column.
498
     * @return    float
499
     */
500
    public static function DSTDEVP($database, $field, $criteria)
501
    {
502
        $field = self::fieldExtract($database, $field);
503
        if (is_null($field)) {
504
            return null;
505
        }
506
507
        // Return
508
        return Statistical::STDEVP(
509
            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
     * @param    mixed[]            $database    The range of cells that makes up the list or database.
523
     *                                        A database is a list of related data in which rows of related
524
     *                                        information are records, and columns of data are fields. The
525
     *                                        first row of the list contains labels for each column.
526
     * @param    string|int    $field        Indicates which column is used in the function. Enter the
527
     *                                        column label enclosed between double quotation marks, such as
528
     *                                        "Age" or "Yield," or a number (without quotation marks) that
529
     *                                        represents the position of the column within the list: 1 for
530
     *                                        the first column, 2 for the second column, and so on.
531
     * @param    mixed[]            $criteria    The range of cells that contains the conditions you specify.
532
     *                                        You can use any range for the criteria argument, as long as it
533
     *                                        includes at least one column label and at least one cell below
534
     *                                        the column label in which you specify a condition for the
535
     *                                        column.
536
     * @return    float
537
     */
538 View Code Duplication
    public static function DSUM($database, $field, $criteria)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
539
    {
540
        $field = self::fieldExtract($database, $field);
541
        if (is_null($field)) {
542
            return null;
543
        }
544
545
        // Return
546
        return MathTrig::SUM(
547
            self::getFilteredColumn($database, $field, $criteria)
548
        );
549
    }
550
551
    /**
552
     * DVAR
553
     *
554
     * Estimates the variance of a population based on a sample by using the numbers in a column
555
     * of a list or database that match conditions that you specify.
556
     *
557
     * Excel Function:
558
     *        DVAR(database,field,criteria)
559
     *
560
     * @category Database Functions
561
     * @param    mixed[]            $database    The range of cells that makes up the list or database.
562
     *                                        A database is a list of related data in which rows of related
563
     *                                        information are records, and columns of data are fields. The
564
     *                                        first row of the list contains labels for each column.
565
     * @param    string|int    $field        Indicates which column is used in the function. Enter the
566
     *                                        column label enclosed between double quotation marks, such as
567
     *                                        "Age" or "Yield," or a number (without quotation marks) that
568
     *                                        represents the position of the column within the list: 1 for
569
     *                                        the first column, 2 for the second column, and so on.
570
     * @param    mixed[]            $criteria    The range of cells that contains the conditions you specify.
571
     *                                        You can use any range for the criteria argument, as long as it
572
     *                                        includes at least one column label and at least one cell below
573
     *                                        the column label in which you specify a condition for the
574
     *                                        column.
575
     * @return    float
576
     */
577
    public static function DVAR($database, $field, $criteria)
578
    {
579
        $field = self::fieldExtract($database, $field);
580
        if (is_null($field)) {
581
            return null;
582
        }
583
584
        // Return
585
        return Statistical::VARFunc(
586
            self::getFilteredColumn($database, $field, $criteria)
587
        );
588
    }
589
590
    /**
591
     * DVARP
592
     *
593
     * Calculates the variance of a population based on the entire population by using the numbers
594
     * in a column of a list or database that match conditions that you specify.
595
     *
596
     * Excel Function:
597
     *        DVARP(database,field,criteria)
598
     *
599
     * @category Database Functions
600
     * @param    mixed[]            $database    The range of cells that makes up the list or database.
601
     *                                        A database is a list of related data in which rows of related
602
     *                                        information are records, and columns of data are fields. The
603
     *                                        first row of the list contains labels for each column.
604
     * @param    string|int    $field        Indicates which column is used in the function. Enter the
605
     *                                        column label enclosed between double quotation marks, such as
606
     *                                        "Age" or "Yield," or a number (without quotation marks) that
607
     *                                        represents the position of the column within the list: 1 for
608
     *                                        the first column, 2 for the second column, and so on.
609
     * @param    mixed[]            $criteria    The range of cells that contains the conditions you specify.
610
     *                                        You can use any range for the criteria argument, as long as it
611
     *                                        includes at least one column label and at least one cell below
612
     *                                        the column label in which you specify a condition for the
613
     *                                        column.
614
     * @return    float
615
     */
616 View Code Duplication
    public static function DVARP($database, $field, $criteria)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
617
    {
618
        $field = self::fieldExtract($database, $field);
619
        if (is_null($field)) {
620
            return null;
621
        }
622
623
        // Return
624
        return Statistical::VARP(
625
            self::getFilteredColumn($database, $field, $criteria)
626
        );
627
    }
628
}
629