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 ( b798ff...fcb034 )
by
unknown
02:41
created

ConstraintManager   C

Complexity

Total Complexity 55

Size/Duplication

Total Lines 240
Duplicated Lines 30 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 3
Bugs 1 Features 2
Metric Value
wmc 55
c 3
b 1
f 2
lcom 1
cbo 2
dl 72
loc 240
rs 6.8

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A getConstraints() 0 8 2
A addConstraints() 0 5 2
A setConstraints() 0 4 1
C normalizeConstraints() 0 39 8
C normalizeMinMaxDaysConstraints() 35 72 21
C normalizeCheckInDayConstraints() 37 71 19

How to fix   Duplicated Code    Complexity   

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:

Complex Class

 Tip:   Before tackling complexity, make sure that you eliminate any duplication first. This often can reduce the size of classes significantly.

Complex classes like ConstraintManager often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use ConstraintManager, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
/**
4
 * @file
5
 * Class ConstraintManager
6
 */
7
8
namespace Roomify\Bat\Constraint;
9
10
class ConstraintManager {
11
12
  protected $constraints;
13
14
  /**
15
   * @param $constraints
16
   */
17
  public function __construct($constraints = array()) {
18
    $this->constraints = array();
19
    foreach ($constraints as $constraint) {
20
      $this->constraints[get_class($constraint)][] = $constraint;
21
    }
22
  }
23
24
  /**
25
   * @return array
26
   */
27
  public function getConstraints($constraint_class = NULL) {
28
    if ($constraint_class == NULL) {
29
      return $this->constraint;
0 ignored issues
show
Bug introduced by
The property constraint does not seem to exist. Did you mean constraints?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
30
    }
31
    else {
32
      return $this->constraint[$constraint_class];
0 ignored issues
show
Bug introduced by
The property constraint does not seem to exist. Did you mean constraints?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
33
    }
34
  }
35
36
  /**
37
   * @param $constraints
38
   */
39
  public function addConstraints($constraints) {
40
    foreach ($constraints as $constraint) {
41
      $this->constraints[get_class($constraint)][] = $constraint;
42
    }
43
  }
44
45
  /**
46
   * @param $constraints
47
   */
48
  public function setConstraints($constraints) {
49
    $this->constraints = array();
50
    $this->addConstraints($constraints);
51
  }
52
53
  /**
54
   * @param $constraint_class
55
   *
56
   * @return array
57
   */
58
  public function normalizeConstraints($constraint_class = NULL) {
59
    if ($constraint_class == NULL) {
60
      $classes = array_keys($this->constraints);
61
    }
62
    else {
63
      if (isset($this->constraints[$constraint_class])) {
64
        $classes = array($constraint_class);
65
      }
66
      else {
67
        return array();
68
      }
69
    }
70
71
    $new_constraints = array();
72
73
    foreach ($classes as $class) {
74
      switch ($class) {
75
        case 'Roomify\Bat\Constraint\MinMaxDaysConstraint':
76
          $new_constraints[$class] = $this->normalizeMinMaxDaysConstraints();
77
          break;
78
79
        case 'Roomify\Bat\Constraint\CheckInDayConstraint':
80
          $new_constraints[$class] = $this->normalizeCheckInDayConstraints();
81
          break;
82
83
        default:
84
          if (isset($this->constraints[$class])) {
85
            $new_constraints[$class] = $this->constraints[$class];
86
          }
87
      }
88
    }
89
90
    if ($constraint_class == NULL) {
91
      return $new_constraints;
92
    }
93
    else {
94
      return $new_constraints[$constraint_class];
95
    }
96
  }
97
98
  /**
99
   * @return array
100
   */
101
  protected function normalizeMinMaxDaysConstraints() {
102
    $new_constraints = array();
103
104
    $constraints = array_map(function ($object) { return clone $object; }, $this->constraints['Roomify\Bat\Constraint\MinMaxDaysConstraint']);
105
106
    foreach (array_reverse($constraints) as $constraint) {
107
      $start_date = $constraint->getStartDate();
108
      $end_date = $constraint->getEndDate();
109
110
      $split_constraint = NULL;
111
112
      if (!empty($new_constraints)) {
113
        foreach ($new_constraints as $new_constraint) {
114
          $new_start_date = $new_constraint->getStartDate();
115
          $new_end_date = $new_constraint->getEndDate();
116
117
          if ($constraint->getMinDays() && $new_constraint->getMinDays() ||
118
              ($constraint->getMaxDays() && $new_constraint->getMaxDays())) {
119 View Code Duplication
            if ($start_date >= $new_start_date && $start_date <= $new_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...
120
              $new_end_date_clone = clone($new_end_date);
121
              $constraint->setStartDate($new_end_date_clone->add(new \DateInterval('P1D')));
122
            }
123
            elseif ($end_date >= $new_start_date && $end_date <= $new_end_date) {
124
              $new_start_date_clone = clone($new_start_date);
125
              $constraint->setEndDate($new_start_date_clone->sub(new \DateInterval('P1D')));
126
            }
127
            elseif ($start_date < $new_start_date && $end_date > $new_end_date) {
128
              if ($constraint->getEndDate() > $new_start_date) {
129
                $new_start_date_clone = clone($new_start_date);
130
                $constraint->setEndDate($new_start_date_clone->sub(new \DateInterval('P1D')));
131
              }
132
133
              if ($split_constraint == NULL) {
134
                $split_start_date = clone($new_end_date);
135
                $split_start_date->add(new \DateInterval('P1D'));
136
                $split_end_date = $end_date;
137
138
                $split_constraint = new MinMaxDaysConstraint($constraint->getUnits(), $constraint->getMinDays(), $constraint->getMaxDays(), $split_start_date, $split_end_date, $constraint->getCheckinDay());
139
              }
140
              else {
141
                $split_start_date = $split_constraint->getStartDate();
142
                $split_end_date = $split_constraint->getEndDate();
143
144
                if ($split_start_date < $new_end_date) {
145
                  $new_end_date_clone = clone($new_end_date);
146
                  $split_constraint->setStartDate($new_end_date_clone->add(new \DateInterval('P1D')));
147
                }
148
                if ($split_end_date < $new_start_date) {
149
                  $new_start_date_clone = clone($new_start_date);
150
                  $split_constraint->setEndDate($new_start_date_clone->sub(new \DateInterval('P1D')));
151
                }
152
              }
153
            }
154
          }
155
        }
156
157
        if ($split_constraint != NULL) {
158
          $new_constraints[] = $split_constraint;
159
        }
160
      }
161
162
      $new_constraints[] = $constraint;
163
    }
164
165
    foreach ($new_constraints as $i => $constraint) {
166
      if ($constraint->getStartDate() > $constraint->getEndDate()) {
167
        unset($new_constraints[$i]);
168
      }
169
    }
170
171
    return $new_constraints;
172
  }
173
174
  /**
175
   * @return array
176
   */
177
  protected function normalizeCheckInDayConstraints() {
178
    $new_constraints = array();
179
180
    $constraints = array_map(function ($object) { return clone $object; }, $this->constraints['Roomify\Bat\Constraint\CheckInDayConstraint']);
181
182
    foreach (array_reverse($constraints) as $constraint) {
183
      $start_date = $constraint->getStartDate();
184
      $end_date = $constraint->getEndDate();
185
186
      $split_constraint = NULL;
187
188
      if (!empty($new_constraints)) {
189
        foreach ($new_constraints as $new_constraint) {
190
          $new_start_date = $new_constraint->getStartDate();
191
          $new_end_date = $new_constraint->getEndDate();
192
193 View Code Duplication
          if ($constraint->getCheckinDay() && $new_constraint->getCheckinDay()) {
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...
194
            if ($start_date >= $new_start_date && $start_date <= $new_end_date) {
195
              $new_end_date_clone = clone($new_end_date);
196
              $constraint->setStartDate($new_end_date_clone->add(new \DateInterval('P1D')));
197
            }
198
            elseif ($end_date >= $new_start_date && $end_date <= $new_end_date) {
199
              $new_start_date_clone = clone($new_start_date);
200
              $constraint->setEndDate($new_start_date_clone->sub(new \DateInterval('P1D')));
201
            }
202
            elseif ($start_date < $new_start_date && $end_date > $new_end_date) {
203
              if ($constraint->getEndDate() > $new_start_date) {
204
                $new_start_date_clone = clone($new_start_date);
205
                $constraint->setEndDate($new_start_date_clone->sub(new \DateInterval('P1D')));
206
              }
207
208
              if ($split_constraint == NULL) {
209
                $split_start_date = clone($new_end_date);
210
                $split_start_date->add(new \DateInterval('P1D'));
211
                $split_end_date = $end_date;
212
213
                $split_constraint = new CheckInDayConstraint($constraint->getUnits(), $constraint->getCheckinDay(), $split_start_date, $split_end_date);
214
              }
215
              else {
216
                $split_start_date = $split_constraint->getStartDate();
217
                $split_end_date = $split_constraint->getEndDate();
218
219
                if ($split_start_date < $new_end_date) {
220
                  $new_end_date_clone = clone($new_end_date);
221
                  $split_constraint->setStartDate($new_end_date_clone->add(new \DateInterval('P1D')));
222
                }
223
                if ($split_end_date < $new_start_date) {
224
                  $new_start_date_clone = clone($new_start_date);
225
                  $split_constraint->setEndDate($new_start_date_clone->sub(new \DateInterval('P1D')));
226
                }
227
              }
228
            }
229
          }
230
        }
231
232
        if ($split_constraint != NULL) {
233
          $new_constraints[] = $split_constraint;
234
        }
235
      }
236
237
      $new_constraints[] = $constraint;
238
    }
239
240
    foreach ($new_constraints as $i => $constraint) {
241
      if ($constraint->getStartDate() > $constraint->getEndDate()) {
242
        unset($new_constraints[$i]);
243
      }
244
    }
245
246
    return $new_constraints;
247
  }
248
  
249
}
250