Total Complexity | 136 |
Total Lines | 325 |
Duplicated Lines | 0 % |
Changes | 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 |
||
10 | class VersionParser |
||
11 | { |
||
12 | private static $modifierRegex = '[._-]?(?:(stable|beta|b|RC|alpha|a|patch|pl|p)((?:[.-]?\\d+)*+)?)?([.-]?dev)?'; |
||
13 | private static $stabilitiesRegex = 'stable|RC|beta|alpha|dev'; |
||
14 | /** |
||
15 | @phpstan-return |
||
16 | */ |
||
17 | public static function parseStability($version) |
||
18 | { |
||
19 | $version = (string) \preg_replace('{#.+$}', '', (string) $version); |
||
20 | if (\strpos($version, 'dev-') === 0 || '-dev' === \substr($version, -4)) { |
||
21 | return 'dev'; |
||
22 | } |
||
23 | \preg_match('{' . self::$modifierRegex . '(?:\\+.*)?$}i', \strtolower($version), $match); |
||
24 | if (!empty($match[3])) { |
||
25 | return 'dev'; |
||
26 | } |
||
27 | if (!empty($match[1])) { |
||
28 | if ('beta' === $match[1] || 'b' === $match[1]) { |
||
29 | return 'beta'; |
||
30 | } |
||
31 | if ('alpha' === $match[1] || 'a' === $match[1]) { |
||
32 | return 'alpha'; |
||
33 | } |
||
34 | if ('rc' === $match[1]) { |
||
35 | return 'RC'; |
||
36 | } |
||
37 | } |
||
38 | return 'stable'; |
||
39 | } |
||
40 | public static function normalizeStability($stability) |
||
41 | { |
||
42 | $stability = \strtolower((string) $stability); |
||
43 | return $stability === 'rc' ? 'RC' : $stability; |
||
44 | } |
||
45 | public function normalize($version, $fullVersion = null) |
||
46 | { |
||
47 | $version = \trim((string) $version); |
||
48 | $origVersion = $version; |
||
49 | if (null === $fullVersion) { |
||
50 | $fullVersion = $version; |
||
51 | } |
||
52 | if (\preg_match('{^([^,\\s]++) ++as ++([^,\\s]++)$}', $version, $match)) { |
||
53 | $version = $match[1]; |
||
54 | } |
||
55 | if (\preg_match('{@(?:' . self::$stabilitiesRegex . ')$}i', $version, $match)) { |
||
56 | $version = \substr($version, 0, \strlen($version) - \strlen($match[0])); |
||
57 | } |
||
58 | if (\in_array($version, array('master', 'trunk', 'default'), \true)) { |
||
59 | $version = 'dev-' . $version; |
||
60 | } |
||
61 | if (\stripos($version, 'dev-') === 0) { |
||
62 | return 'dev-' . \substr($version, 4); |
||
63 | } |
||
64 | if (\preg_match('{^([^,\\s+]++)\\+[^\\s]++$}', $version, $match)) { |
||
65 | $version = $match[1]; |
||
66 | } |
||
67 | if (\preg_match('{^v?(\\d{1,5}+)(\\.\\d++)?(\\.\\d++)?(\\.\\d++)?' . self::$modifierRegex . '$}i', $version, $matches)) { |
||
68 | $version = $matches[1] . (!empty($matches[2]) ? $matches[2] : '.0') . (!empty($matches[3]) ? $matches[3] : '.0') . (!empty($matches[4]) ? $matches[4] : '.0'); |
||
69 | $index = 5; |
||
70 | } elseif (\preg_match('{^v?(\\d{4}(?:[.:-]?\\d{2}){1,6}(?:[.:-]?\\d{1,3}){0,2})' . self::$modifierRegex . '$}i', $version, $matches)) { |
||
71 | $version = (string) \preg_replace('{\\D}', '.', $matches[1]); |
||
72 | $index = 2; |
||
73 | } |
||
74 | if (isset($index)) { |
||
75 | if (!empty($matches[$index])) { |
||
76 | if ('stable' === $matches[$index]) { |
||
77 | return $version; |
||
78 | } |
||
79 | $version .= '-' . $this->expandStability($matches[$index]) . (isset($matches[$index + 1]) && '' !== $matches[$index + 1] ? \ltrim($matches[$index + 1], '.-') : ''); |
||
80 | } |
||
81 | if (!empty($matches[$index + 2])) { |
||
82 | $version .= '-dev'; |
||
83 | } |
||
84 | return $version; |
||
85 | } |
||
86 | if (\preg_match('{(.*?)[.-]?dev$}i', $version, $match)) { |
||
87 | try { |
||
88 | $normalized = $this->normalizeBranch($match[1]); |
||
89 | if (\strpos($normalized, 'dev-') === \false) { |
||
90 | return $normalized; |
||
91 | } |
||
92 | } catch (\Exception $e) { |
||
|
|||
93 | } |
||
94 | } |
||
95 | $extraMessage = ''; |
||
96 | if (\preg_match('{ +as +' . \preg_quote($version) . '(?:@(?:' . self::$stabilitiesRegex . '))?$}', $fullVersion)) { |
||
97 | $extraMessage = ' in "' . $fullVersion . '", the alias must be an exact version'; |
||
98 | } elseif (\preg_match('{^' . \preg_quote($version) . '(?:@(?:' . self::$stabilitiesRegex . '))? +as +}', $fullVersion)) { |
||
99 | $extraMessage = ' in "' . $fullVersion . '", the alias source must be an exact version, if it is a branch name you should prefix it with dev-'; |
||
100 | } |
||
101 | throw new \UnexpectedValueException('Invalid version string "' . $origVersion . '"' . $extraMessage); |
||
102 | } |
||
103 | public function parseNumericAliasPrefix($branch) |
||
104 | { |
||
105 | if (\preg_match('{^(?P<version>(\\d++\\.)*\\d++)(?:\\.x)?-dev$}i', (string) $branch, $matches)) { |
||
106 | return $matches['version'] . '.'; |
||
107 | } |
||
108 | return \false; |
||
109 | } |
||
110 | public function normalizeBranch($name) |
||
111 | { |
||
112 | $name = \trim((string) $name); |
||
113 | if (\preg_match('{^v?(\\d++)(\\.(?:\\d++|[xX*]))?(\\.(?:\\d++|[xX*]))?(\\.(?:\\d++|[xX*]))?$}i', $name, $matches)) { |
||
114 | $version = ''; |
||
115 | for ($i = 1; $i < 5; ++$i) { |
||
116 | $version .= isset($matches[$i]) ? \str_replace(array('*', 'X'), 'x', $matches[$i]) : '.x'; |
||
117 | } |
||
118 | return \str_replace('x', '9999999', $version) . '-dev'; |
||
119 | } |
||
120 | return 'dev-' . $name; |
||
121 | } |
||
122 | public function normalizeDefaultBranch($name) |
||
123 | { |
||
124 | if ($name === 'dev-master' || $name === 'dev-default' || $name === 'dev-trunk') { |
||
125 | return '9999999-dev'; |
||
126 | } |
||
127 | return (string) $name; |
||
128 | } |
||
129 | public function parseConstraints($constraints) |
||
130 | { |
||
131 | $prettyConstraint = (string) $constraints; |
||
132 | $orConstraints = \preg_split('{\\s*\\|\\|?\\s*}', \trim((string) $constraints)); |
||
133 | if (\false === $orConstraints) { |
||
134 | throw new \RuntimeException('Failed to preg_split string: ' . $constraints); |
||
135 | } |
||
136 | $orGroups = array(); |
||
137 | foreach ($orConstraints as $orConstraint) { |
||
138 | $andConstraints = \preg_split('{(?<!^|as|[=>< ,]) *(?<!-)[, ](?!-) *(?!,|as|$)}', $orConstraint); |
||
139 | if (\false === $andConstraints) { |
||
140 | throw new \RuntimeException('Failed to preg_split string: ' . $orConstraint); |
||
141 | } |
||
142 | if (\count($andConstraints) > 1) { |
||
143 | $constraintObjects = array(); |
||
144 | foreach ($andConstraints as $andConstraint) { |
||
145 | foreach ($this->parseConstraint($andConstraint) as $parsedAndConstraint) { |
||
146 | $constraintObjects[] = $parsedAndConstraint; |
||
147 | } |
||
148 | } |
||
149 | } else { |
||
150 | $constraintObjects = $this->parseConstraint($andConstraints[0]); |
||
151 | } |
||
152 | if (1 === \count($constraintObjects)) { |
||
153 | $constraint = $constraintObjects[0]; |
||
154 | } else { |
||
155 | $constraint = new MultiConstraint($constraintObjects); |
||
156 | } |
||
157 | $orGroups[] = $constraint; |
||
158 | } |
||
159 | $parsedConstraint = MultiConstraint::create($orGroups, \false); |
||
160 | $parsedConstraint->setPrettyString($prettyConstraint); |
||
161 | return $parsedConstraint; |
||
162 | } |
||
163 | /** |
||
164 | @phpstan-return |
||
165 | */ |
||
166 | private function parseConstraint($constraint) |
||
298 | } |
||
299 | /** |
||
300 | @phpstan-param |
||
301 | */ |
||
302 | private function manipulateVersionString(array $matches, $position, $increment = 0, $pad = '0') |
||
319 | } |
||
320 | private function expandStability($stability) |
||
335 | } |
||
336 | } |
||
337 | } |
||
338 |