From 61e62cb473126f6508696ef92e0b5ae4b0e3f420 Mon Sep 17 00:00:00 2001 From: Thomas Kerin Date: Tue, 10 Dec 2019 02:01:12 +0000 Subject: [PATCH] [BC BREAK] Remove Collection classes, refactor ScriptWitness. This PR removes the Collection classes, whose only usage is by ScriptWitness. This way the ScriptWitness class is a little easier to follow --- src/Collection/CollectionInterface.php | 35 ---- src/Collection/StaticBufferCollection.php | 66 ------- src/Collection/StaticCollection.php | 163 ----------------- src/Script/ScriptWitness.php | 166 +++++++++++++++++- src/Script/ScriptWitnessInterface.php | 25 ++- .../ScriptWitnessTest.php} | 28 ++- 6 files changed, 200 insertions(+), 283 deletions(-) delete mode 100644 src/Collection/CollectionInterface.php delete mode 100644 src/Collection/StaticBufferCollection.php delete mode 100644 src/Collection/StaticCollection.php rename tests/{Collection/StaticCollectionImplTest.php => Script/ScriptWitnessTest.php} (59%) diff --git a/src/Collection/CollectionInterface.php b/src/Collection/CollectionInterface.php deleted file mode 100644 index 171347ae8..000000000 --- a/src/Collection/CollectionInterface.php +++ /dev/null @@ -1,35 +0,0 @@ -set = $values; - } - - /** - * @return BufferInterface - */ - public function bottom(): BufferInterface - { - return parent::bottom(); - } - - /** - * @return BufferInterface - */ - public function top(): BufferInterface - { - return parent::top(); - } - - /** - * @return BufferInterface - */ - public function current(): BufferInterface - { - return $this->set[$this->position]; - } - - /** - * @param int $offset - * @return BufferInterface - */ - public function offsetGet($offset) - { - if (!array_key_exists($offset, $this->set)) { - throw new \OutOfRangeException('No offset found'); - } - - return $this->set[$offset]; - } -} diff --git a/src/Collection/StaticCollection.php b/src/Collection/StaticCollection.php deleted file mode 100644 index 12be180f8..000000000 --- a/src/Collection/StaticCollection.php +++ /dev/null @@ -1,163 +0,0 @@ -set; - } - - /** - * @param int $start - * @param int $length - * @return self - */ - public function slice(int $start, int $length) - { - $end = count($this->set); - if ($start > $end || $length > $end) { - throw new \RuntimeException('Invalid start or length'); - } - - $sliced = array_slice($this->set, $start, $length); - return new static(...$sliced); - } - - /** - * @return int - */ - public function count(): int - { - return count($this->set); - } - - /** - * @return BufferInterface - */ - public function bottom(): BufferInterface - { - if (count($this->set) === 0) { - throw new \RuntimeException('No bottom for empty collection'); - } - - return $this->offsetGet(count($this) - 1); - } - - /** - * @return BufferInterface - */ - public function top(): BufferInterface - { - if (count($this->set) === 0) { - throw new \RuntimeException('No top for empty collection'); - } - - return $this->offsetGet(0); - } - - /** - * @return bool - */ - public function isNull(): bool - { - return count($this->set) === 0; - } - - /** - * @return void - */ - public function rewind() - { - $this->position = 0; - } - - /** - * @return BufferInterface - */ - public function current(): BufferInterface - { - return $this->set[$this->position]; - } - - /** - * @return int - */ - public function key(): int - { - return $this->position; - } - - /** - * @return void - */ - public function next() - { - ++$this->position; - } - - /** - * @return bool - */ - public function valid(): bool - { - return isset($this->set[$this->position]); - } - - /** - * @param int $offset - * @return bool - */ - public function offsetExists($offset) - { - return array_key_exists($offset, $this->set); - } - - /** - * @param int $offset - */ - public function offsetUnset($offset) - { - throw new \RuntimeException('Cannot unset from a Static Collection'); - } - - /** - * @param int $offset - * @return mixed - */ - public function offsetGet($offset) - { - if (!array_key_exists($offset, $this->set)) { - throw new \OutOfRangeException('Nothing found at this offset'); - } - - return $this->set[$offset]; - } - - /** - * @param int $offset - * @param mixed $value - */ - public function offsetSet($offset, $value) - { - throw new \RuntimeException('Cannot add to a Static Collection'); - } -} diff --git a/src/Script/ScriptWitness.php b/src/Script/ScriptWitness.php index 2dc1a5cee..b9174a6a4 100644 --- a/src/Script/ScriptWitness.php +++ b/src/Script/ScriptWitness.php @@ -8,8 +8,27 @@ use BitWasp\Bitcoin\Serializer\Script\ScriptWitnessSerializer; use BitWasp\Buffertools\BufferInterface; -class ScriptWitness extends StaticBufferCollection implements ScriptWitnessInterface +class ScriptWitness implements ScriptWitnessInterface { + /** + * @var BufferInterface[] + */ + protected $set = []; + + /** + * @var int + */ + protected $position = 0; + + /** + * StaticBufferCollection constructor. + * @param BufferInterface ...$values + */ + public function __construct(BufferInterface... $values) + { + $this->set = $values; + } + /** * @param ScriptWitnessInterface $witness * @return bool @@ -31,10 +50,153 @@ public function equals(ScriptWitnessInterface $witness): bool } /** - * @return \BitWasp\Buffertools\BufferInterface + * @return BufferInterface */ public function getBuffer(): BufferInterface { return (new ScriptWitnessSerializer())->serialize($this); } + + /** + * @return BufferInterface[] + */ + public function all(): array + { + return $this->set; + } + + /** + * @param int $start + * @param int $length + * @return ScriptWitnessInterface + */ + public function slice(int $start, int $length): ScriptWitnessInterface + { + $end = count($this->set); + if ($start > $end || $length > $end) { + throw new \RuntimeException('Invalid start or length'); + } + + $sliced = array_slice($this->set, $start, $length); + return new static(...$sliced); + } + + /** + * @return int + */ + public function count(): int + { + return count($this->set); + } + + /** + * @return BufferInterface + */ + public function bottom(): BufferInterface + { + if (count($this->set) === 0) { + throw new \RuntimeException('No bottom for empty collection'); + } + + return $this->offsetGet(count($this) - 1); + } + + /** + * @return BufferInterface + */ + public function top(): BufferInterface + { + if (count($this->set) === 0) { + throw new \RuntimeException('No top for empty collection'); + } + + return $this->offsetGet(0); + } + + /** + * @return bool + */ + public function isNull(): bool + { + return count($this->set) === 0; + } + + /** + * @return void + */ + public function rewind() + { + $this->position = 0; + } + + /** + * @return BufferInterface + */ + public function current(): BufferInterface + { + return $this->set[$this->position]; + } + + /** + * @return int + */ + public function key(): int + { + return $this->position; + } + + /** + * @return void + */ + public function next() + { + ++$this->position; + } + + /** + * @return bool + */ + public function valid(): bool + { + return isset($this->set[$this->position]); + } + + /** + * @param int $offset + * @return bool + */ + public function offsetExists($offset) + { + return array_key_exists($offset, $this->set); + } + + /** + * @param int $offset + */ + public function offsetUnset($offset) + { + throw new \RuntimeException('ScriptWitness is immutable'); + } + + /** + * @param int $offset + * @return BufferInterface + */ + public function offsetGet($offset) + { + if (!array_key_exists($offset, $this->set)) { + throw new \OutOfRangeException('Nothing found at this offset'); + } + + return $this->set[$offset]; + } + + /** + * @param int $offset + * @param mixed $value + */ + public function offsetSet($offset, $value) + { + throw new \RuntimeException('ScriptWitness is immutable'); + } } diff --git a/src/Script/ScriptWitnessInterface.php b/src/Script/ScriptWitnessInterface.php index 30dabae86..a21389008 100644 --- a/src/Script/ScriptWitnessInterface.php +++ b/src/Script/ScriptWitnessInterface.php @@ -4,12 +4,33 @@ namespace BitWasp\Bitcoin\Script; -use BitWasp\Bitcoin\Collection\CollectionInterface; use BitWasp\Buffertools\BufferInterface; use BitWasp\Buffertools\SerializableInterface; -interface ScriptWitnessInterface extends CollectionInterface, SerializableInterface +interface ScriptWitnessInterface extends SerializableInterface, \Iterator, \ArrayAccess, \Countable { + /** + * @return BufferInterface + */ + public function bottom(): BufferInterface; + + /** + * @return mixed + */ + public function top(): BufferInterface; + + /** + * @param int $start + * @param int $length + * @return ScriptWitnessInterface + */ + public function slice(int $start, int $length): ScriptWitnessInterface; + + /** + * @return bool + */ + public function isNull(): bool; + /** * @return BufferInterface[] */ diff --git a/tests/Collection/StaticCollectionImplTest.php b/tests/Script/ScriptWitnessTest.php similarity index 59% rename from tests/Collection/StaticCollectionImplTest.php rename to tests/Script/ScriptWitnessTest.php index 103a03958..aac00b7a0 100644 --- a/tests/Collection/StaticCollectionImplTest.php +++ b/tests/Script/ScriptWitnessTest.php @@ -1,18 +1,16 @@ -assertEquals(new Buffer("\x01"), $collection[0]); $this->assertEquals(new Buffer("\x01"), $collection->offsetGet(0)); } @@ -22,32 +20,32 @@ public function testArrayAccessOffStaticBufferCollectionGet() */ public function testArrayAccessOffStaticBufferCollectionGetFailure() { - $collection = new StaticBufferCollection(new Buffer("\x01")); + $collection = new ScriptWitness(new Buffer("\x01")); $collection[20]; } - + public function testIteratorStaticBufferCollectionCurrent() { - $StaticBufferCollection = new StaticBufferCollection(new Buffer("\x01"), new Buffer("\x02")); + $StaticBufferCollection = new ScriptWitness(new Buffer("\x01"), new Buffer("\x02")); $this->assertEquals(new Buffer("\x01"), $StaticBufferCollection->current()); } public function testCountable() { - $StaticBufferCollection = new StaticBufferCollection(new Buffer("\x01"), new Buffer("\x02")); + $StaticBufferCollection = new ScriptWitness(new Buffer("\x01"), new Buffer("\x02")); $this->assertEquals(2, count($StaticBufferCollection)); } public function testAll() { $all = [new Buffer("\x01"),new Buffer("\x02")]; - $StaticBufferCollection = new StaticBufferCollection(new Buffer("\x01"), new Buffer("\x02")); + $StaticBufferCollection = new ScriptWitness(new Buffer("\x01"), new Buffer("\x02")); $this->assertEquals($all, $StaticBufferCollection->all()); } public function testGet() { - $StaticBufferCollection = new StaticBufferCollection(new Buffer("\x01"), new Buffer("\x02")); + $StaticBufferCollection = new ScriptWitness(new Buffer("\x01"), new Buffer("\x02")); $this->assertEquals(new Buffer("\x01"), $StaticBufferCollection[0]); } @@ -56,7 +54,7 @@ public function testGet() */ public function testGetInvalid() { - $StaticBufferCollection = new StaticBufferCollection(); + $StaticBufferCollection = new ScriptWitness(); $StaticBufferCollection[0]; } -} +} \ No newline at end of file