-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathc2efde.diff
More file actions
966 lines (888 loc) · 39.3 KB
/
Copy pathc2efde.diff
File metadata and controls
966 lines (888 loc) · 39.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
commit c2efde6d4897be0ad64ae63f3dbc47de2767cd45
Author: Theodore R. Smith <theodore@phpexperts.pro>
Date: Tue Apr 15 13:04:34 2025 -0500
Claude 3.7 Sonnet refactoring.
diff --git a/src/ComposerConstraintsHelper.php b/src/ComposerConstraintsHelper.php
index ce67f21..f5edc09 100644
--- a/src/ComposerConstraintsHelper.php
+++ b/src/ComposerConstraintsHelper.php
@@ -2,27 +2,27 @@
namespace App\Helpers;
-// Assuming this is your base test case
use Composer\Semver\Constraint\Constraint;
use Composer\Semver\Semver;
use Composer\Semver\VersionParser;
-
-// Useful for inspecting parsed constraints
use UnexpectedValueException;
/**
* Utility class for working with Composer version constraints.
*
* Provides methods for validating constraints, checking if a version satisfies
- * a constraint (using the authoritative Composer library), and generating
- * example versions that match constraints.
- *
- * Note: Version generation is heuristic and may not find a valid version for
- * all complex or contradictory constraints.
+ * a constraint, and generating example versions that match constraints.
*/
-class ComposerConstraintHelper // Renamed: More accurately reflects its role as a helper/utility
+class ComposerConstraintHelper
{
- function matchesVersionAuthoritative(string $constraint, string $version): bool
+ /**
+ * Check if a version satisfies a constraint using Composer's official library.
+ *
+ * @param string $constraint The version constraint to check against
+ * @param string $version The version to validate
+ * @return bool True if the version satisfies the constraint
+ */
+ public function matchesVersionAuthoritative(string $constraint, string $version): bool
{
return Semver::satisfies($version, $constraint);
}
@@ -30,21 +30,27 @@ class ComposerConstraintHelper // Renamed: More accurately reflects its role as
/**
* Check if a Composer version constraint is valid.
*
- * @param string $constraint The version constraint to validate.
- * @return bool True if valid, false if invalid.
+ * @param string $constraint The version constraint to validate
+ * @return bool True if valid, false if invalid
*/
- function isValidVersionConstraint(string $constraint): bool {
+ public function isValidVersionConstraint(string $constraint): bool
+ {
$parser = new VersionParser();
try {
- // This will throw an exception if the constraint is invalid.
$parser->parseConstraints($constraint);
return true;
- } catch (\UnexpectedValueException $e) {
+ } catch (UnexpectedValueException $e) {
return false;
}
}
+ /**
+ * Ensures a version string has at least 3 parts (major.minor.patch).
+ *
+ * @param string $version Version to normalize
+ * @return string Normalized version with at least 3 parts
+ */
private function ensure2Dots(string $version): string
{
$versionParts = explode('.', $version);
@@ -55,19 +61,22 @@ class ComposerConstraintHelper // Renamed: More accurately reflects its role as
return $version;
}
+ /**
+ * Check if a version satisfies a constraint.
+ *
+ * @param string $constraints Version constraint (can contain OR/AND operators)
+ * @param string $version Version to check
+ * @return bool True if the version satisfies the constraint
+ */
public function versionSatisfies(string $constraints, string $version): bool
{
// Normalize version to have at least 3 parts
$version = $this->ensure2Dots($version);
- // Convert "* >" to ">" and other normalizations.
- $constraints = preg_replace('/\* ?([><!]=?)\s*/', '$1', $constraints);
- $constraints = preg_replace('/^ ?([><!]=?)\s*/', '$1', $constraints);
- $constraints = str_replace('. *', '.*', $constraints);
+ // Normalize constraint formatting
+ $constraints = $this->normalizeConstraints($constraints);
- // Split constraint by OR operator
- $constraints = str_replace([' ,', ', '], ',', $constraints);
- $constraints = str_replace([' -', '- '], '-', $constraints);
+ // Split constraint by OR operator (| or -)
$orConstraints = preg_split('/[|-]/', $constraints);
foreach ($orConstraints as $orConstraint) {
@@ -78,66 +87,79 @@ class ComposerConstraintHelper // Renamed: More accurately reflects its role as
$allAndSatisfied = true;
foreach ($andConstraints as $singleConstraint) {
- $status = $this->satisfiesSingleConstraint($singleConstraint, $version);
- if (!$status) {
+ if (!empty($singleConstraint) && !$this->satisfiesSingleConstraint($singleConstraint, $version)) {
$allAndSatisfied = false;
break;
}
}
if ($allAndSatisfied) {
- return true; // If all AND conditions in this OR branch are satisfied, return true
+ return true; // If all AND conditions in this OR branch are satisfied, version is valid
}
}
return false;
}
+ /**
+ * Normalize constraint string formatting.
+ *
+ * @param string $constraints Constraints to normalize
+ * @return string Normalized constraints
+ */
+ private function normalizeConstraints(string $constraints): string
+ {
+ // Convert "* >" to ">" and other normalizations
+ $constraints = preg_replace('/\* ?([><!]=?)\s*/', '$1', $constraints);
+ $constraints = preg_replace('/^ ?([><!]=?)\s*/', '$1', $constraints);
+ $constraints = str_replace('. *', '.*', $constraints);
+
+ // Normalize spaces around commas and hyphens
+ $constraints = str_replace([' ,', ', '], ',', $constraints);
+ $constraints = str_replace([' -', '- '], '-', $constraints);
+
+ return $constraints;
+ }
+
+ /**
+ * Check if a version satisfies a single constraint.
+ *
+ * @param string $constraint Single constraint without AND/OR operators
+ * @param string $version Version to check
+ * @return bool True if the version satisfies the constraint
+ */
private function satisfiesSingleConstraint(string $constraint, string $version): bool
{
- // Handle edge cases like 'x' or '*'
+ // Edge cases
if ($constraint === 'x' || $constraint === '*') {
return true;
}
- // Always return true for git branch constraints.
+ // Always pass dev/rc branches
if (str_ends_with($constraint, '-dev') || str_ends_with($constraint, '-rc')) {
return true;
}
- // Strip v...
+ // Strip v prefix
$constraint = preg_replace('/([><=~^]+)?v/i', '$1', $constraint);
$version = preg_replace('/v([0-9]+)/i', '$1', $version);
- // Normalize hyphen ranges first
+ // Normalize hyphen ranges (e.g., "1.0 - 2.0" to ">=1.0 <2.0")
$constraint = $this->normalizeHyphenRanges($constraint);
- // Remove spaces around comparison operators (>=, >, <, !=) in the constraint string
+ // Format standardization
$constraint = preg_replace('/([><!]=?)\s*/', '$1', $constraint);
-
- // Convert "* >" to ">" and other normalizations.
$constraint = preg_replace('/\* ?([><!]=?)\s*/', '$1', $constraint);
$constraint = str_replace('. *', '.*', $constraint);
- // If it ends with ".", add a "*".
- // This effects 615 projects as of 2025-03-24.
- // @see rinsvent/data2dto
+ // Handle trailing dot (e.g., "1.0.")
if (str_ends_with($constraint, '.')) {
$constraint = substr($constraint, 0, -1);
}
-
- // Handle dev/rc suffixes
- if (str_ends_with($constraint, '-dev') || str_ends_with($constraint, '-rc')) {
- return true; // Simplified for this example
- }
-
// Replace .x with .*
$constraint = str_ireplace('.x', '.*', $constraint);
- // Strip leading 'v'
- $constraint = preg_replace('/([><=~^]+)?v/i', '$1', $constraint);
-
// Handle wildcards
if (str_contains($constraint, '*')) {
$pattern = str_replace('.', '\.', $constraint);
@@ -145,34 +167,14 @@ class ComposerConstraintHelper // Renamed: More accurately reflects its role as
return preg_match('/^' . $pattern . '/', $version) === 1;
}
- // Handle caret (^)
+ // Handle caret (^) - allows changes that don't modify the left-most non-zero digit
if (str_starts_with($constraint, '^')) {
- $baseVersion = substr($constraint, 1);
- $baseVersion = $this->ensure2Dots($baseVersion);
-
- if (preg_match('/^0\.(\d+)/', $baseVersion, $matches)) {
- $minor = (int)$matches[1];
- $nextMinor = "0." . ($minor + 1) . ".0";
- return version_compare($version, $baseVersion, '>=') &&
- version_compare($version, $nextMinor, '<');
- } elseif (preg_match('/^(\d+)/', $baseVersion, $matches)) {
- $major = (int)$matches[0];
- $nextMajor = ($major + 1) . ".0.0";
- return version_compare($version, $baseVersion, '>=') &&
- version_compare($version, $nextMajor, '<');
- }
+ return $this->handleCaretConstraint($constraint, $version);
}
- // Handle tilde (~)
+ // Handle tilde (~) - allows the specified precision of version
if (str_starts_with($constraint, '~')) {
- $baseVersion = substr($constraint, 1);
- $parts = explode('.', $baseVersion);
- $major = (int)($parts[0] ?? 0);
- $minor = (int)($parts[1] ?? 0);
- $nextMinor = "$major." . ($minor + 1) . ".0";
- $baseVersion = $this->ensure2Dots($baseVersion);
- return version_compare($version, $baseVersion, '>=') &&
- version_compare($version, $nextMinor, '<');
+ return $this->handleTildeConstraint($constraint, $version);
}
// Handle comparison operators (>, >=, <, <=, =)
@@ -192,20 +194,67 @@ class ComposerConstraintHelper // Renamed: More accurately reflects its role as
}
/**
- * Matches a single condition against a version.
+ * Handle caret (^) constraint matching.
+ *
+ * @param string $constraint Caret constraint (e.g., "^1.2.3")
+ * @param string $version Version to check
+ * @return bool True if the version satisfies the constraint
+ */
+ private function handleCaretConstraint(string $constraint, string $version): bool
+ {
+ $baseVersion = substr($constraint, 1);
+ $baseVersion = $this->ensure2Dots($baseVersion);
+
+ if (preg_match('/^0\.(\d+)/', $baseVersion, $matches)) {
+ // Special case for 0.x: only allow changes in patch version
+ $minor = (int)$matches[1];
+ $nextMinor = "0." . ($minor + 1) . ".0";
+ return version_compare($version, $baseVersion, '>=') &&
+ version_compare($version, $nextMinor, '<');
+ } elseif (preg_match('/^(\d+)/', $baseVersion, $matches)) {
+ // Normal case: allow everything that doesn't change the major version
+ $major = (int)$matches[0];
+ $nextMajor = ($major + 1) . ".0.0";
+ return version_compare($version, $baseVersion, '>=') &&
+ version_compare($version, $nextMajor, '<');
+ }
+
+ return false;
+ }
+
+ /**
+ * Handle tilde (~) constraint matching.
*
- * @param string $condition Single condition (e.g., '>=2.2').
- * @param string $version Version to check against.
+ * @param string $constraint Tilde constraint (e.g., "~1.2.3")
+ * @param string $version Version to check
+ * @return bool True if the version satisfies the constraint
+ */
+ private function handleTildeConstraint(string $constraint, string $version): bool
+ {
+ $baseVersion = substr($constraint, 1);
+ $parts = explode('.', $baseVersion);
+ $major = (int)($parts[0] ?? 0);
+ $minor = (int)($parts[1] ?? 0);
+ $nextMinor = "$major." . ($minor + 1) . ".0";
+ $baseVersion = $this->ensure2Dots($baseVersion);
+ return version_compare($version, $baseVersion, '>=') &&
+ version_compare($version, $nextMinor, '<');
+ }
+
+ /**
+ * Check if a version matches a single condition.
*
- * @return bool True if the version satisfies the condition, false otherwise.
+ * @param string $condition Single condition (e.g., '>=2.2')
+ * @param string $version Version to check
+ * @return bool True if the version satisfies the condition
*/
- private function matchesSingleCondition($condition, $version): bool
+ private function matchesSingleCondition(string $condition, string $version): bool
{
if (preg_match('/^([><=!]=?|<=|>=)\s*(\d+(\.\d+(\.\d+)?)?)$/', $condition, $matches)) {
$operator = $matches[1];
$versionToCompare = $matches[2];
- // Normalize single number to use proper version format
+ // Normalize version format
if (preg_match('/^\d+$/', $versionToCompare)) {
$versionToCompare .= '.0.0';
} elseif (preg_match('/^\d+\.\d+$/', $versionToCompare)) {
@@ -215,22 +264,15 @@ class ComposerConstraintHelper // Renamed: More accurately reflects its role as
return version_compare($version, $versionToCompare, $operator);
}
- // Additional checks here if needed...
-
return false;
}
/**
* Generate version(s) for a given composer version constraint.
*
- * For each part of a compound constraint (parts separated by "|"),
- * this method generates a candidate version that is either valid (if
- * $matches is true) or invalid (if $matches is false) according to that part.
- *
- * @param string $constraint A composer version constraint (may be compound).
- * @param bool $matches Whether to generate a version that matches the constraint.
- *
- * @return array|int An array of candidate versions or 0 if none could be generated.
+ * @param string $constraint A composer version constraint (may be compound)
+ * @param bool $matches Whether to generate a version that matches the constraint
+ * @return array|int An array of candidate versions or 0 if none could be generated
*/
public function generateVersionForConstraint(string $constraint, bool $matches = true): array|int
{
@@ -271,63 +313,75 @@ class ComposerConstraintHelper // Renamed: More accurately reflects its role as
return empty($results) ? 0 : $results;
}
+ /**
+ * Generate a candidate version for a single constraint.
+ *
+ * @param string $constraint Single constraint without AND/OR operators
+ * @param bool $matches Whether to generate a version that matches the constraint
+ * @return string Generated version
+ */
private function generateCandidateForSingleConstraint(string $constraint, bool $matches): string
{
- // Normalize hyphen ranges first
+ // Normalize hyphen ranges
$constraint = $this->normalizeHyphenRanges($constraint);
- // Remove spaces around comparison operators (>=, >, <, !=) in the constraint string
+ // Format standardization
$constraint = preg_replace('/([><!-]=?)\s*/', '$1', $constraint);
-
- // Convert "* >" to ">" and other normalizations.
$constraint = preg_replace('/\* ?([><!]=?)\s*/', '$1', $constraint);
$constraint = str_replace('. *', '.*', $constraint);
-
- // Split the constraint into parts if it contains multiple conditions (e.g., "^3 <3.30")
+ // Split the constraint if it contains multiple conditions
$parts = preg_split('/\s+/', trim($constraint));
if (count($parts) > 1) {
return $this->handleCompoundConstraint($parts, $matches);
}
- // Handle single constraint
+ // Extract operator and version
$operator = '';
if (preg_match('/^([<>=!~^]+)/', $constraint, $matchesOperator)) {
$operator = $matchesOperator[1];
$constraint = preg_replace('/^[<>=!~^]+\s*/', '', $constraint);
}
+ // Handle wildcards
if (strpos($constraint, '*') !== false) {
return $matches ? str_replace('*', '1', $constraint) : str_replace('*', '0', $constraint);
}
$base = $this->ensure2Dots($constraint);
+ // Generate version based on operator
switch ($operator) {
case '^':
$parts = explode('.', $base);
if ($matches) {
- return $base; // e.g., 3.0.0 for ^3
+ return $base; // Matches the base version
}
- return ((int)$parts[0] + 1) . '.0.0'; // e.g., 4.0.0 for non-matching ^3
+ return ((int)$parts[0] + 1) . '.0.0'; // Next major for non-matching
case '<':
if ($matches) {
return $this->decrementVersion($base); // One step below base
}
- return $base; // Equal to base is non-matching for strict <
+ return $base; // Base is non-matching for strict <
case '>=':
if ($matches) {
return $base; // Base satisfies >=
}
- return $this->decrementVersion($base); // Below base
+ return $this->decrementVersion($base); // Below base for non-matching
default:
- return $base; // Exact version
+ return $base; // Default to exact version
}
}
+ /**
+ * Handle compound constraints like "^3 <3.30".
+ *
+ * @param array $parts Parts of the compound constraint
+ * @param bool $matches Whether to generate a version that matches the constraint
+ * @return string Generated version
+ */
private function handleCompoundConstraint(array $parts, bool $matches): string
{
- // For "^3 <3.30"
$minVersion = null;
$maxVersion = null;
@@ -337,10 +391,10 @@ class ComposerConstraintHelper // Renamed: More accurately reflects its role as
$version = $this->ensure2Dots($match[2]);
if ($operator === '^') {
- $minVersion = $version; // e.g., 3.0.0
- $maxVersion = ((int)explode('.', $version)[0] + 1) . '.0.0'; // e.g., 4.0.0
+ $minVersion = $version;
+ $maxVersion = ((int)explode('.', $version)[0] + 1) . '.0.0';
} elseif ($operator === '<') {
- $maxVersion = $version; // e.g., 3.30.0
+ $maxVersion = $version;
} elseif ($operator === '>=') {
$minVersion = $version;
}
@@ -348,21 +402,23 @@ class ComposerConstraintHelper // Renamed: More accurately reflects its role as
}
if ($matches) {
- // Return a version in the range, e.g., minVersion or slightly above
- if ($maxVersion === null) {
-// dd($matches, $parts);
- $a = 1;
- }
- return $minVersion ?? $this->decrementVersion($maxVersion ?? '');
+ // Return a version in the range
+ return $minVersion ?? $this->decrementVersion($maxVersion ?? '1.0.0');
} else {
- // Return a version outside the range, e.g., below min or at/above max
+ // Return a version outside the range
if ($minVersion && $this->versionCompare($minVersion, $maxVersion) < 0) {
return $this->decrementVersion($minVersion); // Below min
}
- return $maxVersion; // At or above max
+ return $maxVersion ?? '999.999.999'; // At or above max
}
}
+ /**
+ * Decrement a version (e.g. 1.2.3 -> 1.2.2).
+ *
+ * @param string $version Version to decrement
+ * @return string Decremented version
+ */
private function decrementVersion(string $version): string
{
$parts = explode('.', $version);
@@ -378,11 +434,26 @@ class ComposerConstraintHelper // Renamed: More accurately reflects its role as
return implode('.', $parts);
}
+ /**
+ * Compare two versions (wrapper for version_compare).
+ *
+ * @param string $v1 First version
+ * @param string $v2 Second version
+ * @return int Result of comparison (-1, 0, 1)
+ */
private function versionCompare(string $v1, string $v2): int
{
return version_compare($v1, $v2);
}
+ /**
+ * Adjust a candidate version to satisfy an AND constraint.
+ *
+ * @param string $candidate Current candidate version
+ * @param string $constraint Additional constraint to satisfy
+ * @param bool $matches Whether to generate a version that matches the constraint
+ * @return string Adjusted version
+ */
private function adjustCandidateForAnd(string $candidate, string $constraint, bool $matches): string
{
$parts = explode('.', $candidate);
@@ -414,91 +485,72 @@ class ComposerConstraintHelper // Renamed: More accurately reflects its role as
}
/**
- * Generate a candidate version by replacing wildcards and handling constraints.
- *
- * Examples:
- * - "5.0.*@stable" becomes "5.0.1"
- * - "~5.5" becomes "5.5.0"
- * - "^7.2.1" becomes "7.2.1"
- * - ">3" becomes "3.0.0"
- * - "<4" becomes "4.0.0"
- *
- * @param string $constraintPart A single part of the constraint.
+ * Generate a valid candidate version for a constraint part.
*
- * @return string A candidate version string.
+ * @param string $constraintPart Constraint part
+ * @return string Valid candidate version
*/
- private function generateValidCandidate(string $constraintPart): string {
- // Remove comparison operators (>, <, >=, <=, =, ==, !=)
+ private function generateValidCandidate(string $constraintPart): string
+ {
+ // Remove operators and stability flags
$candidate = preg_replace('/^[<>=!~^]+\s*/', '', $constraintPart);
-
- // Remove any stability flags (e.g., "@stable", "@beta", etc.).
$candidate = preg_replace('/\@[a-z]+$/i', '', $candidate);
- // Replace 'x' with '*'.
+ // Normalize wildcards
$candidate = str_ireplace('x', '*', $candidate);
-
- // Replace wildcards with a candidate number.
$candidate = str_replace('*', '1', $candidate);
- // If it ends with a ., remove it...
+ // Remove trailing dot
if (str_ends_with($candidate, '.')) {
$candidate = substr($candidate, 0, -1);
}
- // If the candidate has only a major version, add minor and patch.
+ // Ensure proper version format (major.minor.patch)
if (preg_match('/^\d+$/', $candidate)) {
$candidate .= '.0.0';
- }
- // If the candidate has only major.minor, add patch.
- elseif (preg_match('/^\d+\.\d+$/', $candidate)) {
+ } elseif (preg_match('/^\d+\.\d+$/', $candidate)) {
$candidate .= '.0';
}
- // Ensure we have at least 3 parts in the version
+ // Ensure we have at least 3 parts
$parts = explode('.', $candidate);
while (count($parts) < 3) {
$parts[] = '0';
}
- $candidate = implode('.', $parts);
- return $candidate;
+ return implode('.', $parts);
}
/**
- * Generate a candidate version that is expected to be an alternative (valid or invalid)
- * relative to the constraint.
- *
- * This implementation simply modifies the major version number.
- *
- * @param string $constraintPart A single part of the constraint.
- * @param bool $shouldMatch If true, generate a candidate that matches; otherwise, one that doesn't.
+ * Generate an alternative candidate version.
*
- * @return string A candidate version string.
+ * @param string $constraintPart Constraint part
+ * @param bool $shouldMatch Whether the result should match the constraint
+ * @return string Alternative candidate version
*/
- private function generateAlternativeCandidate(string $constraintPart, bool $shouldMatch): string {
+ private function generateAlternativeCandidate(string $constraintPart, bool $shouldMatch): string
+ {
$validCandidate = $this->generateValidCandidate($constraintPart);
- // Try to extract the major, minor, and patch parts.
+ // Extract version parts
if (preg_match('/^(\d+)\.(\d+)\.(\d+)/', $validCandidate, $matches)) {
$major = (int)$matches[1];
$minor = $matches[2];
$patch = $matches[3];
if ($shouldMatch) {
- // If we want a matching version, return the valid candidate.
return $validCandidate;
} else {
- if (str_starts_with($constraintPart, '>') && str_starts_with($constraintPart, '>=') === false) {
+ // Special handling for '>' operator
+ if (str_starts_with($constraintPart, '>') && !str_starts_with($constraintPart, '>=')) {
++$patch;
return "$major.$minor.$patch";
}
- // To generate an invalid candidate, change the major version.
- // For example, if the candidate is "5.0.1", return "4.0.1".
+ // Generate non-matching version by changing major
$newMajor = ($major === 0) ? 1 : $major - 1;
return $newMajor . '.' . $minor . '.' . $patch;
}
} elseif (preg_match('/^(\d+)\.(\d+)/', $validCandidate, $matches)) {
- // Fallback if we only have major and minor parts.
$major = (int)$matches[1];
$minor = $matches[2];
@@ -510,22 +562,22 @@ class ComposerConstraintHelper // Renamed: More accurately reflects its role as
}
}
- // Final fallback: if unable to parse, return a hardcoded candidate.
-
+ // Fallback values
return $shouldMatch ? '1.0.0' : '0.0.1';
}
/**
- * Generate valid versions for any Composer constraint, including complex multi-constraints
+ * Generate valid versions for a composer constraint.
*
* @param string $constraint The composer constraint
- * @return array An array of valid versions that satisfy the constraint parts
+ * @return array Valid versions that satisfy the constraint
*/
- function generateValidVersionsForConstraint(string $constraint): array {
+ public function generateValidVersionsForConstraint(string $constraint): array
+ {
$versionParser = new VersionParser();
$validVersions = [];
- // Split by OR operators (both || and | are supported in Composer)
+ // Split by OR operators
$parts = preg_split('/\s*\|\|\s*|\s*\|\s*/', $constraint);
foreach ($parts as $part) {
@@ -536,171 +588,172 @@ class ComposerConstraintHelper // Renamed: More accurately reflects its role as
try {
$parsedConstraint = $versionParser->parseConstraints($part);
- $version = null;
+ $version = $this->generateVersionForPart($part);
- // Handle exact version
- if (preg_match('/^\d+\.\d+\.\d+$/', $part)) {
- $version = $part;
- }
-
- // Handle wildcard
- elseif (strpos($part, '*') !== false) {
- $version = str_replace('*', '0', $part);
- if (substr($part, -1) === '*') {
- $version = rtrim($version, '.');
- }
+ // Verify the generated version is valid for this constraint part
+ if ($version !== null) {
+ $normalizedVersion = $versionParser->normalize($version);
+ $versionConstraint = new Constraint('==', $normalizedVersion);
- // Ensure it has three segments
- $versionParts = explode('.', $version);
- while (count($versionParts) < 3) {
- $versionParts[] = '0';
+ if ($parsedConstraint->matches($versionConstraint)) {
+ $validVersions[$part] = $version;
}
- $version = implode('.', $versionParts);
- }
-
- // Handle ^X
- elseif (preg_match('/^\^(\d+)$/', $part, $matches)) {
- $version = $matches[1] . '.0.0';
- }
-
- // Handle ~X
- elseif (preg_match('/^~(\d+)$/', $part, $matches)) {
- $version = $matches[1] . '.0.0';
- }
-
- // Handle ^X.Y
- elseif (preg_match('/^\^(\d+\.\d+)$/', $part, $matches)) {
- $version = $matches[1] . '.0';
}
+ } catch (\Exception $e) {
+ // Skip parts that can't be parsed or matched
+ continue;
+ }
+ }
- // Handle ~X.Y
- elseif (preg_match('/^~(\d+\.\d+)$/', $part, $matches)) {
- $version = $matches[1] . '.0';
- }
+ return $validVersions;
+ }
- // Handle ^X.Y.Z
- elseif (preg_match('/^\^(\d+\.\d+\.\d+)$/', $part, $matches)) {
- $version = $matches[1];
- }
+ /**
+ * Generate a version for a specific constraint part.
+ *
+ * @param string $part Constraint part
+ * @return string|null Generated version or null if not possible
+ */
+ private function generateVersionForPart(string $part): ?string
+ {
+ // Handle exact version
+ if (preg_match('/^\d+\.\d+\.\d+$/', $part)) {
+ return $part;
+ }
- // Handle ~X.Y.Z
- elseif (preg_match('/^~(\d+\.\d+\.\d+)$/', $part, $matches)) {
- $version = $matches[1];
- }
+ // Handle wildcard
+ elseif (strpos($part, '*') !== false) {
+ return $this->handleWildcardPart($part);
+ }
- // Handle >=X.Y
- elseif (preg_match('/^>=(\d+(\.\d+(\.\d+)?)?)$/', $part, $matches)) {
- $version = $matches[1];
- $versionParts = explode('.', $version);
- while (count($versionParts) < 3) {
- $versionParts[] = '0';
- }
- $version = implode('.', $versionParts);
- }
+ // Handle ^X (caret with single digit)
+ elseif (preg_match('/^\^(\d+)$/', $part, $matches)) {
+ return $matches[1] . '.0.0';
+ }
- // Handle >X.Y
- elseif (preg_match('/^>(\d+(\.\d+(\.\d+)?)?)$/', $part, $matches)) {
- $version = $matches[1];
- $versionParts = explode('.', $version);
- while (count($versionParts) < 3) {
- $versionParts[] = '0';
- }
- $versionParts[2] = (int)$versionParts[2] + 1;
- $version = implode('.', $versionParts);
- }
+ // Handle ~X (tilde with single digit)
+ elseif (preg_match('/^~(\d+)$/', $part, $matches)) {
+ return $matches[1] . '.0.0';
+ }
- // Handle <X.Y
- elseif (preg_match('/^<(\d+(\.\d+(\.\d+)?)?)$/', $part, $matches)) {
- $version = $matches[1];
- $versionParts = explode('.', $version);
- while (count($versionParts) < 3) {
- $versionParts[] = '0';
- }
+ // Handle ^X.Y
+ elseif (preg_match('/^\^(\d+\.\d+)$/', $part, $matches)) {
+ return $matches[1] . '.0';
+ }
- if ($versionParts[2] > '0') {
- $versionParts[2] = (int)$versionParts[2] - 1;
- } elseif ($versionParts[1] > '0') {
- $versionParts[1] = (int)$versionParts[1] - 1;
- $versionParts[2] = '9';
- } else {
- $versionParts[0] = (int)$versionParts[0] - 1;
- $versionParts[1] = '9';
- $versionParts[2] = '9';
- }
- $version = implode('.', $versionParts);
- }
+ // Handle ~X.Y
+ elseif (preg_match('/^~(\d+\.\d+)$/', $part, $matches)) {
+ return $matches[1] . '.0';
+ }
- // Handle <=X.Y
- elseif (preg_match('/^<=(\d+(\.\d+(\.\d+)?)?)$/', $part, $matches)) {
- $version = $matches[1];
- $versionParts = explode('.', $version);
- while (count($versionParts) < 3) {
- $versionParts[] = '0';
- }
- $version = implode('.', $versionParts);
- }
+ // Handle ^X.Y.Z or ~X.Y.Z
+ elseif (preg_match('/^[\^~](\d+\.\d+\.\d+)$/', $part, $matches)) {
+ return $matches[1];
+ }
- // For complex constraints, test some candidates
- else {
- $testVersions = [];
+ // Handle >=X.Y
+ elseif (preg_match('/^>=(\d+(\.\d+(\.\d+)?)?)$/', $part, $matches)) {
+ return $this->normalizeVersionParts($matches[1]);
+ }
- // Extract version numbers from the constraint
- preg_match_all('/\d+(\.\d+)*/', $part, $matches);
+ // Handle >X.Y
+ elseif (preg_match('/^>(\d+(\.\d+(\.\d+)?)?)$/', $part, $matches)) {
+ return $this->handleGreaterThanPart($matches[1]);
+ }
- foreach ($matches[0] as $match) {
- $versionParts = explode('.', $match);
- while (count($versionParts) < 3) {
- $versionParts[] = '0';
- }
+ // Handle <X.Y or <=X.Y
+ elseif (preg_match('/^<[=]?(\d+(\.\d+(\.\d+)?)?)$/', $part, $matches)) {
+ return $this->handleLessThanPart($matches[1], $part);
+ }
- // Generate variants
- $base = implode('.', $versionParts);
- $testVersions[] = $base;
- $testVersions[] = $versionParts[0] . '.' . $versionParts[1] . '.' . ((int)$versionParts[2] + 1);
- $testVersions[] = $versionParts[0] . '.' . ((int)$versionParts[1] + 1) . '.0';
- }
+ // For complex constraints, use our main algorithm
+ else {
+ $versions = $this->generateVersionForConstraint($part);
+ return is_array($versions) ? reset($versions) : null;
+ }
+ }
- // Test each candidate
- foreach ($testVersions as $testVersion) {
- try {
- $normalizedVersion = $versionParser->normalize($testVersion);
- $versionConstraint = new Constraint('==', $normalizedVersion);
-
- if ($parsedConstraint->matches($versionConstraint)) {
- $version = $testVersion;
- break;
- }
- } catch (\Exception $e) {
- continue;
- }
- }
- }
+ /**
+ * Handle wildcard part of a constraint.
+ *
+ * @param string $part Wildcard constraint part
+ * @return string Generated version
+ */
+ private function handleWildcardPart(string $part): string
+ {
+ $version = str_replace('*', '0', $part);
+ if (substr($part, -1) === '*') {
+ $version = rtrim($version, '.');
+ }
- // Verify the generated version is valid for this constraint part
- if ($version !== null) {
- $normalizedVersion = $versionParser->normalize($version);
- $versionConstraint = new Constraint('==', $normalizedVersion);
+ return $this->normalizeVersionParts($version);
+ }
- if ($parsedConstraint->matches($versionConstraint)) {
- $validVersions[$part] = $version;
- }
- }
+ /**
+ * Handle greater than operator in constraint.
+ *
+ * @param string $version Version part of constraint
+ * @return string Version that satisfies >version
+ */
+ private function handleGreaterThanPart(string $version): string
+ {
+ $versionParts = explode('.', $this->normalizeVersionParts($version));
+ $versionParts[2] = (int)$versionParts[2] + 1;
+ return implode('.', $versionParts);
+ }
- } catch (\Exception $e) {
- // Skip parts that can't be parsed or matched
- continue;
+ /**
+ * Handle less than operator in constraint.
+ *
+ * @param string $version Version part of constraint
+ * @param string $fullPart Full constraint including operator
+ * @return string Version that satisfies <version
+ */
+ private function handleLessThanPart(string $version, string $fullPart): string
+ {
+ $versionParts = explode('.', $this->normalizeVersionParts($version));
+
+ if (str_starts_with($fullPart, '<') && !str_starts_with($fullPart, '<=')) {
+ // For strict < operator, we need to be less than the specified version
+ if ($versionParts[2] > '0') {
+ $versionParts[2] = (int)$versionParts[2] - 1;
+ } elseif ($versionParts[1] > '0') {
+ $versionParts[1] = (int)$versionParts[1] - 1;
+ $versionParts[2] = '9';
+ } else {
+ $versionParts[0] = (int)$versionParts[0] - 1;
+ $versionParts[1] = '9';
+ $versionParts[2] = '9';
}
}
- return $validVersions;
+ return implode('.', $versionParts);
}
/**
- * Converts "5 - 6" to ">= 5.0.0 < 6.0.0".
+ * Normalize version parts to have 3 segments.
+ *
+ * @param string $version Version to normalize
+ * @return string Normalized version
+ */
+ private function normalizeVersionParts(string $version): string
+ {
+ $versionParts = explode('.', $version);
+ while (count($versionParts) < 3) {
+ $versionParts[] = '0';
+ }
+ return implode('.', $versionParts);
+ }
+
+ /**
+ * Converts hyphen ranges (e.g., "5 - 6") to standard comparison operators.
+ *
+ * @param string $constraint Constraint with potential hyphen ranges
+ * @return string Normalized constraint
*/
private function normalizeHyphenRanges(string $constraint): string
{
- // Handle "X - Y" ranges by converting to ">=X <Y"
+ // Convert "X - Y" to ">=X <Y"
return preg_replace_callback(
'/(\d+(?:\.\d+)*)\s*-\s*(\d+(?:\.\d+)*)/',
function ($matches) {