diff --git a/.travis.yml b/.travis.yml index 6f88ce5..a24d15c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,21 +8,21 @@ php: dist: trusty -before_install: - - composer selfupdate - install: - composer install --prefer-source --dev + - wget https://github.com/infection/infection/releases/download/0.7.0/infection.phar + - wget https://github.com/infection/infection/releases/download/0.7.0/infection.phar.pubkey + - chmod +x infection.phar script: - php vendor/bin/phpunit - - vendor/bin/phpstan analyze -l 4 src tests examples - php vendor/bin/phpcs -n --standard=PSR1,PSR2 --report=full src/ tests/ examples/ + - if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php infection.phar --min-msi=75 --min-covered-msi=75 --threads=4; fi + - php vendor/bin/phpstan analyze -l 4 src tests examples - ./validate_examples.sh after_success: - - wget https://scrutinizer-ci.com/ocular.phar - - sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ] && [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php ocular.phar code-coverage:upload --format=php-clover build/docs/clover.xml; fi;' + - if [ "$TRAVIS_PHP_VERSION" != "nightly" ]; then php ocular.phar code-coverage:upload --format=php-clover build/docs/clover.xml; fi matrix: fast_finish: true diff --git a/infection.json.dist b/infection.json.dist new file mode 100644 index 0000000..b883a21 --- /dev/null +++ b/infection.json.dist @@ -0,0 +1,11 @@ +{ + "timeout": 10, + "source": { + "directories": [ + "src" + ] + }, + "logs": { + "text": "infection-log.txt" + } +} \ No newline at end of file diff --git a/src/Buffertools/Parser.php b/src/Buffertools/Parser.php index 1565bbe..ee0998e 100644 --- a/src/Buffertools/Parser.php +++ b/src/Buffertools/Parser.php @@ -11,7 +11,7 @@ class Parser /** * @var string */ - private $string; + private $string = ''; /** * @var int @@ -26,25 +26,15 @@ class Parser /** * Instantiate class, optionally taking Buffer or HEX. * - * @param null|string|BufferInterface $input + * @param BufferInterface $input */ - public function __construct($input = null) + public function __construct(BufferInterface $input = null) { - if (null === $input) { - $input = ''; + if ($input instanceof BufferInterface) { + $this->string = $input->getBinary(); + $this->size = $input->getSize(); + assert(strlen($this->string) === $this->size); } - - if (is_string($input)) { - $bin = Buffer::hex($input, null)->getBinary(); - } elseif ($input instanceof BufferInterface) { - $bin = $input->getBinary(); - } else { - throw new \InvalidArgumentException("Invalid argument to Parser"); - } - - $this->string = $bin; - $this->position = 0; - $this->size = strlen($this->string); } /** @@ -96,64 +86,6 @@ public function readBytes(int $numBytes, bool $flipBytes = false): BufferInterfa return new Buffer($string, $length); } - /** - * Write $data as $bytes bytes. Can be flipped if needed. - * - * @param integer $numBytes - number of bytes to write - * @param SerializableInterface|BufferInterface|string $data - buffer, serializable or hex - * @param bool $flipBytes - * @return Parser - */ - public function writeBytes(int $numBytes, $data, bool $flipBytes = false): Parser - { - // Treat $data to ensure it's a buffer, with the correct size - if ($data instanceof SerializableInterface) { - $data = $data->getBuffer(); - } - - if (is_string($data)) { - // Convert to a buffer - $data = Buffer::hex($data, $numBytes); - } else if (!($data instanceof BufferInterface)) { - throw new \RuntimeException('Invalid data passed to Parser::writeBytes'); - } - - $this->writeBuffer($numBytes, $data, $flipBytes); - - return $this; - } - - /** - * Write $data as $bytes bytes. Can be flipped if needed. - * - * @param integer $numBytes - * @param string $data - * @param bool $flipBytes - * @return Parser - */ - public function writeRawBinary(int $numBytes, string $data, bool $flipBytes = false): Parser - { - return $this->writeBuffer($numBytes, new Buffer($data, $numBytes), $flipBytes); - } - - /** - * @param BufferInterface $buffer - * @param bool $flipBytes - * @param int $numBytes - * @return Parser - */ - public function writeBuffer(int $numBytes, BufferInterface $buffer, bool $flipBytes = false): Parser - { - // only create a new buffer if the size does not match - if ($buffer->getSize() != $numBytes) { - $buffer = new Buffer($buffer->getBinary(), $numBytes); - } - - $this->appendBuffer($buffer, $flipBytes); - - return $this; - } - /** * @param BufferInterface $buffer * @param bool $flipBytes @@ -195,7 +127,7 @@ public function writeArray(array $serializable): Parser } if ($object instanceof BufferInterface) { - $parser->writeBytes($object->getSize(), $object); + $parser->appendBinary($object->getBinary()); } else { throw new \RuntimeException('Input to writeArray must be Buffer[], or SerializableInterface[]'); } @@ -214,6 +146,6 @@ public function writeArray(array $serializable): Parser */ public function getBuffer(): BufferInterface { - return new Buffer($this->string, null); + return new Buffer($this->string, $this->size); } } diff --git a/src/Buffertools/Template.php b/src/Buffertools/Template.php index bc57afa..4df2d97 100644 --- a/src/Buffertools/Template.php +++ b/src/Buffertools/Template.php @@ -63,7 +63,7 @@ public function addItem(TypeInterface $item): Template */ public function parse(Parser $parser): array { - if (0 == count($this->template)) { + if (empty($this->template)) { throw new \RuntimeException('No items in template'); } diff --git a/src/Buffertools/Types/ByteString.php b/src/Buffertools/Types/ByteString.php index 2df9cc5..b273867 100644 --- a/src/Buffertools/Types/ByteString.php +++ b/src/Buffertools/Types/ByteString.php @@ -43,7 +43,7 @@ public function writeBits(BufferInterface $string): string } /** - * @param Buffer $string + * @param BufferInterface $string * @return string * @throws \Exception */ @@ -84,7 +84,7 @@ public function readBits(BufferInterface $buffer): string /** * @param Parser $parser * @return BufferInterface - * @throws \BitWasp\Buffertools\Exceptions\ParserOutOfRange + * @throws \Exception */ public function read(Parser $parser): BufferInterface { diff --git a/tests/ParserTest.php b/tests/ParserTest.php index e2dbc36..4a2ac8d 100644 --- a/tests/ParserTest.php +++ b/tests/ParserTest.php @@ -16,6 +16,7 @@ public function testParserEmpty() $this->assertInstanceOf(Parser::class, $parser); $this->assertSame(0, $parser->getPosition()); + $this->assertSame(0, $parser->getSize()); $this->assertInstanceOf(Buffer::class, $parser->getBuffer()); $this->assertEmpty($parser->getBuffer()->getHex()); } @@ -23,9 +24,10 @@ public function testParserEmpty() public function testGetBuffer() { $buffer = Buffer::hex('41414141'); - $parser = new Parser($buffer); - $this->assertSame($parser->getBuffer()->getBinary(), $buffer->getBinary()); + $this->assertSame(0, $parser->getPosition()); + $this->assertSame($buffer->getBinary(), $parser->getBuffer()->getBinary()); + $this->assertEquals($buffer->getSize(), $parser->getSize()); } public function testGetBufferEmptyNull() @@ -39,58 +41,67 @@ public function testGetBufferEmptyNull() public function testWriteBytes() { - $bytes = '41424344'; + $bytes = Buffer::hex('41424344'); + $parser = new Parser(); - $parser->writeBytes(4, Buffer::hex($bytes)); - $returned = $parser->getBuffer()->getHex(); - $this->assertSame($returned, '41424344'); + $this->assertEquals(0, $parser->getSize()); + $this->assertEquals(0, $parser->getPosition()); + $parser->appendBuffer($bytes); + $this->assertEquals(4, $parser->getSize()); + $this->assertEquals(0, $parser->getPosition()); + + $this->assertTrue($parser->getBuffer()->equals($bytes)); + + $bytesAgain = $parser->readBytes(4); + + $this->assertEquals(4, $parser->getSize()); + $this->assertEquals(4, $parser->getPosition()); + $this->assertTrue($bytesAgain->equals($bytes)); } public function testWriteBytesFlip() { - $bytes = '41424344'; + $bytes = Buffer::hex('41424344'); $parser = new Parser(); - $parser->writeBytes(4, Buffer::hex($bytes), true); - $returned = $parser->getBuffer()->getHex(); - $this->assertSame($returned, '44434241'); + $parser->appendBuffer($bytes, true); + + $this->assertEquals('44434241', $parser->getBuffer()->getHex()); } public function testWriteBytesPadded() { $parser = new Parser(); - $parser->writeBytes(4, Buffer::hex('34')); + $parser->appendBuffer(Buffer::hex('34')); $this->assertEquals("00000034", $parser->getBuffer()->getHex()); } public function testWriteBytesFlipPadded() { $parser = new Parser(); - $parser->writeBytes(4, Buffer::hex('34'), true); + $parser->appendBuffer(Buffer::hex('34', 4), true); $this->assertEquals("34000000", $parser->getBuffer()->getHex()); } public function testReadBytes() { - $bytes = '41424344'; + $bytes = Buffer::hex('41424344'); $parser = new Parser($bytes); $read = $parser->readBytes(4); $this->assertInstanceOf(Buffer::class, $read); - $hex = $read->getHex(); - $this->assertSame($bytes, $hex); + $this->assertTrue($read->equals($bytes)); } public function testReadBytesFlip() { - $bytes = '41424344'; + $bytes = Buffer::hex('41424344'); $parser = new Parser($bytes); $read = $parser->readBytes(4, true); $this->assertInstanceOf(Buffer::class, $read); - $hex = $read->getHex(); - $this->assertSame('44434241', $hex); + $this->assertEquals($bytes->flip()->getHex(), $read->getHex()); } /** @@ -103,8 +114,7 @@ public function testReadBytesEmpty() // and length is zero. $parser = new Parser(); - $data = $parser->readBytes(0); - $this->assertFalse(!!$data); + $parser->readBytes(0); } /** * @expectedException \BitWasp\Buffertools\Exceptions\ParserOutOfRange @@ -112,7 +122,7 @@ public function testReadBytesEmpty() */ public function testReadBytesEndOfString() { - $parser = new Parser('4041414142414141'); + $parser = new Parser(Buffer::hex('4041414142414141')); $bytes1 = $parser->readBytes(4); $bytes2 = $parser->readBytes(4); $this->assertSame($bytes1->getHex(), '40414141'); @@ -125,7 +135,7 @@ public function testReadBytesEndOfString() */ public function testReadBytesBeyondLength() { - $bytes = '41424344'; + $bytes = Buffer::hex('41424344'); $parser = new Parser($bytes); $parser->readBytes(5); } diff --git a/tests/TemplateTest.php b/tests/TemplateTest.php index 84f8c19..f42b409 100644 --- a/tests/TemplateTest.php +++ b/tests/TemplateTest.php @@ -29,7 +29,7 @@ public function testTemplate() public function testTemplateEmptyParse() { $template = new Template(); - $parser = new Parser('010203040a0b0c0d'); + $parser = new Parser(Buffer::hex('010203040a0b0c0d')); $template->parse($parser); } diff --git a/tests/Types/IntegerBitSizeTest.php b/tests/Types/IntegerBitSizeTest.php new file mode 100644 index 0000000..b1e8b7f --- /dev/null +++ b/tests/Types/IntegerBitSizeTest.php @@ -0,0 +1,52 @@ +assertEquals($bitSize, $integer->getBitSize()); + } +}