Skip to content

Commit

Permalink
New interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
Spomky committed Sep 17, 2021
1 parent f028ab1 commit fd0687c
Show file tree
Hide file tree
Showing 29 changed files with 316 additions and 179 deletions.
16 changes: 4 additions & 12 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
it: coding-standards tests static-analyse performance-tests mutation-tests

.PHONY: code-coverage
code-coverage: vendor ## Show test coverage rates
code-coverage: vendor ## Show test coverage rates (console)
vendor/bin/phpunit --coverage-text

.PHONY: code-coverage-html
Expand All @@ -11,16 +11,12 @@ code-coverage-html: vendor ## Show test coverage rates (HTML)

.PHONY: fix-coding-standards
fix-coding-standards: vendor ## Fix all files using defined PHP-CS-FIXER rules
vendor/bin/php-cs-fixer fix --diff --verbose
vendor/bin/php-cs-fixer fix

.PHONY: coding-standards
coding-standards: vendor ## Check all files using defined PHP-CS-FIXER rules
vendor/bin/php-cs-fixer fix --dry-run --stop-on-violation --using-cache=no

.PHONY: mutation-tests
mutation-tests: vendor ## Run mutation tests with minimum MSI and covered MSI enabled
vendor/bin/infection --logger-github --git-diff-filter=AM -s --threads=$(nproc) --min-msi=50 --min-covered-msi=59

.PHONY: tests
tests: vendor ## Run all tests
vendor/bin/phpunit --color
Expand All @@ -30,21 +26,17 @@ vendor: composer.json composer.lock
composer install

.PHONY: tu
tu: vendor ## Run all unit tests
tu: vendor ## Run only unit tests
vendor/bin/phpunit --color --group Unit

.PHONY: tf
tf: vendor ## Run all functional tests
tf: vendor ## Run only functional tests
vendor/bin/phpunit --color --group Functional

.PHONY: static-analyse
static-analyse: vendor ## Run static analyse
vendor/bin/phpstan analyse

.PHONY: performance-tests
performance-tests: vendor ## Run performance test suite
vendor/bin/phpbench run -l dots --report aggregate

.PHONY: rector
rector: vendor ## Check all files using Rector
vendor/bin/rector process --ansi --dry-run
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ $otherObjectManager = OtherObject\OtherObjectManager::create()
->add(OtherObject\DoublePrecisionFloatObject::class)
;

$tagManager = Tag\TagObjectManager::create()
$tagManager = Tag\TagManager::create()
->add(Tag\DatetimeTag::class)
->add(Tag\TimestampTag::class)
->add(Tag\UnsignedBigIntegerTag::class)
Expand Down
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ parameters:
-
message: '#Parameter .* of static method CBOR\\Utils\:\:binToInt\(\) expects string, string\|null given\.#'
count: 1
path: src/Tag/TagObjectManager.php
path: src/Tag/TagManager.php
-
message: '#Instanceof between CBOR\\CBORObject and CBOR\\CBORObject will always evaluate to true\.#'
count: 1
Expand Down
68 changes: 53 additions & 15 deletions src/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,29 @@

namespace CBOR;

use CBOR\OtherObject\BreakObject;
use CBOR\OtherObject\OtherObjectManager;
use CBOR\Tag\TagObjectManager;
use InvalidArgumentException;
use function ord;
use RuntimeException;

