Total Complexity | 94 |
Total Lines | 296 |
Duplicated Lines | 0 % |
Changes | 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 |
||
11 | class Intervals |
||
12 | { |
||
13 | /** |
||
14 | @phpstan-var |
||
15 | */ |
||
16 | private static $intervalsCache = array(); |
||
17 | /** |
||
18 | @phpstan-var |
||
19 | */ |
||
20 | private static $opSortOrder = array('>=' => -3, '<' => -2, '>' => 2, '<=' => 3); |
||
21 | public static function clear() |
||
24 | } |
||
25 | public static function isSubsetOf(ConstraintInterface $candidate, ConstraintInterface $constraint) |
||
61 | } |
||
62 | public static function haveIntersections(ConstraintInterface $a, ConstraintInterface $b) |
||
63 | { |
||
64 | if ($a instanceof MatchAllConstraint || $b instanceof MatchAllConstraint) { |
||
65 | return \true; |
||
66 | } |
||
67 | if ($a instanceof MatchNoneConstraint || $b instanceof MatchNoneConstraint) { |
||
68 | return \false; |
||
69 | } |
||
70 | $intersectionIntervals = self::generateIntervals(new MultiConstraint(array($a, $b), \true), \true); |
||
71 | return \count($intersectionIntervals['numeric']) > 0 || $intersectionIntervals['branches']['exclude'] || \count($intersectionIntervals['branches']['names']) > 0; |
||
72 | } |
||
73 | public static function compactConstraint(ConstraintInterface $constraint) |
||
74 | { |
||
75 | if (!$constraint instanceof MultiConstraint) { |
||
76 | return $constraint; |
||
77 | } |
||
78 | $intervals = self::generateIntervals($constraint); |
||
79 | $constraints = array(); |
||
80 | $hasNumericMatchAll = \false; |
||
81 | if (\count($intervals['numeric']) === 1 && (string) $intervals['numeric'][0]->getStart() === (string) Interval::fromZero() && (string) $intervals['numeric'][0]->getEnd() === (string) Interval::untilPositiveInfinity()) { |
||
82 | $constraints[] = $intervals['numeric'][0]->getStart(); |
||
83 | $hasNumericMatchAll = \true; |
||
84 | } else { |
||
85 | $unEqualConstraints = array(); |
||
86 | for ($i = 0, $count = \count($intervals['numeric']); $i < $count; $i++) { |
||
87 | $interval = $intervals['numeric'][$i]; |
||
88 | if ($interval->getEnd()->getOperator() === '<' && $i + 1 < $count) { |
||
89 | $nextInterval = $intervals['numeric'][$i + 1]; |
||
90 | if ($interval->getEnd()->getVersion() === $nextInterval->getStart()->getVersion() && $nextInterval->getStart()->getOperator() === '>') { |
||
91 | if (\count($unEqualConstraints) === 0 && (string) $interval->getStart() !== (string) Interval::fromZero()) { |
||
92 | $unEqualConstraints[] = $interval->getStart(); |
||
93 | } |
||
94 | $unEqualConstraints[] = new Constraint('!=', $interval->getEnd()->getVersion()); |
||
95 | continue; |
||
96 | } |
||
97 | } |
||
98 | if (\count($unEqualConstraints) > 0) { |
||
99 | if ((string) $interval->getEnd() !== (string) Interval::untilPositiveInfinity()) { |
||
100 | $unEqualConstraints[] = $interval->getEnd(); |
||
101 | } |
||
102 | if (\count($unEqualConstraints) > 1) { |
||
103 | $constraints[] = new MultiConstraint($unEqualConstraints, \true); |
||
104 | } else { |
||
105 | $constraints[] = $unEqualConstraints[0]; |
||
106 | } |
||
107 | $unEqualConstraints = array(); |
||
108 | continue; |
||
109 | } |
||
110 | if ($interval->getStart()->getVersion() === $interval->getEnd()->getVersion() && $interval->getStart()->getOperator() === '>=' && $interval->getEnd()->getOperator() === '<=') { |
||
111 | $constraints[] = new Constraint('==', $interval->getStart()->getVersion()); |
||
112 | continue; |
||
113 | } |
||
114 | if ((string) $interval->getStart() === (string) Interval::fromZero()) { |
||
115 | $constraints[] = $interval->getEnd(); |
||
116 | } elseif ((string) $interval->getEnd() === (string) Interval::untilPositiveInfinity()) { |
||
117 | $constraints[] = $interval->getStart(); |
||
118 | } else { |
||
119 | $constraints[] = new MultiConstraint(array($interval->getStart(), $interval->getEnd()), \true); |
||
120 | } |
||
121 | } |
||
122 | } |
||
123 | $devConstraints = array(); |
||
124 | if (0 === \count($intervals['branches']['names'])) { |
||
125 | if ($intervals['branches']['exclude']) { |
||
126 | if ($hasNumericMatchAll) { |
||
127 | return new MatchAllConstraint(); |
||
128 | } |
||
129 | } |
||
130 | } else { |
||
131 | foreach ($intervals['branches']['names'] as $branchName) { |
||
132 | if ($intervals['branches']['exclude']) { |
||
133 | $devConstraints[] = new Constraint('!=', $branchName); |
||
134 | } else { |
||
135 | $devConstraints[] = new Constraint('==', $branchName); |
||
136 | } |
||
137 | } |
||
138 | if ($intervals['branches']['exclude']) { |
||
139 | if (\count($constraints) > 1) { |
||
140 | return new MultiConstraint(\array_merge(array(new MultiConstraint($constraints, \false)), $devConstraints), \true); |
||
141 | } |
||
142 | if (\count($constraints) === 1 && (string) $constraints[0] === (string) Interval::fromZero()) { |
||
143 | if (\count($devConstraints) > 1) { |
||
144 | return new MultiConstraint($devConstraints, \true); |
||
145 | } |
||
146 | return $devConstraints[0]; |
||
147 | } |
||
148 | return new MultiConstraint(\array_merge($constraints, $devConstraints), \true); |
||
149 | } |
||
150 | $constraints = \array_merge($constraints, $devConstraints); |
||
151 | } |
||
152 | if (\count($constraints) > 1) { |
||
153 | return new MultiConstraint($constraints, \false); |
||
154 | } |
||
155 | if (\count($constraints) === 1) { |
||
156 | return $constraints[0]; |
||
157 | } |
||
158 | return new MatchNoneConstraint(); |
||
159 | } |
||
160 | /** |
||
161 | @phpstan-return |
||
162 | */ |
||
163 | public static function get(ConstraintInterface $constraint) |
||
170 | } |
||
171 | /** |
||
172 | @phpstan-return |
||
173 | */ |
||
174 | private static function generateIntervals(ConstraintInterface $constraint, $stopOnFirstValidInterval = \false) |
||
279 | } |
||
280 | /** |
||
281 | @phpstan-return |
||
282 | */ |
||
283 | private static function generateSingleConstraintIntervals(Constraint $constraint) |
||
309 |