浏览代码

Use constants with names for bitfield offsets

Nils Adermann 10 年之前
父节点
当前提交
f535542fca
共有 1 个文件被更改,包括 16 次插入12 次删除
  1. 16 12
      src/Composer/DependencyResolver/Rule.php

+ 16 - 12
src/Composer/DependencyResolver/Rule.php

@@ -29,6 +29,10 @@ class Rule
     const RULE_LEARNED = 12;
     const RULE_PACKAGE_ALIAS = 13;
 
+    const BITFIELD_TYPE = 0;
+    const BITFIELD_REASON = 8;
+    const BITFIELD_DISABLED = 16;
+
     /**
      * READ-ONLY: The literals this rule consists of.
      * @var array
@@ -50,7 +54,9 @@ class Rule
             $this->job = $job;
         }
 
-        $this->bitfield = (0 << 16) | ($reason << 8) | (255 << 0);
+        $this->bitfield = (0 << self::BITFIELD_DISABLED) |
+            ($reason << self::BITFIELD_REASON) |
+            (255 << self::BITFIELD_TYPE);
     }
 
     public function getHash()
@@ -66,7 +72,7 @@ class Rule
 
     public function getReason()
     {
-        return $this->getBitfield(1);
+        return $this->getBitfield(self::BITFIELD_REASON);
     }
 
     public function getReasonData()
@@ -110,32 +116,32 @@ class Rule
 
     public function setType($type)
     {
-        return $this->setBitfield(0, $type);
+        return $this->setBitfield(self::BITFIELD_TYPE, $type);
     }
 
     public function getType()
     {
-        return $this->getBitfield(0);
+        return $this->getBitfield(self::BITFIELD_TYPE);
     }
 
     public function disable()
     {
-        return $this->setBitfield(2, 1);
+        return $this->setBitfield(self::BITFIELD_DISABLED, 1);
     }
 
     public function enable()
     {
-        return $this->setBitfield(2, 0);
+        return $this->setBitfield(self::BITFIELD_DISABLED, 0);
     }
 
     public function isDisabled()
     {
-        return (bool) $this->getBitfield(2);
+        return (bool) $this->getBitfield(self::BITFIELD_DISABLED);
     }
 
     public function isEnabled()
     {
-        return !$this->getBitfield(2);
+        return !$this->getBitfield(self::BITFIELD_DISABLED);
     }
 
     /**
@@ -253,15 +259,13 @@ class Rule
         return implode(', ', $prepared);
     }
 
-    private function getBitfield($var)
+    private function getBitfield($offset)
     {
-        $offset = 8*$var;
         return ($this->bitfield & (255 << $offset)) >> $offset;
     }
 
-    private function setBitfield($var, $value)
+    private function setBitfield($offset, $value)
     {
-        $offset = 8*$var;
         $this->bitfield = ($this->bitfield & ~(255 << $offset)) | ((255 & $value) << $offset);
     }