Browse Source

Remove unnecessary pool reference from rules

Nils Adermann 10 years ago
parent
commit
26598c4a9a

+ 1 - 1
src/Composer/DependencyResolver/Problem.php

@@ -130,7 +130,7 @@ class Problem
                 $messages[] = $this->jobToText($job);
             } elseif ($rule) {
                 if ($rule instanceof Rule) {
-                    $messages[] = $rule->getPrettyString($installedMap);
+                    $messages[] = $rule->getPrettyString($this->pool, $installedMap);
                 }
             }
         }

+ 15 - 21
src/Composer/DependencyResolver/Rule.php

@@ -31,16 +31,10 @@ class Rule
 
     /**
      * The literals this rule consists of.
-     *
-     * Each element is a package id either positive for installation or
-     * negative meaning removal.
-     *
      * @var array
      */
     public $literals;
 
-    protected $pool;
-
     protected $disabled;
     protected $type;
     protected $id;
@@ -51,10 +45,8 @@ class Rule
 
     protected $ruleHash;
 
-    public function __construct(Pool $pool, array $literals, $reason, $reasonData, $job = null)
+    public function __construct(array $literals, $reason, $reasonData, $job = null)
     {
-        $this->pool = $pool;
-
         // sort all packages ascending by id
         sort($literals);
 
@@ -174,14 +166,14 @@ class Rule
         return 1 === count($this->literals);
     }
 
-    public function getPrettyString(array $installedMap = array())
+    public function getPrettyString(Pool $pool, array $installedMap = array())
     {
         $ruleText = '';
         foreach ($this->literals as $i => $literal) {
             if ($i != 0) {
                 $ruleText .= '|';
             }
-            $ruleText .= $this->pool->literalToPrettyString($literal, $installedMap);
+            $ruleText .= $pool->literalToPrettyString($literal, $installedMap);
         }
 
         switch ($this->reason) {
@@ -195,24 +187,24 @@ class Rule
                 return "Remove command rule ($ruleText)";
 
             case self::RULE_PACKAGE_CONFLICT:
-                $package1 = $this->pool->literalToPackage($this->literals[0]);
-                $package2 = $this->pool->literalToPackage($this->literals[1]);
+                $package1 = $pool->literalToPackage($this->literals[0]);
+                $package2 = $pool->literalToPackage($this->literals[1]);
 
-                return $package1->getPrettyString().' conflicts with '.$this->formatPackagesUnique(array($package2)).'.';
+                return $package1->getPrettyString().' conflicts with '.$this->formatPackagesUnique($pool, array($package2)).'.';
 
             case self::RULE_PACKAGE_REQUIRES:
                 $literals = $this->literals;
                 $sourceLiteral = array_shift($literals);
-                $sourcePackage = $this->pool->literalToPackage($sourceLiteral);
+                $sourcePackage = $pool->literalToPackage($sourceLiteral);
 
                 $requires = array();
                 foreach ($literals as $literal) {
-                    $requires[] = $this->pool->literalToPackage($literal);
+                    $requires[] = $pool->literalToPackage($literal);
                 }
 
                 $text = $this->reasonData->getPrettyString($sourcePackage);
                 if ($requires) {
-                    $text .= ' -> satisfiable by ' . $this->formatPackagesUnique($requires) . '.';
+                    $text .= ' -> satisfiable by ' . $this->formatPackagesUnique($pool, $requires) . '.';
                 } else {
                     $targetName = $this->reasonData->getTarget();
 
@@ -239,22 +231,24 @@ class Rule
             case self::RULE_INSTALLED_PACKAGE_OBSOLETES:
                 return $ruleText;
             case self::RULE_PACKAGE_SAME_NAME:
-                return 'Can only install one of: ' . $this->formatPackagesUnique($this->literals) . '.';
+                return 'Can only install one of: ' . $this->formatPackagesUnique($pool, $this->literals) . '.';
             case self::RULE_PACKAGE_IMPLICIT_OBSOLETES:
                 return $ruleText;
             case self::RULE_LEARNED:
                 return 'Conclusion: '.$ruleText;
             case self::RULE_PACKAGE_ALIAS:
                 return $ruleText;
+            default:
+                return '('.$ruleText.')';
         }
     }
 
-    protected function formatPackagesUnique(array $packages)
+    protected function formatPackagesUnique($pool, array $packages)
     {
         $prepared = array();
         foreach ($packages as $package) {
             if (!is_object($package)) {
-                $package = $this->pool->literalToPackage($package);
+                $package = $pool->literalToPackage($package);
             }
             $prepared[$package->getName()]['name'] = $package->getPrettyName();
             $prepared[$package->getName()]['versions'][$package->getVersion()] = $package->getPrettyVersion();
@@ -279,7 +273,7 @@ class Rule
             if ($i != 0) {
                 $result .= '|';
             }
-            $result .= $this->pool->literalToString($literal);
+            $result .= $literal;
         }
 
         $result .= ')';

+ 7 - 2
src/Composer/DependencyResolver/RuleSet.php

@@ -145,17 +145,22 @@ class RuleSet implements \IteratorAggregate, \Countable
         return false;
     }
 
-    public function __toString()
+    public function getPrettyString(Pool $pool = null)
     {
         $string = "\n";
         foreach ($this->rules as $type => $rules) {
             $string .= str_pad(self::$types[$type], 8, ' ') . ": ";
             foreach ($rules as $rule) {
-                $string .= $rule."\n";
+                $string .= ($pool ? $rule->getPrettyString($pool) : $rule)."\n";
             }
             $string .= "\n\n";
         }
 
         return $string;
     }
+
+    public function __toString()
+    {
+        return $this->getPrettyString(null);
+    }
 }

