Total Complexity | 136 |
Total Lines | 557 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Complex classes like VersionParser 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 VersionParser, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
24 | class VersionParser |
||
25 | { |
||
26 | /** |
||
27 | * Regex to match pre-release data (sort of). |
||
28 | * |
||
29 | * Due to backwards compatibility: |
||
30 | * - Instead of enforcing hyphen, an underscore, dot or nothing at all are also accepted. |
||
31 | * - Only stabilities as recognized by Composer are allowed to precede a numerical identifier. |
||
32 | * - Numerical-only pre-release identifiers are not supported, see tests. |
||
33 | * |
||
34 | * |--------------| |
||
35 | * [major].[minor].[patch] -[pre-release] +[build-metadata] |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | private static $modifierRegex = '[._-]?(?:(stable|beta|b|RC|alpha|a|patch|pl|p)((?:[.-]?\d+)*+)?)?([.-]?dev)?'; |
||
40 | |||
41 | /** @var string */ |
||
42 | private static $stabilitiesRegex = 'stable|RC|beta|alpha|dev'; |
||
43 | |||
44 | /** |
||
45 | * Returns the stability of a version. |
||
46 | * |
||
47 | * @param string $version |
||
48 | * |
||
49 | * @return string |
||
50 | */ |
||
51 | public static function parseStability($version) |
||
52 | { |
||
53 | $version = (string) preg_replace('{#.+$}', '', $version); |
||
54 | |||
55 | if (strpos($version, 'dev-') === 0 || '-dev' === substr($version, -4)) { |
||
56 | return 'dev'; |
||
57 | } |
||
58 | |||
59 | preg_match('{' . self::$modifierRegex . '(?:\+.*)?$}i', strtolower($version), $match); |
||
60 | |||
61 | if (!empty($match[3])) { |
||
62 | return 'dev'; |
||
63 | } |
||
64 | |||
65 | if (!empty($match[1])) { |
||
66 | if ('beta' === $match[1] || 'b' === $match[1]) { |
||
67 | return 'beta'; |
||
68 | } |
||
69 | if ('alpha' === $match[1] || 'a' === $match[1]) { |
||
70 | return 'alpha'; |
||
71 | } |
||
72 | if ('rc' === $match[1]) { |
||
73 | return 'RC'; |
||
74 | } |
||
75 | } |
||
76 | |||
77 | return 'stable'; |
||
78 | } |
||
79 | |||
80 | /** |
||
81 | * @param string $stability |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | public static function normalizeStability($stability) |
||
86 | { |
||
87 | $stability = strtolower($stability); |
||
88 | |||
89 | return $stability === 'rc' ? 'RC' : $stability; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Normalizes a version string to be able to perform comparisons on it. |
||
94 | * |
||
95 | * @param string $version |
||
96 | * @param string $fullVersion optional complete version string to give more context |
||
97 | * |
||
98 | * @throws \UnexpectedValueException |
||
99 | * |
||
100 | * @return string |
||
101 | */ |
||
102 | public function normalize($version, $fullVersion = null) |
||
103 | { |
||
104 | $version = trim($version); |
||
105 | $origVersion = $version; |
||
106 | if (null === $fullVersion) { |
||
107 | $fullVersion = $version; |
||
108 | } |
||
109 | |||
110 | // strip off aliasing |
||
111 | if (preg_match('{^([^,\s]++) ++as ++([^,\s]++)$}', $version, $match)) { |
||
112 | $version = $match[1]; |
||
113 | } |
||
114 | |||
115 | // strip off stability flag |
||
116 | if (preg_match('{@(?:' . self::$stabilitiesRegex . ')$}i', $version, $match)) { |
||
117 | $version = substr($version, 0, strlen($version) - strlen($match[0])); |
||
118 | } |
||
119 | |||
120 | // normalize master/trunk/default branches to dev-name for BC with 1.x as these used to be valid constraints |
||
121 | if (\in_array($version, array('master', 'trunk', 'default'), true)) { |
||
122 | $version = 'dev-' . $version; |
||
123 | } |
||
124 | |||
125 | // if requirement is branch-like, use full name |
||
126 | if (stripos($version, 'dev-') === 0) { |
||
127 | return 'dev-' . substr($version, 4); |
||
128 | } |
||
129 | |||
130 | // strip off build metadata |
||
131 | if (preg_match('{^([^,\s+]++)\+[^\s]++$}', $version, $match)) { |
||
132 | $version = $match[1]; |
||
133 | } |
||
134 | |||
135 | // match classical versioning |
||
136 | if (preg_match('{^v?(\d{1,5})(\.\d++)?(\.\d++)?(\.\d++)?' . self::$modifierRegex . '$}i', $version, $matches)) { |
||
137 | $version = $matches[1] |
||
138 | . (!empty($matches[2]) ? $matches[2] : '.0') |
||
139 | . (!empty($matches[3]) ? $matches[3] : '.0') |
||
140 | . (!empty($matches[4]) ? $matches[4] : '.0'); |
||
141 | $index = 5; |
||
142 | // match date(time) based versioning |
||
143 | } elseif (preg_match('{^v?(\d{4}(?:[.:-]?\d{2}){1,6}(?:[.:-]?\d{1,3})?)' . self::$modifierRegex . '$}i', $version, $matches)) { |
||
144 | $version = preg_replace('{\D}', '.', $matches[1]); |
||
145 | $index = 2; |
||
146 | } |
||
147 | |||
148 | // add version modifiers if a version was matched |
||
149 | if (isset($index)) { |
||
150 | if (!empty($matches[$index])) { |
||
151 | if ('stable' === $matches[$index]) { |
||
152 | return $version; |
||
153 | } |
||
154 | $version .= '-' . $this->expandStability($matches[$index]) . (isset($matches[$index + 1]) && '' !== $matches[$index + 1] ? ltrim($matches[$index + 1], '.-') : ''); |
||
155 | } |
||
156 | |||
157 | if (!empty($matches[$index + 2])) { |
||
158 | $version .= '-dev'; |
||
159 | } |
||
160 | |||
161 | return $version; |
||
162 | } |
||
163 | |||
164 | // match dev branches |
||
165 | if (preg_match('{(.*?)[.-]?dev$}i', $version, $match)) { |
||
166 | try { |
||
167 | $normalized = $this->normalizeBranch($match[1]); |
||
168 | // a branch ending with -dev is only valid if it is numeric |
||
169 | // if it gets prefixed with dev- it means the branch name should |
||
170 | // have had a dev- prefix already when passed to normalize |
||
171 | if (strpos($normalized, 'dev-') === false) { |
||
172 | return $normalized; |
||
173 | } |
||
174 | } catch (\Exception $e) { |
||
|
|||
175 | } |
||
176 | } |
||
177 | |||
178 | $extraMessage = ''; |
||
179 | if (preg_match('{ +as +' . preg_quote($version) . '(?:@(?:'.self::$stabilitiesRegex.'))?$}', $fullVersion)) { |
||
180 | $extraMessage = ' in "' . $fullVersion . '", the alias must be an exact version'; |
||
181 | } elseif (preg_match('{^' . preg_quote($version) . '(?:@(?:'.self::$stabilitiesRegex.'))? +as +}', $fullVersion)) { |
||
182 | $extraMessage = ' in "' . $fullVersion . '", the alias source must be an exact version, if it is a branch name you should prefix it with dev-'; |
||
183 | } |
||
184 | |||
185 | throw new \UnexpectedValueException('Invalid version string "' . $origVersion . '"' . $extraMessage); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Extract numeric prefix from alias, if it is in numeric format, suitable for version comparison. |
||
190 | * |
||
191 | * @param string $branch Branch name (e.g. 2.1.x-dev) |
||
192 | * |
||
193 | * @return string|false Numeric prefix if present (e.g. 2.1.) or false |
||
194 | */ |
||
195 | public function parseNumericAliasPrefix($branch) |
||
196 | { |
||
197 | if (preg_match('{^(?P<version>(\d++\\.)*\d++)(?:\.x)?-dev$}i', $branch, $matches)) { |
||
198 | return $matches['version'] . '.'; |
||
199 | } |
||
200 | |||
201 | return false; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Normalizes a branch name to be able to perform comparisons on it. |
||
206 | * |
||
207 | * @param string $name |
||
208 | * |
||
209 | * @return string |
||
210 | */ |
||
211 | public function normalizeBranch($name) |
||
212 | { |
||
213 | $name = trim($name); |
||
214 | |||
215 | if (preg_match('{^v?(\d++)(\.(?:\d++|[xX*]))?(\.(?:\d++|[xX*]))?(\.(?:\d++|[xX*]))?$}i', $name, $matches)) { |
||
216 | $version = ''; |
||
217 | for ($i = 1; $i < 5; ++$i) { |
||
218 | $version .= isset($matches[$i]) ? str_replace(array('*', 'X'), 'x', $matches[$i]) : '.x'; |
||
219 | } |
||
220 | |||
221 | return str_replace('x', '9999999', $version) . '-dev'; |
||
222 | } |
||
223 | |||
224 | return 'dev-' . $name; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * Normalizes a default branch name (i.e. master on git) to 9999999-dev. |
||
229 | * |
||
230 | * @param string $name |
||
231 | * |
||
232 | * @return string |
||
233 | */ |
||
234 | public function normalizeDefaultBranch($name) |
||
235 | { |
||
236 | if ($name === 'dev-master' || $name === 'dev-default' || $name === 'dev-trunk') { |
||
237 | return '9999999-dev'; |
||
238 | } |
||
239 | |||
240 | return $name; |
||
241 | } |
||
242 | |||
243 | /** |
||
244 | * Parses a constraint string into MultiConstraint and/or Constraint objects. |
||
245 | * |
||
246 | * @param string $constraints |
||
247 | * |
||
248 | * @return ConstraintInterface |
||
249 | */ |
||
250 | public function parseConstraints($constraints) |
||
251 | { |
||
252 | $prettyConstraint = $constraints; |
||
253 | |||
254 | $orConstraints = preg_split('{\s*\|\|?\s*}', trim($constraints)); |
||
255 | if (false === $orConstraints) { |
||
256 | throw new \RuntimeException('Failed to preg_split string: '.$constraints); |
||
257 | } |
||
258 | $orGroups = array(); |
||
259 | |||
260 | foreach ($orConstraints as $constraints) { |
||
261 | $andConstraints = preg_split('{(?<!^|as|[=>< ,]) *(?<!-)[, ](?!-) *(?!,|as|$)}', $constraints); |
||
262 | if (false === $andConstraints) { |
||
263 | throw new \RuntimeException('Failed to preg_split string: '.$constraints); |
||
264 | } |
||
265 | if (\count($andConstraints) > 1) { |
||
266 | $constraintObjects = array(); |
||
267 | foreach ($andConstraints as $constraint) { |
||
268 | foreach ($this->parseConstraint($constraint) as $parsedConstraint) { |
||
269 | $constraintObjects[] = $parsedConstraint; |
||
270 | } |
||
271 | } |
||
272 | } else { |
||
273 | $constraintObjects = $this->parseConstraint($andConstraints[0]); |
||
274 | } |
||
275 | |||
276 | if (1 === \count($constraintObjects)) { |
||
277 | $constraint = $constraintObjects[0]; |
||
278 | } else { |
||
279 | $constraint = new MultiConstraint($constraintObjects); |
||
280 | } |
||
281 | |||
282 | $orGroups[] = $constraint; |
||
283 | } |
||
284 | |||
285 | $constraint = MultiConstraint::create($orGroups, false); |
||
286 | |||
287 | $constraint->setPrettyString($prettyConstraint); |
||
288 | |||
289 | return $constraint; |
||
290 | } |
||
291 | |||
292 | /** |
||
293 | * @param string $constraint |
||
294 | * |
||
295 | * @throws \UnexpectedValueException |
||
296 | * |
||
297 | * @return array |
||
298 | * |
||
299 | * @phpstan-return non-empty-array<ConstraintInterface> |
||
300 | */ |
||
301 | private function parseConstraint($constraint) |
||
519 | } |
||
520 | |||
521 | /** |
||
522 | * Increment, decrement, or simply pad a version number. |
||
523 | * |
||
524 | * Support function for {@link parseConstraint()} |
||
525 | * |
||
526 | * @param array $matches Array with version parts in array indexes 1,2,3,4 |
||
527 | * @param int $position 1,2,3,4 - which segment of the version to increment/decrement |
||
528 | * @param int $increment |
||
529 | * @param string $pad The string to pad version parts after $position |
||
530 | * |
||
531 | * @return string|null The new version |
||
532 | * |
||
533 | * @phpstan-param string[] $matches |
||
534 | */ |
||
535 | private function manipulateVersionString(array $matches, $position, $increment = 0, $pad = '0') |
||
536 | { |
||
537 | for ($i = 4; $i > 0; --$i) { |
||
538 | if ($i > $position) { |
||
539 | $matches[$i] = $pad; |
||
540 | } elseif ($i === $position && $increment) { |
||
541 | $matches[$i] += $increment; |
||
542 | // If $matches[$i] was 0, carry the decrement |
||
543 | if ($matches[$i] < 0) { |
||
544 | $matches[$i] = $pad; |
||
545 | --$position; |
||
546 | |||
547 | // Return null on a carry overflow |
||
548 | if ($i === 1) { |
||
549 | return null; |
||
550 | } |
||
551 | } |
||
552 | } |
||
553 | } |
||
554 | |||
555 | return $matches[1] . '.' . $matches[2] . '.' . $matches[3] . '.' . $matches[4]; |
||
556 | } |
||
557 | |||
558 | /** |
||
559 | * Expand shorthand stability string to long version. |
||
560 | * |
||
561 | * @param string $stability |
||
562 | * |
||
563 | * @return string |
||
564 | */ |
||
565 | private function expandStability($stability) |
||
581 | } |
||
582 | } |
||
583 | } |
||
584 |