Issues (109)

src/EoC/Opt/ChangesFeedOpts.php (8 issues)

1
<?php
2
3
/**
4
 * @file ChangesFeedOpts.php
5
 * @brief This file contains the ChangesFeedOpts class.
6
 * @details
7
 * @author Filippo F. Fadda
8
 */
9
10
11
namespace EoC\Opt;
12
13
14
/**
15
 * @brief To change the feed type, set a different timeout, etc, you can create a ChangesFeedOpts instance and pass
16
 * it as parameter to the Couch::getDbChanges() method.
17
 * @nosubgrouping
18
 * @see http://docs.couchdb.org/en/latest/changes.html#changes
19
 */
20
class ChangesFeedOpts extends AbstractOpts {
21
22
  //! Default period after which an empty line is sent during a longpoll or continuous feed.
23
  const DEFAULT_HEARTBEAT = 60000;
24
25
  //! Period in milliseconds to wait for a change before the response is sent, even if there are no results.
26
  const DEFAULT_TIMEOUT = 60000;
27
28
  /** @name Feed Types */
29
  //!@{
0 ignored issues
show
Unused Code Comprehensibility introduced by
100% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
30
31
  //! Normal mode.
32
  const NORMAL_TYPE = "normal";
33
34
  /**  
35
   * @brief Long polling mode.
36
   * @details The longpoll feed (probably most useful used from a browser) is a more efficient form of polling that waits
37
   * for a change to occur before the response is sent. longpoll avoids the need to frequently poll CouchDB to discover
38
   * nothing has changed.
39
   */
40
  const LONGPOLL_TYPE = "longpoll";
41
42
  /**
43
   * @brief Continuous (non-polling) mode.
44
   * @details Polling the CouchDB server is not a good thing to do. Setting up new HTTP connections just to tell the
45
   * client that nothing happened puts unnecessary strain on CouchDB.
46
   * A continuous feed stays open and connected to the database until explicitly closed and changes are sent to the
47
   * client as they happen, i.e. in near real-time.
48
   */
49
  const CONTINUOUS_TYPE = "continuous";
50
51
  /*
52
   * @brief The eventsource feed provides push notifications that can be consumed in the form of DOM events in the browser.
53
   * @see http://www.w3.org/TR/eventsource/
54
   */
55
  const EVENTSOURCE_TYPE = "eventsource";
56
57
  //!@}
0 ignored issues
show
Unused Code Comprehensibility introduced by
100% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
58
59
  /** @name Feed Styles */
60
  //!@{
0 ignored issues
show
Unused Code Comprehensibility introduced by
100% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
61
  const MAIN_ONLY_STYLE = "main_only";
62
  const ALL_DOCS_STYLE = "all_docs";
63
  //!@}
0 ignored issues
show
Unused Code Comprehensibility introduced by
100% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
64
65
66
  private static $supportedStyles = array( // Cannot use [] syntax otherwise Doxygen generates a warning.
67
    self::MAIN_ONLY_STYLE => NULL,
68
    self::ALL_DOCS_STYLE => NULL
69
  );
70
71
  private static $supportedTypes = array( // Cannot use [] syntax otherwise Doxygen generates a warning.
72
    self::NORMAL_TYPE => NULL,
73
    self::LONGPOLL_TYPE => NULL,
74
    self::CONTINUOUS_TYPE => NULL,
75
    self::EVENTSOURCE_TYPE => NULL
76
  );
77
78
79
  /**
80
   * @brief Starts the results from the change immediately after the given sequence number.
81
   * @param integer $since Sequence number to start results. Allowed values: positive integers | 'now'.
82
   */
83
  public function setSince($since = 0) {
84
    if (($since == "now") || (is_int($since) and ($since >= 0)))
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
85
      $this->options["since"] = $since;
86
    else
87
      throw new \InvalidArgumentException("\$since must be a non-negative integer or can be 'now'.");
88
89
    return $this;
90
  }
91
92
93
  /**
94
   * @brief Limits number of result rows to the specified value.
95
   * @param integer $limit Maximum number of rows to return. Must be a positive integer.
96
   */
97
  public function setLimit($limit) {
98
    if (is_int($limit) && ($limit > 0))
99
      $this->options["limit"] = $limit;
100
    else
101
      throw new \InvalidArgumentException("\$value must be a positive integer.");
102
103
    return $this;
104
  }
105
106
107
  /**
108
   * @brief Reverses order of results to return the change results in descending sequence order (most recent change first).
109
   */
110
  public function reverseOrderOfResults() {
111
    $this->options["descending"] = "true";
112
  }
113
114
115
  /**
116
   * @brief Sets the type of feed.
117
   * @param string $type Type of feed.
118
   */
119
  public function setFeedType($type) {
120
    if (array_key_exists($type, self::$supportedTypes))
121
      $this->options["feed"] = $type;
122
    else
123
      throw new \InvalidArgumentException("Invalid feed type.");
124
  }
125
126
127
  /**
128
   * @brief Specifies how many revisions are returned in the changes array. The default, `main_only`, will only
129
   * return the winning revision; `all_docs` will return all the conflicting revisions.
130
   * @param bool $style The feed style.
131
   */
132
  public function setStyle($style) {
133
    if (array_key_exists($style, self::$supportedStyles))
134
      $this->options["style"] = $style;
135
    else
136
      throw new \InvalidArgumentException("Invalid feed style.");
137
  }
138
139
140
  /**
141
   * @brief Period in milliseconds after which an empty line is sent in the results. Overrides any timeout to keep the
142
   * feed alive indefinitely.
143
   * @param integer $heartbeat Period in milliseconds after which an empty line is sent in the results.
144
   * @warning Only applicable for `longpoll` or `continuous` feeds.
145
   */
146
  public function setHeartbeat($heartbeat = self::DEFAULT_HEARTBEAT) {
147
    $feed = $this->options['feed'];
148
149
    if (($feed == self::CONTINUOUS_TYPE) || ($feed == self::LONGPOLL_TYPE))
150
      if (is_int($heartbeat) && ($heartbeat >= 0))
151
        $this->options["heartbeat"] = $heartbeat;
152
      else
153
        throw new \InvalidArgumentException("\$heartbeat must be a non-negative integer.");
154
  }
155
156
157
  /**
158
   * @brief Maximum period in milliseconds to wait for a change before the response is sent, even if there are no results.
159
   * @details Note that 60000 is also the default maximum timeout to prevent undetected dead connections.
160
   * @param integer $timeout Maximum period to wait before the response is sent. Must be a positive integer.
161
   * @warning Only applicable for `longpoll` or `continuous` feeds.
162
   */
163
  public function setTimeout($timeout = self::DEFAULT_TIMEOUT) {
164
    $feed = $this->options['feed'];
165
166
    if (($feed == self::CONTINUOUS_TYPE) || ($feed == self::LONGPOLL_TYPE))
167
      if (is_int($timeout) && ($timeout > 0))
168
        $this->options["timeout"] = $timeout;
169
      else
170
        throw new \InvalidArgumentException("\$timeout must be a positive integer.");
171
  }
172
173
174
  /**
175
   * @brief Automatically fetches and includes full documents.
176
   */
177
  public function includeDocs() {
178
    $this->options["include_docs"] = "true";
179
  }
180
181
182
  /**
183
   * @brief Sets a filter function.
184
   * @param string $designDocName The design document's name.
185
   * @param string $filterName Filter function from a design document to get updates.
186
   * @todo Implement the setFilter() method.
187
   */
188
  public function setFilter($designDocName, $filterName) {
0 ignored issues
show
The parameter $designDocName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

188
  public function setFilter(/** @scrutinizer ignore-unused */ $designDocName, $filterName) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $filterName is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

188
  public function setFilter($designDocName, /** @scrutinizer ignore-unused */ $filterName) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
189
    //if (is_string($designDocName) && !empty($designDocName)) &&
0 ignored issues
show
Unused Code Comprehensibility introduced by
73% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
190
  }
191
192
}