+ 4 - 4
src/Composer/DependencyResolver/RuleSetGenerator.php

@@ -61,7 +61,7 @@ class RuleSetGenerator
             $literals[] = $provider->id;
         }
 
-        return new Rule($this->pool, $literals, $reason, $reasonData);
+        return new Rule($literals, $reason, $reasonData);
     }
 
     /**
@@ -83,7 +83,7 @@ class RuleSetGenerator
             $literals[] = $package->id;
         }
 
-        return new Rule($this->pool, $literals, $reason, $job['packageName'], $job);
+        return new Rule($literals, $reason, $job['packageName'], $job);
     }
 
     /**
@@ -99,7 +99,7 @@ class RuleSetGenerator
      */
     protected function createRemoveRule(PackageInterface $package, $reason, $job)
     {
-        return new Rule($this->pool, array(-$package->id), $reason, $job['packageName'], $job);
+        return new Rule(array(-$package->id), $reason, $job['packageName'], $job);
     }
 
     /**
@@ -123,7 +123,7 @@ class RuleSetGenerator
             return null;
         }
 
-        return new Rule($this->pool, array(-$issuer->id, -$provider->id), $reason, $reasonData);
+        return new Rule(array(-$issuer->id, -$provider->id), $reason, $reasonData);
     }
 
     /**

+ 2 - 2
src/Composer/DependencyResolver/Solver.php

@@ -155,7 +155,7 @@ class Solver
 
                     if (!$this->pool->whatProvides($job['packageName'], $job['constraint'])) {
                         $problem = new Problem($this->pool);
-                        $problem->addRule(new Rule($this->pool, array(), null, null, $job));
+                        $problem->addRule(new Rule(array(), null, null, $job));
                         $this->problems[] = $problem;
                     }
                     break;
@@ -441,7 +441,7 @@ class Solver
             );
         }
 
-        $newRule = new Rule($this->pool, $learnedLiterals, Rule::RULE_LEARNED, $why);
+        $newRule = new Rule($learnedLiterals, Rule::RULE_LEARNED, $why);
 
         return array($learnedLiterals[0], $ruleLevel, $newRule, $why);
     }

+ 3 - 3
tests/Composer/Test/DependencyResolver/RuleSetIteratorTest.php

@@ -27,11 +27,11 @@ class RuleSetIteratorTest extends \PHPUnit_Framework_TestCase
 
         $this->rules = array(
             RuleSet::TYPE_JOB => array(
-                new Rule($this->pool, array(), 'job1', null),
-                new Rule($this->pool, array(), 'job2', null),
+                new Rule(array(), 'job1', null),
+                new Rule(array(), 'job2', null),
             ),
             RuleSet::TYPE_LEARNED => array(
-                new Rule($this->pool, array(), 'update1', null),
+                new Rule(array(), 'update1', null),
             ),
             RuleSet::TYPE_PACKAGE => array(),
         );

+ 16 - 16
tests/Composer/Test/DependencyResolver/RuleSetTest.php

@@ -32,11 +32,11 @@ class RuleSetTest extends TestCase
         $rules = array(
             RuleSet::TYPE_PACKAGE => array(),
             RuleSet::TYPE_JOB => array(
-                new Rule($this->pool, array(), 'job1', null),
-                new Rule($this->pool, array(), 'job2', null),
+                new Rule(array(), 'job1', null),
+                new Rule(array(), 'job2', null),
             ),
             RuleSet::TYPE_LEARNED => array(
-                new Rule($this->pool, array(), 'update1', null),
+                new Rule(array(), 'update1', null),
             ),
         );
 
@@ -56,15 +56,15 @@ class RuleSetTest extends TestCase
     {
         $ruleSet = new RuleSet;
 
-        $ruleSet->add(new Rule($this->pool, array(), 'job1', null), 7);
+        $ruleSet->add(new Rule(array(), 'job1', null), 7);
     }
 
     public function testCount()
     {
         $ruleSet = new RuleSet;
 
-        $ruleSet->add(new Rule($this->pool, array(), 'job1', null), RuleSet::TYPE_JOB);
-        $ruleSet->add(new Rule($this->pool, array(), 'job2', null), RuleSet::TYPE_JOB);
+        $ruleSet->add(new Rule(array(), 'job1', null), RuleSet::TYPE_JOB);
+        $ruleSet->add(new Rule(array(), 'job2', null), RuleSet::TYPE_JOB);
 
         $this->assertEquals(2, $ruleSet->count());
     }
@@ -73,7 +73,7 @@ class RuleSetTest extends TestCase
     {
         $ruleSet = new RuleSet;
 
-        $rule = new Rule($this->pool, array(), 'job1', null);
+        $rule = new Rule(array(), 'job1', null);
         $ruleSet->add($rule, RuleSet::TYPE_JOB);
 
         $this->assertSame($rule, $ruleSet->ruleById[0]);
@@ -83,8 +83,8 @@ class RuleSetTest extends TestCase
     {
         $ruleSet = new RuleSet;
 
-        $rule1 = new Rule($this->pool, array(), 'job1', null);
-        $rule2 = new Rule($this->pool, array(), 'job1', null);
+        $rule1 = new Rule(array(), 'job1', null);
+        $rule2 = new Rule(array(), 'job1', null);
         $ruleSet->add($rule1, RuleSet::TYPE_JOB);
         $ruleSet->add($rule2, RuleSet::TYPE_LEARNED);
 
@@ -98,8 +98,8 @@ class RuleSetTest extends TestCase
     public function testGetIteratorFor()
     {
         $ruleSet = new RuleSet;
-        $rule1 = new Rule($this->pool, array(), 'job1', null);
-        $rule2 = new Rule($this->pool, array(), 'job1', null);
+        $rule1 = new Rule(array(), 'job1', null);
+        $rule2 = new Rule(array(), 'job1', null);
 
         $ruleSet->add($rule1, RuleSet::TYPE_JOB);
         $ruleSet->add($rule2, RuleSet::TYPE_LEARNED);
@@ -112,8 +112,8 @@ class RuleSetTest extends TestCase
     public function testGetIteratorWithout()
     {
         $ruleSet = new RuleSet;
-        $rule1 = new Rule($this->pool, array(), 'job1', null);
-        $rule2 = new Rule($this->pool, array(), 'job1', null);
+        $rule1 = new Rule(array(), 'job1', null);
+        $rule2 = new Rule(array(), 'job1', null);
 
         $ruleSet->add($rule1, RuleSet::TYPE_JOB);
         $ruleSet->add($rule2, RuleSet::TYPE_LEARNED);
@@ -155,7 +155,7 @@ class RuleSetTest extends TestCase
         $this->assertFalse($ruleSet->containsEqual($rule3));
     }
 
-    public function testToString()
+    public function testPrettyString()
     {
         $repo = new ArrayRepository;
         $repo->addPackage($p = $this->getPackage('foo', '2.1'));
@@ -163,11 +163,11 @@ class RuleSetTest extends TestCase
 
         $ruleSet = new RuleSet;
         $literal = $p->getId();
-        $rule = new Rule($this->pool, array($literal), 'job1', null);
+        $rule = new Rule(array($literal), 'job1', null);
 
         $ruleSet->add($rule, RuleSet::TYPE_JOB);
 
-        $this->assertContains('JOB     : (+foo-2.1.0.0)', $ruleSet->__toString());
+        $this->assertContains('JOB     : (install foo 2.1)', $ruleSet->getPrettyString($this->pool));
     }
 
     private function getRuleMock()

+ 16 - 16
tests/Composer/Test/DependencyResolver/RuleTest.php

@@ -28,14 +28,14 @@ class RuleTest extends TestCase
 
     public function testGetHash()
     {
-        $rule = new Rule($this->pool, array(123), 'job1', null);
+        $rule = new Rule(array(123), 'job1', null);
 
         $this->assertEquals(substr(md5('123'), 0, 5), $rule->getHash());
     }
 
     public function testSetAndGetId()
     {
-        $rule = new Rule($this->pool, array(), 'job1', null);
+        $rule = new Rule(array(), 'job1', null);
         $rule->setId(666);
 
         $this->assertEquals(666, $rule->getId());
@@ -43,31 +43,31 @@ class RuleTest extends TestCase
 
     public function testEqualsForRulesWithDifferentHashes()
     {
-        $rule = new Rule($this->pool, array(1, 2), 'job1', null);
-        $rule2 = new Rule($this->pool, array(1, 3), 'job1', null);
+        $rule = new Rule(array(1, 2), 'job1', null);
+        $rule2 = new Rule(array(1, 3), 'job1', null);
 
         $this->assertFalse($rule->equals($rule2));
     }
 
     public function testEqualsForRulesWithDifferLiteralsQuantity()
     {
-        $rule = new Rule($this->pool, array(1, 12), 'job1', null);
-        $rule2 = new Rule($this->pool, array(1), 'job1', null);
+        $rule = new Rule(array(1, 12), 'job1', null);
+        $rule2 = new Rule(array(1), 'job1', null);
 
         $this->assertFalse($rule->equals($rule2));
     }
 
     public function testEqualsForRulesWithSameLiterals()
     {
-        $rule = new Rule($this->pool, array(1, 12), 'job1', null);
-        $rule2 = new Rule($this->pool, array(1, 12), 'job1', null);
+        $rule = new Rule(array(1, 12), 'job1', null);
+        $rule2 = new Rule(array(1, 12), 'job1', null);
 
         $this->assertTrue($rule->equals($rule2));
     }
 
     public function testSetAndGetType()
     {
-        $rule = new Rule($this->pool, array(), 'job1', null);
+        $rule = new Rule(array(), 'job1', null);
         $rule->setType('someType');
 
         $this->assertEquals('someType', $rule->getType());
@@ -75,7 +75,7 @@ class RuleTest extends TestCase
 
     public function testEnable()
     {
-        $rule = new Rule($this->pool, array(), 'job1', null);
+        $rule = new Rule(array(), 'job1', null);
         $rule->disable();
         $rule->enable();
 
@@ -85,7 +85,7 @@ class RuleTest extends TestCase
 
     public function testDisable()
     {
-        $rule = new Rule($this->pool, array(), 'job1', null);
+        $rule = new Rule(array(), 'job1', null);
         $rule->enable();
         $rule->disable();
 
@@ -95,22 +95,22 @@ class RuleTest extends TestCase
 
     public function testIsAssertions()
     {
-        $rule = new Rule($this->pool, array(1, 12), 'job1', null);
-        $rule2 = new Rule($this->pool, array(1), 'job1', null);
+        $rule = new Rule(array(1, 12), 'job1', null);
+        $rule2 = new Rule(array(1), 'job1', null);
 
         $this->assertFalse($rule->isAssertion());
         $this->assertTrue($rule2->isAssertion());
     }
 
-    public function testToString()
+    public function testPrettyString()
     {
         $repo = new ArrayRepository;
         $repo->addPackage($p1 = $this->getPackage('foo', '2.1'));
         $repo->addPackage($p2 = $this->getPackage('baz', '1.1'));
         $this->pool->addRepository($repo);
 
-        $rule = new Rule($this->pool, array($p1->getId(), -$p2->getId()), 'job1', null);
+        $rule = new Rule(array($p1->getId(), -$p2->getId()), 'job1', null);
 
-        $this->assertEquals('(-baz-1.1.0.0|+foo-2.1.0.0)', $rule->__toString());
+        $this->assertEquals('(don\'t install baz 1.1|install foo 2.1)', $rule->getPrettyString($this->pool));
     }
 }