GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

MinMaxDaysConstraint::toString()   F
last analyzed

Complexity

Conditions 22
Paths 5760

Size

Total Lines 70

Duplication

Lines 11
Ratio 15.71 %

Importance

Changes 0
Metric Value
dl 11
loc 70
rs 0
c 0
b 0
f 0
cc 22
nc 5760
nop 0

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
/**
4
 * @file
5
 * Class MinMaxConstraint
6
 */
7
8
namespace Roomify\Bat\Constraint;
9
10
use Roomify\Bat\Calendar\CalendarResponse;
11
use Roomify\Bat\Constraint\Constraint;
12
13
/**
14
 * Checks that a request is at least a set number of days and does not exceed a
15
 * set number of days.
16
 *
17
 */
18
class MinMaxDaysConstraint extends Constraint {
19
20
  /**
21
   * @var int
22
   */
23
  protected $min_days = 0;
24
25
  /**
26
   * @var int
27
   */
28
  protected $max_days = 0;
29
30
  /**
31
   * @var int
32
   */
33
  protected $checkin_day = NULL;
34
35
  /**
36
   * @param $min_days
37
   * @param $max_days
38
   * @param $start_date
39
   * @param $end_date
40
   * @param $checkin_day
41
   */
42
  public function __construct($units, $min_days = 0, $max_days = 0, $start_date = NULL, $end_date = NULL, $checkin_day = NULL) {
43
    parent::__construct($units);
44
45
    $this->min_days = $min_days;
46
    $this->max_days = $max_days;
47
    $this->start_date = $start_date;
48
    $this->end_date = $end_date;
49
    $this->checkin_day = $checkin_day;
50
  }
51
52
  /**
53
   * {@inheritdoc}
54
   */
55
  public function applyConstraint(CalendarResponse &$calendar_response) {
56
    parent::applyConstraint($calendar_response);
57
58
    if ($this->start_date === NULL) {
59
      $this->start_date = new \DateTime('1970-01-01');
60
    }
61
    if ($this->end_date === NULL) {
62
      $this->end_date = new \DateTime('2999-12-31');
63
    }
64
65
    if ( (($calendar_response->getStartDate()->getTimestamp() >= $this->start_date->getTimestamp() &&
66
           $calendar_response->getStartDate()->getTimestamp() <= $this->end_date->getTimestamp()) ||
67
          ($calendar_response->getEndDate()->getTimestamp() >= $this->start_date->getTimestamp() &&
68
           $calendar_response->getEndDate()->getTimestamp() <= $this->end_date->getTimestamp()) ||
69
          ($calendar_response->getStartDate()->getTimestamp() <= $this->start_date->getTimestamp() &&
70
           $calendar_response->getEndDate()->getTimestamp() >= $this->end_date->getTimestamp())) &&
71
         ($this->checkin_day === NULL || $this->checkin_day == $calendar_response->getStartDate()->format('N')) ) {
72
73
      $units = $this->getUnits();
74
75
      $included_set = $calendar_response->getIncluded();
76
77
      foreach ($included_set as $unit_id => $set) {
78
        if (isset($units[$unit_id]) || empty($units)) {
79
          $start_date = $calendar_response->getStartDate();
80
          $end_date = $calendar_response->getEndDate();
81
82
          $temp_end_date = clone($end_date);
83
          $temp_end_date->add(new \DateInterval('PT1M'));
84
85
          $diff = $temp_end_date->diff($start_date)->days;
86
          if (is_numeric($this->min_days) && $diff < $this->min_days) {
87
            $calendar_response->removeFromMatched($included_set[$unit_id]['unit'], CalendarResponse::CONSTRAINT, $this);
88
89
            $this->affected_units[$unit_id] = $included_set[$unit_id]['unit'];
90
          } elseif (is_numeric($this->max_days) && $diff > $this->max_days) {
91
            $calendar_response->removeFromMatched($included_set[$unit_id]['unit'], CalendarResponse::CONSTRAINT, $this);
92
93
            $this->affected_units[$unit_id] = $included_set[$unit_id]['unit'];
94
          }
95
        }
96
      }
97
    }
98
  }
99
100
  /**
101
   * Generates a text describing an availability_constraint.
102
   *
103
   * @return string
104
   *   The formatted message.
105
   */
106
  public function toString() {
107
    $text = '';
108
109
    // Min/max stay length constraint variables.
110
    $minimum_stay = empty($this->min_days) ? '' : (($this->min_days == 1) ? $this->min_days . ' day' : $this->min_days . ' days');
111
    $maximum_stay = empty($this->max_days) ? '' : (($this->max_days == 1) ? $this->max_days . ' day' : $this->max_days . ' days');
112
113
    // Day of the week constraint variable.
114
    $day_of_the_week = $this->getWeekDay($this->checkin_day);
115
116
    $start_date = FALSE;
117
    $end_date = FALSE;
118
119
    // Date range constraint variables.
120 View Code Duplication
    if ($this->start_date !== NULL && $this->start_date != (new \DateTime('1970-01-01'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
121
      $start_date = $this->start_date->format('Y-m-d');
122
    }
123 View Code Duplication
    if ($this->end_date !== NULL && $this->end_date != (new \DateTime('2999-12-31'))) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
124
      $end_date = $this->end_date->format('Y-m-d');
125
    }
126
127
    // Next create replacement placeholders to be used in t() below.
128
    $args = array(
129
      '@minimum_stay' => $minimum_stay,
130
      '@maximum_stay' => $maximum_stay,
131
      '@day_of_the_week' => $day_of_the_week,
132
    );
133
134
    // Finally, build out the constraint text string adding components
135
    // as necessary.
136
137
    // Specify a date range constraint.
138 View Code Duplication
    if ($start_date && $end_date) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
139
      $text = 'From @start_date to @end_date';
140
      $args['@start_date'] = $start_date;
141
      $args['@end_date'] = $end_date;
142
    }
143
144
    // Specify the day of the week constraint.
145
    if ($day_of_the_week) {
146
      if ($start_date && $end_date) {
147
        $text = 'From @start_date to @end_date, if booking starts on @day_of_the_week';
148
      } else {
149
        $text = 'If booking starts on @day_of_the_week';
150
      }
151
    }
152
153
    // Specify the min/max stay length constraint.
154
    if ($minimum_stay || $maximum_stay) {
155
      if (empty($text)) {
156
        $text = 'The stay ';
157
      } else {
158
        $text .= ' the stay ';
159
      }
160
    }
161
    if ($minimum_stay && $maximum_stay) {
162
      // Special case when min stay and max stay are the same.
163
      if ($minimum_stay == $maximum_stay) {
164
        $text .= 'must be for @minimum_stay';
165
      } else {
166
        $text .= 'must be at least @minimum_stay and at most @maximum_stay';
167
      }
168
    } elseif ($minimum_stay) {
169
      $text .= 'must be for at least @minimum_stay';
170
    } elseif ($maximum_stay) {
171
      $text .= 'cannot be more than @maximum_stay';
172
    }
173
174
    return array('text' => $text, 'args' => $args);
175
  }
176
177
  /**
178
   * @param $day
179
   * @return string
180
   */
181 View Code Duplication
  private function getWeekDay($day) {
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...
182
    $weekdays = array(
183
      1 => 'Monday',
184
      2 => 'Tuesday',
185
      3 => 'Wednesday',
186
      4 => 'Thursday',
187
      5 => 'Friday',
188
      6 => 'Saturday',
189
      7 => 'Sunday',
190
    );
191
192
    return isset($weekdays[$day]) ? $weekdays[$day] : '';
193
  }
194
195
  /**
196
   * @return int
197
   */
198
  public function getMinDays() {
199
    return $this->min_days;
200
  }
201
202
  /**
203
   * @param $min_days
204
   */
205
  public function setMinDays($min_days) {
206
    $this->min_days = $min_days;
207
  }
208
209
  /**
210
   * @return int
211
   */
212
  public function getMaxDays() {
213
    return $this->max_days;
214
  }
215
216
  /**
217
   * @param $max_days
218
   */
219
  public function setMaxDays($max_days) {
220
    $this->max_days = $max_days;
221
  }
222
223
  /**
224
   * @return int
225
   */
226
  public function getCheckinDay() {
227
    return $this->checkin_day;
228
  }
229
230
  /**
231
   * @param $checkin_day
232
   */
233
  public function setCheckinDay($checkin_day) {
234
    $this->checkin_day = $checkin_day;
235
  }
236
237
}
238