Total Complexity | 94 |
Total Lines | 445 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like Intervals 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.
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 Intervals, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
32 | class Intervals |
||
33 | { |
||
34 | /** |
||
35 | * @phpstan-var array<string, array{'numeric': Interval[], 'branches': array{'names': string[], 'exclude': bool}}> |
||
36 | */ |
||
37 | private static $intervalsCache = array(); |
||
38 | |||
39 | /** |
||
40 | * @phpstan-var array<string, int> |
||
41 | */ |
||
42 | private static $opSortOrder = array( |
||
43 | '>=' => -3, |
||
44 | '<' => -2, |
||
45 | '>' => 2, |
||
46 | '<=' => 3, |
||
47 | ); |
||
48 | |||
49 | /** |
||
50 | * Clears the memoization cache once you are done |
||
51 | * |
||
52 | * @return void |
||
53 | */ |
||
54 | public static function clear() |
||
55 | { |
||
56 | self::$intervalsCache = array(); |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Checks whether $candidate is a subset of $constraint |
||
61 | * |
||
62 | * @return bool |
||
63 | */ |
||
64 | public static function isSubsetOf(ConstraintInterface $candidate, ConstraintInterface $constraint) |
||
65 | { |
||
66 | if ($constraint instanceof MatchAllConstraint) { |
||
67 | return true; |
||
68 | } |
||
69 | |||
70 | if ($candidate instanceof MatchNoneConstraint || $constraint instanceof MatchNoneConstraint) { |
||
71 | return false; |
||
72 | } |
||
73 | |||
74 | $intersectionIntervals = self::get(new MultiConstraint(array($candidate, $constraint), true)); |
||
75 | $candidateIntervals = self::get($candidate); |
||
76 | if (\count($intersectionIntervals['numeric']) !== \count($candidateIntervals['numeric'])) { |
||
77 | return false; |
||
78 | } |
||
79 | |||
80 | foreach ($intersectionIntervals['numeric'] as $index => $interval) { |
||
81 | if (!isset($candidateIntervals['numeric'][$index])) { |
||
82 | return false; |
||
83 | } |
||
84 | |||
85 | if ((string) $candidateIntervals['numeric'][$index]->getStart() !== (string) $interval->getStart()) { |
||
86 | return false; |
||
87 | } |
||
88 | |||
89 | if ((string) $candidateIntervals['numeric'][$index]->getEnd() !== (string) $interval->getEnd()) { |
||
90 | return false; |
||
91 | } |
||
92 | } |
||
93 | |||
94 | if ($intersectionIntervals['branches']['exclude'] !== $candidateIntervals['branches']['exclude']) { |
||
95 | return false; |
||
96 | } |
||
97 | if (\count($intersectionIntervals['branches']['names']) !== \count($candidateIntervals['branches']['names'])) { |
||
98 | return false; |
||
99 | } |
||
100 | foreach ($intersectionIntervals['branches']['names'] as $index => $name) { |
||
101 | if ($name !== $candidateIntervals['branches']['names'][$index]) { |
||
102 | return false; |
||
103 | } |
||
104 | } |
||
105 | |||
106 | return true; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * Checks whether $a and $b have any intersection, equivalent to $a->matches($b) |
||
111 | * |
||
112 | * @return bool |
||
113 | */ |
||
114 | public static function haveIntersections(ConstraintInterface $a, ConstraintInterface $b) |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * Attempts to optimize a MultiConstraint |
||
131 | * |
||
132 | * When merging MultiConstraints together they can get very large, this will |
||
133 | * compact it by looking at the real intervals covered by all the constraints |
||
134 | * and then creates a new constraint containing only the smallest amount of rules |
||
135 | * to match the same intervals. |
||
136 | * |
||
137 | * @return ConstraintInterface |
||
138 | */ |
||
139 | public static function compactConstraint(ConstraintInterface $constraint) |
||
140 | { |
||
141 | if (!$constraint instanceof MultiConstraint) { |
||
142 | return $constraint; |
||
143 | } |
||
144 | |||
145 | $intervals = self::generateIntervals($constraint); |
||
146 | $constraints = array(); |
||
147 | $hasNumericMatchAll = false; |
||
148 | |||
149 | if (\count($intervals['numeric']) === 1 && (string) $intervals['numeric'][0]->getStart() === (string) Interval::fromZero() && (string) $intervals['numeric'][0]->getEnd() === (string) Interval::untilPositiveInfinity()) { |
||
150 | $constraints[] = $intervals['numeric'][0]->getStart(); |
||
151 | $hasNumericMatchAll = true; |
||
152 | } else { |
||
153 | $unEqualConstraints = array(); |
||
154 | for ($i = 0, $count = \count($intervals['numeric']); $i < $count; $i++) { |
||
155 | $interval = $intervals['numeric'][$i]; |
||
156 | |||
157 | // if current interval ends with < N and next interval begins with > N we can swap this out for != N |
||
158 | // but this needs to happen as a conjunctive expression together with the start of the current interval |
||
159 | // and end of next interval, so [>=M, <N] || [>N, <P] => [>=M, !=N, <P] but M/P can be skipped if |
||
160 | // they are zero/+inf |
||
161 | if ($interval->getEnd()->getOperator() === '<' && $i+1 < $count) { |
||
162 | $nextInterval = $intervals['numeric'][$i+1]; |
||
163 | if ($interval->getEnd()->getVersion() === $nextInterval->getStart()->getVersion() && $nextInterval->getStart()->getOperator() === '>') { |
||
164 | // only add a start if we didn't already do so, can be skipped if we're looking at second |
||
165 | // interval in [>=M, <N] || [>N, <P] || [>P, <Q] where unEqualConstraints currently contains |
||
166 | // [>=M, !=N] already and we only want to add !=P right now |
||
167 | if (\count($unEqualConstraints) === 0 && (string) $interval->getStart() !== (string) Interval::fromZero()) { |
||
168 | $unEqualConstraints[] = $interval->getStart(); |
||
169 | } |
||
170 | $unEqualConstraints[] = new Constraint('!=', $interval->getEnd()->getVersion()); |
||
171 | continue; |
||
172 | } |
||
173 | } |
||
174 | |||
175 | if (\count($unEqualConstraints) > 0) { |
||
176 | // this is where the end of the following interval of a != constraint is added as explained above |
||
177 | if ((string) $interval->getEnd() !== (string) Interval::untilPositiveInfinity()) { |
||
178 | $unEqualConstraints[] = $interval->getEnd(); |
||
179 | } |
||
180 | |||
181 | // count is 1 if entire constraint is just one != expression |
||
182 | if (\count($unEqualConstraints) > 1) { |
||
183 | $constraints[] = new MultiConstraint($unEqualConstraints, true); |
||
184 | } else { |
||
185 | $constraints[] = $unEqualConstraints[0]; |
||
186 | } |
||
187 | |||
188 | $unEqualConstraints = array(); |
||
189 | continue; |
||
190 | } |
||
191 | |||
192 | // convert back >= x - <= x intervals to == x |
||
193 | if ($interval->getStart()->getVersion() === $interval->getEnd()->getVersion() && $interval->getStart()->getOperator() === '>=' && $interval->getEnd()->getOperator() === '<=') { |
||
194 | $constraints[] = new Constraint('==', $interval->getStart()->getVersion()); |
||
195 | continue; |
||
196 | } |
||
197 | |||
198 | if ((string) $interval->getStart() === (string) Interval::fromZero()) { |
||
199 | $constraints[] = $interval->getEnd(); |
||
200 | } elseif ((string) $interval->getEnd() === (string) Interval::untilPositiveInfinity()) { |
||
201 | $constraints[] = $interval->getStart(); |
||
202 | } else { |
||
203 | $constraints[] = new MultiConstraint(array($interval->getStart(), $interval->getEnd()), true); |
||
204 | } |
||
205 | } |
||
206 | } |
||
207 | |||
208 | $devConstraints = array(); |
||
209 | |||
210 | if (0 === \count($intervals['branches']['names'])) { |
||
211 | if ($intervals['branches']['exclude']) { |
||
212 | if ($hasNumericMatchAll) { |
||
213 | return new MatchAllConstraint; |
||
214 | } |
||
215 | // otherwise constraint should contain a != operator and already cover this |
||
216 | } |
||
217 | } else { |
||
218 | foreach ($intervals['branches']['names'] as $branchName) { |
||
219 | if ($intervals['branches']['exclude']) { |
||
220 | $devConstraints[] = new Constraint('!=', $branchName); |
||
221 | } else { |
||
222 | $devConstraints[] = new Constraint('==', $branchName); |
||
223 | } |
||
224 | } |
||
225 | |||
226 | // excluded branches, e.g. != dev-foo are conjunctive with the interval, so |
||
227 | // > 2.0 != dev-foo must return a conjunctive constraint |
||
228 | if ($intervals['branches']['exclude']) { |
||
229 | if (\count($constraints) > 1) { |
||
230 | return new MultiConstraint(array_merge( |
||
231 | array(new MultiConstraint($constraints, false)), |
||
232 | $devConstraints |
||
233 | ), true); |
||
234 | } |
||
235 | |||
236 | if (\count($constraints) === 1 && (string)$constraints[0] === (string)Interval::fromZero()) { |
||
237 | if (\count($devConstraints) > 1) { |
||
238 | return new MultiConstraint($devConstraints, true); |
||
239 | } |
||
240 | return $devConstraints[0]; |
||
241 | } |
||
242 | |||
243 | return new MultiConstraint(array_merge($constraints, $devConstraints), true); |
||
244 | } |
||
245 | |||
246 | // otherwise devConstraints contains a list of == operators for branches which are disjunctive with the |
||
247 | // rest of the constraint |
||
248 | $constraints = array_merge($constraints, $devConstraints); |
||
249 | } |
||
250 | |||
251 | if (\count($constraints) > 1) { |
||
252 | return new MultiConstraint($constraints, false); |
||
253 | } |
||
254 | |||
255 | if (\count($constraints) === 1) { |
||
256 | return $constraints[0]; |
||
257 | } |
||
258 | |||
259 | return new MatchNoneConstraint; |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Creates an array of numeric intervals and branch constraints representing a given constraint |
||
264 | * |
||
265 | * if the returned numeric array is empty it means the constraint matches nothing in the numeric range (0 - +inf) |
||
266 | * if the returned branches array is empty it means no dev-* versions are matched |
||
267 | * if a constraint matches all possible dev-* versions, branches will contain Interval::anyDev() |
||
268 | * |
||
269 | * @return array |
||
270 | * @phpstan-return array{'numeric': Interval[], 'branches': array{'names': string[], 'exclude': bool}} |
||
271 | */ |
||
272 | public static function get(ConstraintInterface $constraint) |
||
273 | { |
||
274 | $key = (string) $constraint; |
||
275 | |||
276 | if (!isset(self::$intervalsCache[$key])) { |
||
277 | self::$intervalsCache[$key] = self::generateIntervals($constraint); |
||
278 | } |
||
279 | |||
280 | return self::$intervalsCache[$key]; |
||
281 | } |
||
282 | |||
283 | /** |
||
284 | * @param bool $stopOnFirstValidInterval |
||
285 | * |
||
286 | * @phpstan-return array{'numeric': Interval[], 'branches': array{'names': string[], 'exclude': bool}} |
||
287 | */ |
||
288 | private static function generateIntervals(ConstraintInterface $constraint, $stopOnFirstValidInterval = false) |
||
431 | } |
||
432 | |||
433 | /** |
||
434 | * @phpstan-return array{'numeric': Interval[], 'branches': array{'names': string[], 'exclude': bool}}} |
||
435 | */ |
||
436 | private static function generateSingleConstraintIntervals(Constraint $constraint) |
||
477 | } |
||
478 | } |
||
479 |