From 18492a1f8471ef3b5c1ba2b5e0861fa164b8519b Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 12 Oct 2012 18:33:27 +0200 Subject: [PATCH] Remove Pool::getMaxId and the solver's reliance on it --- src/Composer/DependencyResolver/Decisions.php | 32 ++++++++++--------- src/Composer/DependencyResolver/Pool.php | 10 ------ .../Test/DependencyResolver/PoolTest.php | 16 ---------- 3 files changed, 17 insertions(+), 41 deletions(-) diff --git a/src/Composer/DependencyResolver/Decisions.php b/src/Composer/DependencyResolver/Decisions.php index 451cb5ff7..a9808e60e 100644 --- a/src/Composer/DependencyResolver/Decisions.php +++ b/src/Composer/DependencyResolver/Decisions.php @@ -29,12 +29,7 @@ class Decisions implements \Iterator, \Countable public function __construct($pool) { $this->pool = $pool; - - if (version_compare(PHP_VERSION, '5.3.4', '>=')) { - $this->decisionMap = new \SplFixedArray($this->pool->getMaxId() + 1); - } else { - $this->decisionMap = array_fill(0, $this->pool->getMaxId() + 1, 0); - } + $this->decisionMap = array(); } public function decide($literal, $level, $why) @@ -51,8 +46,8 @@ class Decisions implements \Iterator, \Countable $packageId = abs($literal); return ( - $literal > 0 && $this->decisionMap[$packageId] > 0 || - $literal < 0 && $this->decisionMap[$packageId] < 0 + $literal > 0 && isset($this->decisionMap[$packageId]) && $this->decisionMap[$packageId] > 0 || + $literal < 0 && isset($this->decisionMap[$packageId]) && $this->decisionMap[$packageId] < 0 ); } @@ -61,29 +56,36 @@ class Decisions implements \Iterator, \Countable $packageId = abs($literal); return ( - ($this->decisionMap[$packageId] > 0 && $literal < 0) || - ($this->decisionMap[$packageId] < 0 && $literal > 0) + (isset($this->decisionMap[$packageId]) && $this->decisionMap[$packageId] > 0 && $literal < 0) || + (isset($this->decisionMap[$packageId]) && $this->decisionMap[$packageId] < 0 && $literal > 0) ); } public function decided($literalOrPackageId) { - return $this->decisionMap[abs($literalOrPackageId)] != 0; + return !empty($this->decisionMap[abs($literalOrPackageId)]); } public function undecided($literalOrPackageId) { - return $this->decisionMap[abs($literalOrPackageId)] == 0; + return empty($this->decisionMap[abs($literalOrPackageId)]); } public function decidedInstall($literalOrPackageId) { - return $this->decisionMap[abs($literalOrPackageId)] > 0; + $packageId = abs($literalOrPackageId); + + return isset($this->decisionMap[$packageId]) && $this->decisionMap[$packageId] > 0; } public function decisionLevel($literalOrPackageId) { - return abs($this->decisionMap[abs($literalOrPackageId)]); + $packageId = abs($literalOrPackageId); + if (isset($this->decisionMap[$packageId])) { + return abs($this->decisionMap[$packageId]); + } + + return 0; } public function decisionRule($literalOrPackageId) @@ -179,7 +181,7 @@ class Decisions implements \Iterator, \Countable { $packageId = abs($literal); - $previousDecision = $this->decisionMap[$packageId]; + $previousDecision = isset($this->decisionMap[$packageId]) ? $this->decisionMap[$packageId] : null; if ($previousDecision != 0) { $literalString = $this->pool->literalToString($literal); $package = $this->pool->literalToPackage($literal); diff --git a/src/Composer/DependencyResolver/Pool.php b/src/Composer/DependencyResolver/Pool.php index 57e1b245c..2857924c4 100644 --- a/src/Composer/DependencyResolver/Pool.php +++ b/src/Composer/DependencyResolver/Pool.php @@ -206,16 +206,6 @@ class Pool return $this->packages[$id - 1]; } - /** - * Retrieves the highest id assigned to a package in this pool - * - * @return int Highest package id - */ - public function getMaxId() - { - return $this->id - 1; - } - /** * Searches all packages providing the given package name and match the constraint * diff --git a/tests/Composer/Test/DependencyResolver/PoolTest.php b/tests/Composer/Test/DependencyResolver/PoolTest.php index 7c6618582..aa38fa31d 100644 --- a/tests/Composer/Test/DependencyResolver/PoolTest.php +++ b/tests/Composer/Test/DependencyResolver/PoolTest.php @@ -129,20 +129,4 @@ class PoolTest extends TestCase $this->assertEquals(array(), $pool->whatProvides('foo')); } - - public function testGetMaxId() - { - $pool = new Pool; - $repository = new ArrayRepository; - $firstPackage = $this->getPackage('foo', '1'); - $secondPackage = $this->getPackage('foo1', '1'); - - $this->assertEquals(0, $pool->getMaxId()); - - $repository->addPackage($firstPackage); - $repository->addPackage($secondPackage); - $pool->addRepository($repository); - - $this->assertEquals(2, $pool->getMaxId()); - } }