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.
Completed
Push — master ( 24f210...86539e )
by
unknown
15:03
created

MinMaxDaysConstraint   A

Complexity

Total Complexity 35

Size/Duplication

Total Lines 170
Duplicated Lines 7.65 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 35
c 4
b 1
f 0
lcom 1
cbo 2
dl 13
loc 170
rs 9

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
C applyConstraint() 0 38 14
F toString() 0 68 18
A getWeekDay() 13 13 2

How to fix   Duplicated Code   

Duplicated Code

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:

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');
0 ignored issues
show
Documentation Bug introduced by
It seems like new \DateTime('1970-01-01') of type object<DateTime> is incompatible with the declared type object<Roomify\Bat\Constraint\DateTime> of property $start_date.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
60
    }
61
    if ($this->end_date === NULL) {
62
      $this->end_date = new \DateTime('2999-12-31');
0 ignored issues
show
Documentation Bug introduced by
It seems like new \DateTime('2999-12-31') of type object<DateTime> is incompatible with the declared type object<Roomify\Bat\Constraint\DateTime> of property $end_date.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
63
    }
64
65
    if ($this->start_date->getTimestamp() <= $calendar_response->getStartDate()->getTimestamp() &&
66
        $this->end_date->getTimestamp() >= $calendar_response->getEndDate()->getTimestamp() &&
67
        ($this->checkin_day === NULL || $this->checkin_day == $calendar_response->getStartDate()->format('N'))) {
68
69
      $units = $this->getUnits();
70
71
      $included_set = $calendar_response->getIncluded();
72
73
      foreach ($included_set as $unit_id => $set) {
74
        if (isset($units[$unit_id]) || empty($units)) {
75
          $start_date = $calendar_response->getStartDate();
76
          $end_date = $calendar_response->getEndDate();
77
78
          $diff = $end_date->diff($start_date)->days;
79
          if (is_numeric($this->min_days) && $diff < $this->min_days) {
80
            $calendar_response->removeFromMatched($included_set[$unit_id]['unit'], CalendarResponse::CONSTRAINT, $this);
81
82
            $this->affected_units[$unit_id] = $included_set[$unit_id]['unit'];
83
          }
84
          elseif (is_numeric($this->max_days) && $diff > $this->max_days) {
85
            $calendar_response->removeFromMatched($included_set[$unit_id]['unit'], CalendarResponse::CONSTRAINT, $this);
86
87
            $this->affected_units[$unit_id] = $included_set[$unit_id]['unit'];
88
          }
89
        }
90
      }
91
    }
92
  }
93
94
  /**
95
   * Generates a text describing an availability_constraint.
96
   *
97
   * @return string
98
   *   The formatted message.
99
   */
100
  public function toString() {
101
    $text = '';
102
103
    // Min/max stay length constraint variables.
104
    $minimum_stay = empty($this->min_days) ? '' : (($this->min_days == 1) ? '@count day' : '@count days');
105
    $maximum_stay = empty($this->max_days) ? '' : (($this->max_days == 1) ? '@count day' : '@count days');
106
107
    // Day of the week constraint variable.
108
    $day_of_the_week = $this->getWeekDay($this->checkin_day);
109
110
    // Date range constraint variables.
111
    $start_date = $this->start_date->format('Y-m-d');
112
    $end_date = $this->end_date->format('Y-m-d');
113
114
    // Next create replacement placeholders to be used in t() below.
115
    $args = array(
116
      '@minimum_stay' => $minimum_stay,
117
      '@maximum_stay' => $maximum_stay,
118
      '@start_date' => $start_date,
119
      '@end_date' => $end_date,
120
      '@day_of_the_week' => $day_of_the_week,
121
    );
122
123
    // Finally, build out the constraint text string adding components
124
    // as necessary.
125
126
    // Specify a date range constraint.
127
    if ($start_date && $end_date) {
128
      $text = 'From @start_date to @end_date';
129
    }
130
131
    // Specify the day of the week constraint.
132
    if ($day_of_the_week) {
133
      if ($start_date && $end_date) {
134
        $text = 'From @start_date to @end_date, if booking starts on @day_of_the_week';
135
      }
136
      else {
137
        $text = 'If booking starts on @day_of_the_week';
138
      }
139
    }
140
141
    // Specify the min/max stay length constraint.
142
    if ($minimum_stay || $maximum_stay) {
143
      if (empty($text)) {
144
        $text = 'The stay ';
145
      }
146
      else {
147
        $text .= ' the stay ';
148
      }
149
    }
150
    if ($minimum_stay && $maximum_stay) {
151
      // Special case when min stay and max stay are the same.
152
      if ($minimum_stay == $maximum_stay) {
153
        $text .= 'must be for @minimum_stay';
154
      }
155
      else {
156
        $text .= 'must be at least @minimum_stay and at most @maximum_stay';
157
      }
158
    }
159
    elseif ($minimum_stay) {
160
      $text .= 'must be for at least @minimum_stay';
161
    }
162
    elseif ($maximum_stay) {
163
      $text .= 'cannot be more than @maximum_stay';
164
    }
165
166
    return array('text' => $text, 'args' => $args);
167
  }
168
169
  /**
170
   * @param $day
171
   * @return string
172
   */
173 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...
174
    $weekdays = array(
175
      1 => 'Monday',
176
      2 => 'Tuesday',
177
      3 => 'Wednesday',
178
      4 => 'Thursday',
179
      5 => 'Friday',
180
      6 => 'Saturday',
181
      7 => 'Sunday',
182
    );
183
184
    return isset($weekdays[$day]) ? $weekdays[$day] : '';
185
  }
186
187
}
188