final class Decoder
final class Decoder implements DecoderInterface
{
/**
* @var TagObjectManager
* @var Tag\TagManagerInterface
*/
private $tagObjectManager;

/**
* @var OtherObjectManager
* @var OtherObject\OtherObjectManagerInterface
*/
private $otherTypeManager;

public function __construct(TagObjectManager $tagObjectManager, OtherObjectManager $otherTypeManager)
public function __construct(?Tag\TagManagerInterface $tagObjectManager = null, ?OtherObject\OtherObjectManagerInterface $otherTypeManager = null)
{
$this->tagObjectManager = $tagObjectManager;
$this->otherTypeManager = $otherTypeManager;
$this->tagObjectManager = $tagObjectManager ?? $this->generateTagManager();
$this->otherTypeManager = $otherTypeManager ?? $this->generateOtherObjectManager();
}

public static function create(TagObjectManager $tagObjectManager, OtherObjectManager $otherTypeManager): self
public static function create(?Tag\TagManagerInterface $tagObjectManager = null, ?OtherObject\OtherObjectManagerInterface $otherTypeManager = null): self
{
return new self($tagObjectManager, $otherTypeManager);
}
Expand Down Expand Up @@ -117,7 +114,7 @@ private function processInfinite(Stream $stream, int $mt, bool $breakable): CBOR
switch ($mt) {
case CBORObject::MAJOR_TYPE_BYTE_STRING: //2
$object = IndefiniteLengthByteStringObject::create();
while (!($it = $this->process($stream, true)) instanceof BreakObject) {
while (!($it = $this->process($stream, true)) instanceof OtherObject\BreakObject) {
if (!$it instanceof ByteStringObject) {
throw new RuntimeException('Unable to parse the data. Infinite Byte String object can only get Byte String objects.');
}
Expand All @@ -127,7 +124,7 @@ private function processInfinite(Stream $stream, int $mt, bool $breakable): CBOR
return $object;
case CBORObject::MAJOR_TYPE_TEXT_STRING: //3
$object = IndefiniteLengthTextStringObject::create();
while (!($it = $this->process($stream, true)) instanceof BreakObject) {
while (!($it = $this->process($stream, true)) instanceof OtherObject\BreakObject) {
if (!$it instanceof TextStringObject) {
throw new RuntimeException('Unable to parse the data. Infinite Text String object can only get Text String objects.');
}
Expand All @@ -137,14 +134,14 @@ private function processInfinite(Stream $stream, int $mt, bool $breakable): CBOR
return $object;
case CBORObject::MAJOR_TYPE_LIST: //4
$object = IndefiniteLengthListObject::create();
while (!($it = $this->process($stream, true)) instanceof BreakObject) {
while (!($it = $this->process($stream, true)) instanceof OtherObject\BreakObject) {
$object->add($it);
}

return $object;
case CBORObject::MAJOR_TYPE_MAP: //5
$object = IndefiniteLengthMapObject::create();
while (!($it = $this->process($stream, true)) instanceof BreakObject) {
while (!($it = $this->process($stream, true)) instanceof OtherObject\BreakObject) {
$object->append($it, $this->process($stream, false));
}

Expand All @@ -154,12 +151,53 @@ private function processInfinite(Stream $stream, int $mt, bool $breakable): CBOR
throw new InvalidArgumentException('Cannot parse the data. No enclosing indefinite.');
}

return BreakObject::create();
return OtherObject\BreakObject::create();
case CBORObject::MAJOR_TYPE_UNSIGNED_INTEGER: //0
case CBORObject::MAJOR_TYPE_NEGATIVE_INTEGER: //1
case CBORObject::MAJOR_TYPE_TAG: //6
default:
throw new InvalidArgumentException(sprintf('Cannot parse the data. Found infinite length for Major Type "%s" (%d).', str_pad(decbin($mt), 5, '0', STR_PAD_LEFT), $mt));
}
}

private function generateTagManager(): Tag\TagManagerInterface
{
return Tag\TagManager::create()
->add(Tag\DatetimeTag::class)
->add(Tag\TimestampTag::class)

->add(Tag\UnsignedBigIntegerTag::class)
->add(Tag\NegativeBigIntegerTag::class)

->add(Tag\DecimalFractionTag::class)
->add(Tag\BigFloatTag::class)

->add(Tag\Base64UrlEncodingTag::class)
->add(Tag\Base64EncodingTag::class)
->add(Tag\Base16EncodingTag::class)
->add(Tag\CBOREncodingTag::class)

->add(Tag\UriTag::class)
->add(Tag\Base64UrlTag::class)
->add(Tag\Base64Tag::class)
->add(Tag\MimeTag::class)

->add(Tag\CBORTag::class)
;
}

private function generateOtherObjectManager(): OtherObject\OtherObjectManagerInterface
{
return OtherObject\OtherObjectManager::create()
->add(OtherObject\BreakObject::class)
->add(OtherObject\SimpleObject::class)
->add(OtherObject\FalseObject::class)
->add(OtherObject\TrueObject::class)
->add(OtherObject\NullObject::class)
->add(OtherObject\UndefinedObject::class)
->add(OtherObject\HalfPrecisionFloatObject::class)
->add(OtherObject\SinglePrecisionFloatObject::class)
->add(OtherObject\DoublePrecisionFloatObject::class)
;
}
}
19 changes: 19 additions & 0 deletions src/DecoderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

/*
* The MIT License (MIT)
*
* Copyright (c) 2018-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

namespace CBOR;

interface DecoderInterface
{
public function decode(Stream $stream): CBORObject;
}
2 changes: 1 addition & 1 deletion src/OtherObject/OtherObjectManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use CBOR\OtherObject;
use InvalidArgumentException;

class OtherObjectManager
class OtherObjectManager implements OtherObjectManagerInterface
{
/**
* @var string[]
Expand Down
21 changes: 21 additions & 0 deletions src/OtherObject/OtherObjectManagerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

/*
* The MIT License (MIT)
*
* Copyright (c) 2018-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

namespace CBOR\OtherObject;

use CBOR\OtherObject;

interface OtherObjectManagerInterface
{
public function createObjectForValue(int $value, ?string $data): OtherObject;
}
56 changes: 56 additions & 0 deletions src/Tag.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

/*
* The MIT License (MIT)
*
* Copyright (c) 2018-2020 Spomky-Labs
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/

namespace CBOR;

abstract class Tag extends AbstractCBORObject
{
private const MAJOR_TYPE = self::MAJOR_TYPE_TAG;

/**
* @var string|null
*/
protected $data;

/**
* @var CBORObject
*/
protected $object;

public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
{
parent::__construct(self::MAJOR_TYPE, $additionalInformation);
$this->data = $data;
$this->object = $object;
}

public function __toString(): string
{
$result = parent::__toString();
if (null !== $this->data) {
$result .= $this->data;
}
$result .= (string) $this->object;

return $result;
}

abstract public static function getTagId(): int;

abstract public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): self;

public function getValue(): CBORObject
{
return $this->object;
}
}
8 changes: 4 additions & 4 deletions src/Tag/Base16EncodingTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,22 @@
use CBOR\CBORObject;
use CBOR\IndefiniteLengthByteStringObject;
use CBOR\IndefiniteLengthTextStringObject;
use CBOR\TagObject as Base;
use CBOR\Tag;
use CBOR\TextStringObject;

final class Base16EncodingTag extends Base
final class Base16EncodingTag extends Tag
{
public static function getTagId(): int
{
return self::TAG_ENCODED_BASE16;
}

public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
{
return new self($additionalInformation, $data, $object);
}

public static function create(CBORObject $object): Base
public static function create(CBORObject $object): Tag
{
return new self(self::TAG_ENCODED_BASE16, null, $object);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Tag/Base64EncodingTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,23 @@
use CBOR\CBORObject;
use CBOR\IndefiniteLengthByteStringObject;
use CBOR\IndefiniteLengthTextStringObject;
use CBOR\TagObject as Base;
use CBOR\Tag;
use CBOR\TextStringObject;
use InvalidArgumentException;

final class Base64EncodingTag extends Base
final class Base64EncodingTag extends Tag
{
public static function getTagId(): int
{
return self::TAG_ENCODED_BASE64;
}

public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
{
return new self($additionalInformation, $data, $object);
}

public static function create(CBORObject $object): Base
public static function create(CBORObject $object): Tag
{
return new self(self::TAG_ENCODED_BASE64, null, $object);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Tag/Base64Tag.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

use CBOR\CBORObject;
use CBOR\IndefiniteLengthTextStringObject;
use CBOR\TagObject as Base;
use CBOR\Tag;
use CBOR\TextStringObject;
use InvalidArgumentException;

final class Base64Tag extends Base
final class Base64Tag extends Tag
{
public function __construct(int $additionalInformation, ?string $data, CBORObject $object)
{
Expand All @@ -35,12 +35,12 @@ public static function getTagId(): int
return self::TAG_BASE64;
}

public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Base
public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): Tag
{
return new self($additionalInformation, $data, $object);
}

public static function create(CBORObject $object): Base
public static function create(CBORObject $object): Tag
{
return new self(self::TAG_BASE64, null, $object);
}
Expand Down
Loading

0 comments on commit fd0687c

Please sign in to comment.