1
0
Fork 0

Use constants with names for bitfield offsets

pull/4239/head
Nils Adermann 2015-07-09 18:59:16 +02:00
parent 329ab5cf41
commit f535542fca
1 changed files with 16 additions and 12 deletions

View File

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