@@ -48,11 +48,6 @@ class PoolOptimizer
48
48
*/
49
49
private $ conflictConstraintsPerPackage = [];
50
50
51
- /**
52
- * @var array<int, true>
53
- */
54
- private $ packagesToRemove = [];
55
-
56
51
/**
57
52
* @var array<int, BasePackage[]>
58
53
*/
@@ -74,8 +69,6 @@ public function optimize(Request $request, Pool $pool): Pool
74
69
75
70
$ this ->optimizeByIdenticalDependencies ($ request , $ pool );
76
71
77
- $ this ->optimizeImpossiblePackagesAway ($ request , $ pool );
78
-
79
72
$ optimizedPool = $ this ->applyRemovalsToPool ($ pool );
80
73
81
74
// No need to run this recursively at the moment
@@ -86,7 +79,6 @@ public function optimize(Request $request, Pool $pool): Pool
86
79
$ this ->irremovablePackages = [];
87
80
$ this ->requireConstraintsPerPackage = [];
88
81
$ this ->conflictConstraintsPerPackage = [];
89
- $ this ->packagesToRemove = [];
90
82
$ this ->aliasesPerPackage = [];
91
83
$ this ->removedVersionsByPackage = [];
92
84
@@ -145,6 +137,11 @@ private function prepare(Request $request, Pool $pool): void
145
137
146
138
private function markPackageIrremovable (BasePackage $ package ): void
147
139
{
140
+ // Already marked to keep
141
+ if (isset ($ this ->irremovablePackages [$ package ->id ])) {
142
+ return ;
143
+ }
144
+
148
145
$ this ->irremovablePackages [$ package ->id ] = true ;
149
146
if ($ package instanceof AliasPackage) {
150
147
// recursing here so aliasesPerPackage for the aliasOf can be checked
@@ -166,7 +163,7 @@ private function applyRemovalsToPool(Pool $pool): Pool
166
163
$ packages = [];
167
164
$ removedVersions = [];
168
165
foreach ($ pool ->getPackages () as $ package ) {
169
- if (! isset ($ this ->packagesToRemove [$ package ->id ])) {
166
+ if (isset ($ this ->irremovablePackages [$ package ->id ])) {
170
167
$ packages [] = $ package ;
171
168
} else {
172
169
$ removedVersions [$ package ->getName ()][$ package ->getVersion ()] = $ package ->getPrettyVersion ();
@@ -191,8 +188,6 @@ private function optimizeByIdenticalDependencies(Request $request, Pool $pool):
191
188
continue ;
192
189
}
193
190
194
- $ this ->markPackageForRemoval ($ package ->id );
195
-
196
191
$ dependencyHash = $ this ->calculateDependencyHash ($ package );
197
192
198
193
foreach ($ package ->getNames (false ) as $ packageName ) {
@@ -240,7 +235,7 @@ private function optimizeByIdenticalDependencies(Request $request, Pool $pool):
240
235
foreach ($ constraintGroup as $ packages ) {
241
236
// Only one package in this constraint group has the same requirements, we're not allowed to remove that package
242
237
if (1 === \count ($ packages )) {
243
- $ this ->keepPackage ($ packages [0 ], $ identicalDefinitionsPerPackage , $ packageIdenticalDefinitionLookup );
238
+ $ this ->keepPackageWithinDependencyGroups ($ packages [0 ], $ identicalDefinitionsPerPackage , $ packageIdenticalDefinitionLookup );
244
239
continue ;
245
240
}
246
241
@@ -253,7 +248,7 @@ private function optimizeByIdenticalDependencies(Request $request, Pool $pool):
253
248
}
254
249
255
250
foreach ($ this ->policy ->selectPreferredPackages ($ pool , $ literals ) as $ preferredLiteral ) {
256
- $ this ->keepPackage ($ pool ->literalToPackage ($ preferredLiteral ), $ identicalDefinitionsPerPackage , $ packageIdenticalDefinitionLookup );
251
+ $ this ->keepPackageWithinDependencyGroups ($ pool ->literalToPackage ($ preferredLiteral ), $ identicalDefinitionsPerPackage , $ packageIdenticalDefinitionLookup );
257
252
}
258
253
}
259
254
}
@@ -300,33 +295,18 @@ private function calculateDependencyHash(BasePackage $package): string
300
295
return $ hash ;
301
296
}
302
297
303
- private function markPackageForRemoval (int $ id ): void
304
- {
305
- // We are not allowed to remove packages if they have been marked as irremovable
306
- if (isset ($ this ->irremovablePackages [$ id ])) {
307
- throw new \LogicException ('Attempted removing a package which was previously marked irremovable ' );
308
- }
309
-
310
- $ this ->packagesToRemove [$ id ] = true ;
311
- }
312
-
313
298
/**
314
299
* @param array<string, array<string, array<string, list<BasePackage>>>> $identicalDefinitionsPerPackage
315
300
* @param array<int, array<string, array{groupHash: string, dependencyHash: string}>> $packageIdenticalDefinitionLookup
316
301
*/
317
- private function keepPackage (BasePackage $ package , array $ identicalDefinitionsPerPackage , array $ packageIdenticalDefinitionLookup ): void
302
+ private function keepPackageWithinDependencyGroups (BasePackage $ package , array $ identicalDefinitionsPerPackage , array $ packageIdenticalDefinitionLookup ): void
318
303
{
319
- // Already marked to keep
320
- if (!isset ($ this ->packagesToRemove [$ package ->id ])) {
321
- return ;
322
- }
323
-
324
- unset($ this ->packagesToRemove [$ package ->id ]);
304
+ $ this ->markPackageIrremovable ($ package );
325
305
326
306
if ($ package instanceof AliasPackage) {
327
307
// recursing here so aliasesPerPackage for the aliasOf can be checked
328
308
// and all its aliases marked to be kept as well
329
- $ this ->keepPackage ($ package ->getAliasOf (), $ identicalDefinitionsPerPackage , $ packageIdenticalDefinitionLookup );
309
+ $ this ->keepPackageWithinDependencyGroups ($ package ->getAliasOf (), $ identicalDefinitionsPerPackage , $ packageIdenticalDefinitionLookup );
330
310
}
331
311
332
312
// record all the versions of the package group so we can list them later in Problem output
@@ -345,8 +325,6 @@ private function keepPackage(BasePackage $package, array $identicalDefinitionsPe
345
325
346
326
if (isset ($ this ->aliasesPerPackage [$ package ->id ])) {
347
327
foreach ($ this ->aliasesPerPackage [$ package ->id ] as $ aliasPackage ) {
348
- unset($ this ->packagesToRemove [$ aliasPackage ->id ]);
349
-
350
328
// record all the versions of the package group so we can list them later in Problem output
351
329
foreach ($ aliasPackage ->getNames (false ) as $ name ) {
352
330
if (isset ($ packageIdenticalDefinitionLookup [$ aliasPackage ->id ][$ name ])) {
@@ -364,70 +342,6 @@ private function keepPackage(BasePackage $package, array $identicalDefinitionsPe
364
342
}
365
343
}
366
344
367
- /**
368
- * Use the list of locked packages to constrain the loaded packages
369
- * This will reduce packages with significant numbers of historical versions to a smaller number
370
- * and reduce the resulting rule set that is generated
371
- */
372
- private function optimizeImpossiblePackagesAway (Request $ request , Pool $ pool ): void
373
- {
374
- if (count ($ request ->getLockedPackages ()) === 0 ) {
375
- return ;
376
- }
377
-
378
- $ packageIndex = [];
379
-
380
- foreach ($ pool ->getPackages () as $ package ) {
381
- $ id = $ package ->id ;
382
-
383
- // Do not remove irremovable packages
384
- if (isset ($ this ->irremovablePackages [$ id ])) {
385
- continue ;
386
- }
387
- // Do not remove a package aliased by another package, nor aliases
388
- if (isset ($ this ->aliasesPerPackage [$ id ]) || $ package instanceof AliasPackage) {
389
- continue ;
390
- }
391
- // Do not remove locked packages
392
- if ($ request ->isFixedPackage ($ package ) || $ request ->isLockedPackage ($ package )) {
393
- continue ;
394
- }
395
-
396
- $ packageIndex [$ package ->getName ()][$ package ->id ] = $ package ;
397
- }
398
-
399
- foreach ($ request ->getLockedPackages () as $ package ) {
400
- // If this locked package is no longer required by root or anything in the pool, it may get uninstalled so do not apply its requirements
401
- // In a case where a requirement WERE to appear in the pool by a package that would not be used, it would've been unlocked and so not filtered still
402
- $ isUnusedPackage = true ;
403
- foreach ($ package ->getNames (false ) as $ packageName ) {
404
- if (isset ($ this ->requireConstraintsPerPackage [$ packageName ])) {
405
- $ isUnusedPackage = false ;
406
- break ;
407
- }
408
- }
409
-
410
- if ($ isUnusedPackage ) {
411
- continue ;
412
- }
413
-
414
- foreach ($ package ->getRequires () as $ link ) {
415
- $ require = $ link ->getTarget ();
416
- if (!isset ($ packageIndex [$ require ])) {
417
- continue ;
418
- }
419
-
420
- $ linkConstraint = $ link ->getConstraint ();
421
- foreach ($ packageIndex [$ require ] as $ id => $ requiredPkg ) {
422
- if (false === CompilingMatcher::match ($ linkConstraint , Constraint::OP_EQ , $ requiredPkg ->getVersion ())) {
423
- $ this ->markPackageForRemoval ($ id );
424
- unset($ packageIndex [$ require ][$ id ]);
425
- }
426
- }
427
- }
428
- }
429
- }
430
-
431
345
/**
432
346
* Disjunctive require constraints need to be considered in their own group. E.g. "^2.14 || ^3.3" needs to generate
433
347
* two require constraint groups in order for us to keep the best matching package for "^2.14" AND "^3.3" as otherwise, we'd
0 commit comments