ReduceCmd::getName()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @file ReduceCmd.php
5
 * @brief This file contains the ReduceCmd class.
6
 * @details
7
 * @author Filippo F. Fadda
8
 */
9
10
11
namespace EoC\Command;
12
13
14
/**
15
 * @brief The map command (MapDocCmd) generates a set of key/value pairs, which can then optionally be reduced to single
16
 * value or to a group of values by the reduce command (ReduceCmd). So that's the purpose of this class.
17
 * @details The reduce step primarily involves working with keys and values, not document IDs. Either a single computed
18
 * reduction of all values will be produced, or reductions of values grouped by keys, will ultimately be produced.
19
 * Grouping is controlled by parameters passed to your view, not by the reduce function itself.\n\n
20
 * The argument provided by CouchDB has the following structure:
21
 @code
22
   Array
23
   (
24
       [0] => Array
25
       (
26
           [0] => function($keys, $values, $rereduce) {
27
                    if ($rereduce)
28
                      return array_sum($values);
29
                    else
30
                      return sizeof($values);
31
                  };
32
       )
33
       [1] => Array
34
       (
35
           [0] => Array
36
           (
37
               [0] => Array
38
               (
39
                   [0] => 48360
40
                   [1] => 48360
41
               )
42
               [1] =>
43
           )
44
           [1] => Array
45
           (
46
               [0] => Array
47
               (
48
                   [0] => 48365
49
                   [1] => 48365
50
               )
51
               [1] =>
52
           )
53
       )
54
   )
55
 @endcode
56
 */
57
final class ReduceCmd extends AbstractCmd {
58
  use CmdTrait;
59
60
61
  public static function getName() {
62
    return "reduce";
63
  }
64
65
66
  public function execute() {
67
    // Extracts functions and pairs (keys, values) from the arguments array.
68
    @list($funcs, $pairs) = $this->args;
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
69
70
    $keys = [];
71
    $values = [];
72
73
    // Extracts keys and values.
74
    foreach ($pairs as $pair) {
75
      $keys[] = $pair[0];
76
      $values[] = $pair[1];
77
    }
78
79
    $this->server->reduce($funcs, $keys, $values, FALSE);
80
  }
81
82
}