diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php deleted file mode 100644 index 1a81a08..0000000 --- a/.php-cs-fixer.dist.php +++ /dev/null @@ -1,69 +0,0 @@ -in('src') - ->in('tests') -; - -return (new PhpCsFixer\Config()) - ->setRules([ - '@PSR1' => true, - '@PSR2' => true, - '@PhpCsFixer' => true, - '@Symfony' => true, - '@DoctrineAnnotation' => true, - '@PHP70Migration' => true, - '@PHP71Migration' => true, - 'strict_param' => true, - 'strict_comparison' => true, - 'array_syntax' => ['syntax' => 'short'], - 'array_indentation' => true, - 'ordered_imports' => true, - 'protected_to_private' => true, - 'declare_strict_types' => true, - 'native_function_invocation' => [ - 'include' => ['@compiler_optimized'], - 'scope' => 'namespaced', - 'strict' => true, - ], - 'mb_str_functions' => true, - 'linebreak_after_opening_tag' => true, - 'combine_consecutive_issets' => true, - 'combine_consecutive_unsets' => true, - 'compact_nullable_typehint' => true, - 'no_superfluous_phpdoc_tags' => true, - 'no_superfluous_elseif' => true, - 'phpdoc_trim_consecutive_blank_line_separation' => true, - 'phpdoc_order' => true, - 'pow_to_exponentiation' => true, - 'simplified_null_return' => true, - 'header_comment' => [ - 'header' => $header, - ], - 'align_multiline_comment' => [ - 'comment_type' => 'all_multiline', - ], - 'php_unit_test_annotation' => [ - 'style' => 'annotation', - ], - 'php_unit_test_case_static_method_calls' => true, - 'method_chaining_indentation' => false, - 'php_unit_expectation' => true, - 'php_unit_test_class_requires_covers' => false, - 'global_namespace_import' => [ - 'import_classes' => true, - 'import_constants' => true, - 'import_functions' => true, - ], - ]) - ->setRiskyAllowed(true) - ->setUsingCache(true) - ->setFinder($finder) - ; diff --git a/.scrutinizer.yml b/.scrutinizer.yml deleted file mode 100644 index f71c416..0000000 --- a/.scrutinizer.yml +++ /dev/null @@ -1,9 +0,0 @@ -before_commands: - - "composer install --prefer-dist" - -filter: - excluded_paths: ["vendor/*", "tests/*"] -build_failure_conditions: - - 'elements.rating(<= C).new.exists' # No new classes/methods with a rating of C or worse - - 'issues.severity(>= MAJOR).new.exists' # New issues of major or higher severity - - 'project.metric("scrutinizer.quality", < 9)' # Code Quality Rating drops below 9 diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 1fc3c25..0000000 --- a/.travis.yml +++ /dev/null @@ -1,57 +0,0 @@ -sudo: false -language: php - -php: - - 7.3 - - 7.4 - - nightly - -cache: - directories: - - $HOME/.composer/cache - - vendor - -before_script: - - wget -O phpunit https://phar.phpunit.de/phpunit-9.phar - - chmod +x phpunit - - mkdir -p build/logs - -before_install: - - mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{,.disabled} || echo "xdebug not available" - -install: travis_retry composer install - -script: - - ./phpunit - -jobs: - allow_failures: - - php: nightly - - include: - - stage: Metrics and quality - env: COVERAGE - before_script: - - wget -O phpunit https://phar.phpunit.de/phpunit-9.phar - - chmod +x phpunit - - wget https://github.com/php-coveralls/php-coveralls/releases/download/v2.2.0/php-coveralls.phar - - chmod +x php-coveralls.phar - - mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{.disabled,} - - if [[ ! $(php -m | grep -si xdebug) ]]; then echo "xdebug required for coverage"; exit 1; fi - script: - - ./phpunit --coverage-clover build/logs/clover.xml - after_script: - - ./php-coveralls --no-interaction - - - stage: Metrics and quality - env: STATIC_ANALYSIS - script: - - ./vendor/bin/phpstan analyse - - - stage: Security Check - env: SECURITY_CHECK - before_script: - - wget -c https://get.sensiolabs.org/security-checker.phar - - chmod +x security-checker.phar - script: - - ./security-checker.phar security:check diff --git a/composer.json b/composer.json index 8fcfced..367c456 100644 --- a/composer.json +++ b/composer.json @@ -29,19 +29,20 @@ "ext-mbstring": "*" }, "require-dev": { - "ekino/phpstan-banned-code": "^0.5.0", - "friendsofphp/php-cs-fixer": "^2.0|^3.0", + "ext-json": "*", + "ekino/phpstan-banned-code": "^1.0", "infection/infection": "^0.18|^0.25", "phpstan/extension-installer": "^1.1", - "phpstan/phpstan": "^0.12", - "phpstan/phpstan-beberlei-assert": "^0.12", - "phpstan/phpstan-deprecation-rules": "^0.12", - "phpstan/phpstan-phpunit": "^0.12", - "phpstan/phpstan-strict-rules": "^0.12", + "phpstan/phpstan": "^1.0", + "phpstan/phpstan-beberlei-assert": "^1.0", + "phpstan/phpstan-deprecation-rules": "^1.0", + "phpstan/phpstan-phpunit": "^1.0", + "phpstan/phpstan-strict-rules": "^1.0", "phpunit/phpunit": "^9.5", - "rector/rector": "^0.11.53", + "rector/rector": "^0.12", "roave/security-advisories": "dev-latest", - "symfony/var-dumper": "^5.3" + "symfony/var-dumper": "^5.3", + "symplify/easy-coding-standard": "^9.4" }, "config": { "sort-packages": true diff --git a/ecs.php b/ecs.php new file mode 100644 index 0000000..1ce086e --- /dev/null +++ b/ecs.php @@ -0,0 +1,120 @@ +import(SetList::PSR_12); + $containerConfigurator->import(SetList::PHP_CS_FIXER); + $containerConfigurator->import(SetList::PHP_CS_FIXER_RISKY); + $containerConfigurator->import(SetList::CLEAN_CODE); + $containerConfigurator->import(SetList::SYMFONY); + $containerConfigurator->import(SetList::DOCTRINE_ANNOTATIONS); + $containerConfigurator->import(SetList::SPACES); + $containerConfigurator->import(SetList::PHPUNIT); + $containerConfigurator->import(SetList::SYMPLIFY); + $containerConfigurator->import(SetList::ARRAY); + $containerConfigurator->import(SetList::COMMON); + $containerConfigurator->import(SetList::COMMENTS); + $containerConfigurator->import(SetList::CONTROL_STRUCTURES); + $containerConfigurator->import(SetList::DOCBLOCK); + $containerConfigurator->import(SetList::NAMESPACES); + $containerConfigurator->import(SetList::STRICT); + + $services = $containerConfigurator->services(); + $services->set(StrictParamFixer::class); + $services->set(StrictComparisonFixer::class); + $services->set(ArraySyntaxFixer::class) + ->call('configure', [[ + 'syntax' => 'short', + ]]) + ; + $services->set(ArrayIndentationFixer::class); + $services->set(OrderedImportsFixer::class); + $services->set(ProtectedToPrivateFixer::class); + $services->set(DeclareStrictTypesFixer::class); + $services->set(NativeConstantInvocationFixer::class); + $services->set(NativeFunctionInvocationFixer::class) + ->call('configure', [[ + 'include' => ['@compiler_optimized'], + 'scope' => 'namespaced', + 'strict' => true, + ]]) + ; + $services->set(MbStrFunctionsFixer::class); + $services->set(LinebreakAfterOpeningTagFixer::class); + $services->set(CombineConsecutiveIssetsFixer::class); + $services->set(CombineConsecutiveUnsetsFixer::class); + $services->set(CompactNullableTypehintFixer::class); + $services->set(NoSuperfluousElseifFixer::class); + $services->set(NoSuperfluousPhpdocTagsFixer::class); + $services->set(PhpdocTrimConsecutiveBlankLineSeparationFixer::class); + $services->set(PhpdocOrderFixer::class); + $services->set(SimplifiedNullReturnFixer::class); + $services->set(HeaderCommentFixer::class) + ->call('configure', [[ + 'header' => $header, + ]]) + ; + $services->set(AlignMultilineCommentFixer::class) + ->call('configure', [[ + 'comment_type' => 'all_multiline', + ]]) + ; + $services->set(PhpUnitTestAnnotationFixer::class) + ->call('configure', [[ + 'style' => 'annotation', + ]]) + ; + $services->set(PhpUnitTestCaseStaticMethodCallsFixer::class); + $services->set(GlobalNamespaceImportFixer::class) + ->call('configure', [[ + 'import_classes' => true, + 'import_constants' => true, + 'import_functions' => true, + ]]) + ; + + // The absence of @cover in tests is OK + $services->remove(PhpUnitTestClassRequiresCoversFixer::class); + + // The assignment in if of while statements are OK + $services->remove(AssignmentInConditionSniff::class); + + $parameters = $containerConfigurator->parameters(); + $parameters + ->set(Option::PATHS, [__DIR__]) + ->set(Option::SKIP, [__DIR__ . '/.github', __DIR__ . '/build', __DIR__ . '/vendor']) + ; +}; diff --git a/phpstan.neon b/phpstan.neon index 0ce81cc..532f795 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -32,3 +32,35 @@ parameters: message: '#Method CBOR\\OtherObject\\NullObject\:\:normalize\(\) should return mixed but return statement is missing\.#' count: 1 path: src/OtherObject/NullObject.php + - + message: '#Class CBOR\\TextStringWithChunkObject extends \@final class CBOR\\IndefiniteLengthTextStringObject\.#' + count: 1 + path: src/TextStringWithChunkObject.php + - + message: '#Class CBOR\\Tag\\TagObjectManager extends \@final class CBOR\\Tag\\TagManager\.#' + count: 1 + path: src/Tag/TagObjectManager.php + - + message: '#Class CBOR\\Tag\\PositiveBigIntegerTag extends \@final class CBOR\\Tag\\UnsignedBigIntegerTag\.#' + count: 1 + path: src/Tag/PositiveBigIntegerTag.php + - + message: '#Class CBOR\\Tag\\EpochTag extends \@final class CBOR\\Tag\\DatetimeTag\.#' + count: 1 + path: src/Tag/EpochTag.php + - + message: '#Class CBOR\\SignedIntegerObject extends \@final class CBOR\\NegativeIntegerObject\.#' + count: 1 + path: src/SignedIntegerObject.php + - + message: '#Class CBOR\\InfiniteMapObject extends \@final class CBOR\\IndefiniteLengthMapObject\.#' + count: 1 + path: src/InfiniteMapObject.php + - + message: '#Class CBOR\\InfiniteListObject extends \@final class CBOR\\IndefiniteLengthListObject\.#' + count: 1 + path: src/InfiniteListObject.php + - + message: '#Class CBOR\\ByteStringWithChunkObject extends \@final class CBOR\\IndefiniteLengthByteStringObject\.#' + count: 1 + path: src/ByteStringWithChunkObject.php diff --git a/rector.php b/rector.php index 385f71c..2f62c6e 100644 --- a/rector.php +++ b/rector.php @@ -12,10 +12,12 @@ return static function (ContainerConfigurator $containerConfigurator): void { $containerConfigurator->import(SetList::DEAD_CODE); $containerConfigurator->import(SetList::PHP_73); - $containerConfigurator->import(SymfonySetList::SYMFONY_52); + $containerConfigurator->import(SymfonySetList::SYMFONY_52_VALIDATOR_ATTRIBUTES); + $containerConfigurator->import(SymfonySetList::SYMFONY_CODE_QUALITY); + $containerConfigurator->import(SymfonySetList::SYMFONY_CONSTRUCTOR_INJECTION); $containerConfigurator->import(SymfonySetList::SYMFONY_CODE_QUALITY); $parameters = $containerConfigurator->parameters(); - $parameters->set(Option::PATHS, [__DIR__.'/src', __DIR__.'/tests']); + $parameters->set(Option::PATHS, [__DIR__ . '/src', __DIR__ . '/tests']); $parameters->set(Option::PHP_VERSION_FEATURES, PhpVersion::PHP_73); $parameters->set(Option::AUTO_IMPORT_NAMES, true); $parameters->set(Option::IMPORT_SHORT_CLASSES, false); diff --git a/src/AbstractCBORObject.php b/src/AbstractCBORObject.php index 764d7e6..f77362c 100644 --- a/src/AbstractCBORObject.php +++ b/src/AbstractCBORObject.php @@ -2,15 +2,6 @@ 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; use function chr; @@ -21,6 +12,7 @@ abstract class AbstractCBORObject implements CBORObject * @var int */ protected $additionalInformation; + /** * @var int */ diff --git a/src/ByteStringObject.php b/src/ByteStringObject.php index 5e918df..34242cc 100644 --- a/src/ByteStringObject.php +++ b/src/ByteStringObject.php @@ -2,15 +2,6 @@ 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; final class ByteStringObject extends AbstractCBORObject implements Normalizable @@ -36,19 +27,19 @@ public function __construct(string $data) $this->value = $data; } - public static function create(string $data): self - { - return new self($data); - } - public function __toString(): string { $result = parent::__toString(); - if (null !== $this->length) { + if ($this->length !== null) { $result .= $this->length; } - return $result.$this->value; + return $result . $this->value; + } + + public static function create(string $data): self + { + return new self($data); } public function getValue(): string diff --git a/src/ByteStringWithChunkObject.php b/src/ByteStringWithChunkObject.php index d173e2f..e524301 100644 --- a/src/ByteStringWithChunkObject.php +++ b/src/ByteStringWithChunkObject.php @@ -2,15 +2,6 @@ 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; /** diff --git a/src/CBORObject.php b/src/CBORObject.php index 9224733..0cf0a42 100644 --- a/src/CBORObject.php +++ b/src/CBORObject.php @@ -2,62 +2,88 @@ 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 CBORObject { public const MAJOR_TYPE_UNSIGNED_INTEGER = 0b000; + public const MAJOR_TYPE_NEGATIVE_INTEGER = 0b001; + public const MAJOR_TYPE_BYTE_STRING = 0b010; + public const MAJOR_TYPE_TEXT_STRING = 0b011; + public const MAJOR_TYPE_LIST = 0b100; + public const MAJOR_TYPE_MAP = 0b101; + public const MAJOR_TYPE_TAG = 0b110; + public const MAJOR_TYPE_OTHER_TYPE = 0b111; public const LENGTH_1_BYTE = 0b00011000; + public const LENGTH_2_BYTES = 0b00011001; + public const LENGTH_4_BYTES = 0b00011010; + public const LENGTH_8_BYTES = 0b00011011; + public const LENGTH_INDEFINITE = 0b00011111; public const FUTURE_USE_1 = 0b00011100; + public const FUTURE_USE_2 = 0b00011101; + public const FUTURE_USE_3 = 0b00011110; public const OBJECT_FALSE = 20; + public const OBJECT_TRUE = 21; + public const OBJECT_NULL = 22; + public const OBJECT_UNDEFINED = 23; + public const OBJECT_SIMPLE_VALUE = 24; + public const OBJECT_HALF_PRECISION_FLOAT = 25; + public const OBJECT_SINGLE_PRECISION_FLOAT = 26; + public const OBJECT_DOUBLE_PRECISION_FLOAT = 27; + public const OBJECT_BREAK = 0b00011111; public const TAG_STANDARD_DATETIME = 0; + public const TAG_EPOCH_DATETIME = 1; + public const TAG_UNSIGNED_BIG_NUM = 2; + public const TAG_NEGATIVE_BIG_NUM = 3; + public const TAG_DECIMAL_FRACTION = 4; + public const TAG_BIG_FLOAT = 5; + public const TAG_ENCODED_BASE64_URL = 21; + public const TAG_ENCODED_BASE64 = 22; + public const TAG_ENCODED_BASE16 = 23; + public const TAG_ENCODED_CBOR = 24; + public const TAG_URI = 32; + public const TAG_BASE64_URL = 33; + public const TAG_BASE64 = 34; + public const TAG_MIME = 36; + public const TAG_CBOR = 55799; public function __toString(): string; diff --git a/src/Decoder.php b/src/Decoder.php index 450d676..c374e04 100644 --- a/src/Decoder.php +++ b/src/Decoder.php @@ -2,15 +2,6 @@ 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; use CBOR\OtherObject\BreakObject; @@ -44,6 +35,7 @@ use InvalidArgumentException; use function ord; use RuntimeException; +use const STR_PAD_LEFT; final class Decoder implements DecoderInterface { @@ -57,14 +49,18 @@ final class Decoder implements DecoderInterface */ private $otherTypeManager; - public function __construct(?TagManagerInterface $tagObjectManager = null, ?OtherObjectManagerInterface $otherTypeManager = null) - { + public function __construct( + ?TagManagerInterface $tagObjectManager = null, + ?OtherObjectManagerInterface $otherTypeManager = null + ) { $this->tagObjectManager = $tagObjectManager ?? $this->generateTagManager(); $this->otherTypeManager = $otherTypeManager ?? $this->generateOtherObjectManager(); } - public static function create(?TagManagerInterface $tagObjectManager = null, ?OtherObjectManagerInterface $otherTypeManager = null): self - { + public static function create( + ?TagManagerInterface $tagObjectManager = null, + ?OtherObjectManagerInterface $otherTypeManager = null + ): self { return new self($tagObjectManager, $otherTypeManager); } @@ -89,7 +85,11 @@ private function process(Stream $stream, bool $breakable): CBORObject case CBORObject::FUTURE_USE_1: //28 case CBORObject::FUTURE_USE_2: //29 case CBORObject::FUTURE_USE_3: //30 - throw new InvalidArgumentException(sprintf('Cannot parse the data. Found invalid Additional Information "%s" (%d).', str_pad(decbin($ai), 8, '0', STR_PAD_LEFT), $ai)); + throw new InvalidArgumentException(sprintf( + 'Cannot parse the data. Found invalid Additional Information "%s" (%d).', + str_pad(decbin($ai), 8, '0', STR_PAD_LEFT), + $ai + )); case CBORObject::LENGTH_INDEFINITE: //31 return $this->processInfinite($stream, $mt, $breakable); } @@ -105,16 +105,16 @@ private function processFinite(Stream $stream, int $mt, int $ai, ?string $val): case CBORObject::MAJOR_TYPE_NEGATIVE_INTEGER: //1 return NegativeIntegerObject::createObjectForValue($ai, $val); case CBORObject::MAJOR_TYPE_BYTE_STRING: //2 - $length = null === $val ? $ai : Utils::binToInt($val); + $length = $val === null ? $ai : Utils::binToInt($val); return ByteStringObject::create($stream->read($length)); case CBORObject::MAJOR_TYPE_TEXT_STRING: //3 - $length = null === $val ? $ai : Utils::binToInt($val); + $length = $val === null ? $ai : Utils::binToInt($val); return TextStringObject::create($stream->read($length)); case CBORObject::MAJOR_TYPE_LIST: //4 $object = ListObject::create(); - $nbItems = null === $val ? $ai : Utils::binToInt($val); + $nbItems = $val === null ? $ai : Utils::binToInt($val); for ($i = 0; $i < $nbItems; ++$i) { $object->add($this->process($stream, false)); } @@ -122,7 +122,7 @@ private function processFinite(Stream $stream, int $mt, int $ai, ?string $val): return $object; case CBORObject::MAJOR_TYPE_MAP: //5 $object = MapObject::create(); - $nbItems = null === $val ? $ai : Utils::binToInt($val); + $nbItems = $val === null ? $ai : Utils::binToInt($val); for ($i = 0; $i < $nbItems; ++$i) { $object->add($this->process($stream, false), $this->process($stream, false)); } @@ -133,7 +133,11 @@ private function processFinite(Stream $stream, int $mt, int $ai, ?string $val): case CBORObject::MAJOR_TYPE_OTHER_TYPE: //7 return $this->otherTypeManager->createObjectForValue($ai, $val); default: - throw new RuntimeException(sprintf('Unsupported major type "%s" (%d).', str_pad(decbin($mt), 5, '0', STR_PAD_LEFT), $mt)); // Should never append + throw new RuntimeException(sprintf( + 'Unsupported major type "%s" (%d).', + str_pad(decbin($mt), 5, '0', STR_PAD_LEFT), + $mt + )); // Should never append } } @@ -142,9 +146,11 @@ 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) { - if (!$it instanceof ByteStringObject) { - throw new RuntimeException('Unable to parse the data. Infinite Byte String object can only get Byte String objects.'); + while (! ($it = $this->process($stream, true)) instanceof BreakObject) { + if (! $it instanceof ByteStringObject) { + throw new RuntimeException( + 'Unable to parse the data. Infinite Byte String object can only get Byte String objects.' + ); } $object->add($it); } @@ -152,9 +158,11 @@ 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) { - if (!$it instanceof TextStringObject) { - throw new RuntimeException('Unable to parse the data. Infinite Text String object can only get Text String objects.'); + while (! ($it = $this->process($stream, true)) instanceof BreakObject) { + if (! $it instanceof TextStringObject) { + throw new RuntimeException( + 'Unable to parse the data. Infinite Text String object can only get Text String objects.' + ); } $object->add($it); } @@ -162,20 +170,22 @@ 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) { + $it = $this->process($stream, true); + while (! $it instanceof BreakObject) { $object->add($it); + $it = $this->process($stream, true); } 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 BreakObject) { $object->add($it, $this->process($stream, false)); } return $object; case CBORObject::MAJOR_TYPE_OTHER_TYPE: //7 - if (!$breakable) { + if (! $breakable) { throw new InvalidArgumentException('Cannot parse the data. No enclosing indefinite.'); } @@ -184,7 +194,11 @@ private function processInfinite(Stream $stream, int $mt, bool $breakable): CBOR 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)); + 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 + )); } } diff --git a/src/DecoderInterface.php b/src/DecoderInterface.php index 3d7b192..464eb8b 100644 --- a/src/DecoderInterface.php +++ b/src/DecoderInterface.php @@ -2,15 +2,6 @@ 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 diff --git a/src/IndefiniteLengthByteStringObject.php b/src/IndefiniteLengthByteStringObject.php index 09d7f88..f4dd3df 100644 --- a/src/IndefiniteLengthByteStringObject.php +++ b/src/IndefiniteLengthByteStringObject.php @@ -2,15 +2,6 @@ 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; /** @@ -19,6 +10,7 @@ class IndefiniteLengthByteStringObject extends AbstractCBORObject implements Normalizable { private const MAJOR_TYPE = self::MAJOR_TYPE_BYTE_STRING; + private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE; /** @@ -31,11 +23,6 @@ public function __construct() parent::__construct(self::MAJOR_TYPE, self::ADDITIONAL_INFORMATION); } - public static function create(): self - { - return new self(); - } - public function __toString(): string { $result = parent::__toString(); @@ -43,7 +30,12 @@ public function __toString(): string $result .= $chunk->__toString(); } - return $result."\xFF"; + return $result . "\xFF"; + } + + public static function create(): self + { + return new self(); } public function add(ByteStringObject $chunk): self diff --git a/src/IndefiniteLengthListObject.php b/src/IndefiniteLengthListObject.php index 435eebd..79087ff 100644 --- a/src/IndefiniteLengthListObject.php +++ b/src/IndefiniteLengthListObject.php @@ -2,15 +2,6 @@ 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; use function array_key_exists; @@ -30,6 +21,7 @@ class IndefiniteLengthListObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable, ArrayAccess { private const MAJOR_TYPE = self::MAJOR_TYPE_LIST; + private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE; /** @@ -42,11 +34,6 @@ public function __construct() parent::__construct(self::MAJOR_TYPE, self::ADDITIONAL_INFORMATION); } - public static function create(): self - { - return new self(); - } - public function __toString(): string { $result = parent::__toString(); @@ -54,7 +41,12 @@ public function __toString(): string $result .= (string) $object; } - return $result."\xFF"; + return $result . "\xFF"; + } + + public static function create(): self + { + return new self(); } /** @@ -91,7 +83,7 @@ public function has(int $index): bool public function remove(int $index): self { - if (!$this->has($index)) { + if (! $this->has($index)) { return $this; } unset($this->data[$index]); @@ -102,7 +94,7 @@ public function remove(int $index): self public function get(int $index): CBORObject { - if (!$this->has($index)) { + if (! $this->has($index)) { throw new InvalidArgumentException('Index not found.'); } @@ -111,7 +103,7 @@ public function get(int $index): CBORObject public function set(int $index, CBORObject $object): self { - if (!$this->has($index)) { + if (! $this->has($index)) { throw new InvalidArgumentException('Index not found.'); } @@ -148,7 +140,7 @@ public function offsetGet($offset): CBORObject public function offsetSet($offset, $value): void { - if (null === $offset) { + if ($offset === null) { $this->add($value); return; diff --git a/src/IndefiniteLengthMapObject.php b/src/IndefiniteLengthMapObject.php index 009ae74..d5f833a 100644 --- a/src/IndefiniteLengthMapObject.php +++ b/src/IndefiniteLengthMapObject.php @@ -2,15 +2,6 @@ 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; use function array_key_exists; @@ -23,13 +14,14 @@ use IteratorAggregate; /** - * @phpstan-implements ArrayAccess + * @phpstan-implements ArrayAccess * @phpstan-implements IteratorAggregate * @final */ class IndefiniteLengthMapObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable, ArrayAccess { private const MAJOR_TYPE = self::MAJOR_TYPE_MAP; + private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE; /** @@ -42,11 +34,6 @@ public function __construct() parent::__construct(self::MAJOR_TYPE, self::ADDITIONAL_INFORMATION); } - public static function create(): self - { - return new self(); - } - public function __toString(): string { $result = parent::__toString(); @@ -55,7 +42,12 @@ public function __toString(): string $result .= (string) $object->getValue(); } - return $result."\xFF"; + return $result . "\xFF"; + } + + public static function create(): self + { + return new self(); } /** @@ -68,7 +60,7 @@ public function append(CBORObject $key, CBORObject $value): self public function add(CBORObject $key, CBORObject $value): self { - if (!$key instanceof Normalizable) { + if (! $key instanceof Normalizable) { throw new InvalidArgumentException('Invalid key. Shall be normalizable'); } $this->data[$key->normalize()] = MapItem::create($key, $value); @@ -89,7 +81,7 @@ public function has($key): bool */ public function remove($index): self { - if (!$this->has($index)) { + if (! $this->has($index)) { return $this; } unset($this->data[$index]); @@ -101,19 +93,19 @@ public function remove($index): self /** * @param int|string $index */ - public function get($index): MapItem + public function get($index): CBORObject { - if (!$this->has($index)) { + if (! $this->has($index)) { throw new InvalidArgumentException('Index not found.'); } - return $this->data[$index]; + return $this->data[$index]->getValue(); } public function set(MapItem $object): self { $key = $object->getKey(); - if (!$key instanceof Normalizable) { + if (! $key instanceof Normalizable) { throw new InvalidArgumentException('Invalid key. Shall be normalizable'); } @@ -145,7 +137,7 @@ public function normalize(): array { return array_reduce($this->data, static function (array $carry, MapItem $item): array { $key = $item->getKey(); - if (!$key instanceof Normalizable) { + if (! $key instanceof Normalizable) { throw new InvalidArgumentException('Invalid key. Shall be normalizable'); } $valueObject = $item->getValue(); @@ -170,17 +162,17 @@ public function offsetExists($offset): bool return $this->has($offset); } - public function offsetGet($offset): MapItem + public function offsetGet($offset): CBORObject { return $this->get($offset); } public function offsetSet($offset, $value): void { - if (!$offset instanceof CBORObject) { + if (! $offset instanceof CBORObject) { throw new InvalidArgumentException('Invalid key'); } - if (!$value instanceof CBORObject) { + if (! $value instanceof CBORObject) { throw new InvalidArgumentException('Invalid value'); } diff --git a/src/IndefiniteLengthTextStringObject.php b/src/IndefiniteLengthTextStringObject.php index 3805731..659261d 100644 --- a/src/IndefiniteLengthTextStringObject.php +++ b/src/IndefiniteLengthTextStringObject.php @@ -2,15 +2,6 @@ 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; /** @@ -19,6 +10,7 @@ class IndefiniteLengthTextStringObject extends AbstractCBORObject implements Normalizable { private const MAJOR_TYPE = self::MAJOR_TYPE_TEXT_STRING; + private const ADDITIONAL_INFORMATION = self::LENGTH_INDEFINITE; /** @@ -31,11 +23,6 @@ public function __construct() parent::__construct(self::MAJOR_TYPE, self::ADDITIONAL_INFORMATION); } - public static function create(): self - { - return new self(); - } - public function __toString(): string { $result = parent::__toString(); @@ -43,7 +30,12 @@ public function __toString(): string $result .= (string) $object; } - return $result."\xFF"; + return $result . "\xFF"; + } + + public static function create(): self + { + return new self(); } public function add(TextStringObject $chunk): self diff --git a/src/InfiniteListObject.php b/src/InfiniteListObject.php index 526d74c..10287eb 100644 --- a/src/InfiniteListObject.php +++ b/src/InfiniteListObject.php @@ -2,15 +2,6 @@ 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; /** diff --git a/src/InfiniteMapObject.php b/src/InfiniteMapObject.php index efcf781..4213265 100644 --- a/src/InfiniteMapObject.php +++ b/src/InfiniteMapObject.php @@ -2,15 +2,6 @@ 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; /** diff --git a/src/LengthCalculator.php b/src/LengthCalculator.php index 53e4c12..0296e41 100644 --- a/src/LengthCalculator.php +++ b/src/LengthCalculator.php @@ -2,21 +2,13 @@ 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; use Brick\Math\BigInteger; use function chr; use function count; use InvalidArgumentException; +use const STR_PAD_LEFT; final class LengthCalculator { @@ -67,7 +59,7 @@ private static function hex2bin(string $data): string { $data = str_pad($data, (int) (2 ** ceil(log(mb_strlen($data, '8bit'), 2))), '0', STR_PAD_LEFT); $result = hex2bin($data); - if (false === $result) { + if ($result === false) { throw new InvalidArgumentException('Unable to convert the data'); } diff --git a/src/ListObject.php b/src/ListObject.php index 6f55db3..a56c6b5 100644 --- a/src/ListObject.php +++ b/src/ListObject.php @@ -2,15 +2,6 @@ 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; use function array_key_exists; @@ -47,7 +38,7 @@ public function __construct(array $data = []) { [$additionalInformation, $length] = LengthCalculator::getLengthOfArray($data); array_map(static function ($item): void { - if (!$item instanceof CBORObject) { + if (! $item instanceof CBORObject) { throw new InvalidArgumentException('The list must contain only CBORObject objects.'); } }, $data); @@ -57,18 +48,10 @@ public function __construct(array $data = []) $this->length = $length; } - /** - * @param CBORObject[] $data - */ - public static function create(array $data = []): self - { - return new self($data); - } - public function __toString(): string { $result = parent::__toString(); - if (null !== $this->length) { + if ($this->length !== null) { $result .= $this->length; } foreach ($this->data as $object) { @@ -78,6 +61,14 @@ public function __toString(): string return $result; } + /** + * @param CBORObject[] $data + */ + public static function create(array $data = []): self + { + return new self($data); + } + public function add(CBORObject $object): self { $this->data[] = $object; @@ -93,7 +84,7 @@ public function has(int $index): bool public function remove(int $index): self { - if (!$this->has($index)) { + if (! $this->has($index)) { return $this; } unset($this->data[$index]); @@ -105,7 +96,7 @@ public function remove(int $index): self public function get(int $index): CBORObject { - if (!$this->has($index)) { + if (! $this->has($index)) { throw new InvalidArgumentException('Index not found.'); } @@ -114,7 +105,7 @@ public function get(int $index): CBORObject public function set(int $index, CBORObject $object): self { - if (!$this->has($index)) { + if (! $this->has($index)) { throw new InvalidArgumentException('Index not found.'); } @@ -169,7 +160,7 @@ public function offsetGet($offset): CBORObject public function offsetSet($offset, $value): void { - if (null === $offset) { + if ($offset === null) { $this->add($value); return; diff --git a/src/MapItem.php b/src/MapItem.php index 39578a6..f0a8678 100644 --- a/src/MapItem.php +++ b/src/MapItem.php @@ -2,15 +2,6 @@ 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; class MapItem diff --git a/src/MapObject.php b/src/MapObject.php index 890585f..1a0629e 100644 --- a/src/MapObject.php +++ b/src/MapObject.php @@ -2,15 +2,6 @@ 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; use function array_key_exists; @@ -23,7 +14,7 @@ use IteratorAggregate; /** - * @phpstan-implements ArrayAccess + * @phpstan-implements ArrayAccess * @phpstan-implements IteratorAggregate */ final class MapObject extends AbstractCBORObject implements Countable, IteratorAggregate, Normalizable, ArrayAccess @@ -47,7 +38,7 @@ public function __construct(array $data = []) { [$additionalInformation, $length] = LengthCalculator::getLengthOfArray($data); array_map(static function ($item): void { - if (!$item instanceof MapItem) { + if (! $item instanceof MapItem) { throw new InvalidArgumentException('The list must contain only MapItem objects.'); } }, $data); @@ -57,31 +48,35 @@ public function __construct(array $data = []) $this->length = $length; } - /** - * @param MapItem[] $data - */ - public static function create(array $data = []): self - { - return new self($data); - } - public function __toString(): string { $result = parent::__toString(); - if (null !== $this->length) { + if ($this->length !== null) { $result .= $this->length; } foreach ($this->data as $object) { - $result .= $object->getKey()->__toString(); - $result .= $object->getValue()->__toString(); + $result .= $object->getKey() + ->__toString() + ; + $result .= $object->getValue() + ->__toString() + ; } return $result; } + /** + * @param MapItem[] $data + */ + public static function create(array $data = []): self + { + return new self($data); + } + public function add(CBORObject $key, CBORObject $value): self { - if (!$key instanceof Normalizable) { + if (! $key instanceof Normalizable) { throw new InvalidArgumentException('Invalid key. Shall be normalizable'); } $this->data[$key->normalize()] = MapItem::create($key, $value); @@ -103,7 +98,7 @@ public function has($key): bool */ public function remove($index): self { - if (!$this->has($index)) { + if (! $this->has($index)) { return $this; } unset($this->data[$index]); @@ -116,19 +111,19 @@ public function remove($index): self /** * @param int|string $index */ - public function get($index): MapItem + public function get($index): CBORObject { - if (!$this->has($index)) { + if (! $this->has($index)) { throw new InvalidArgumentException('Index not found.'); } - return $this->data[$index]; + return $this->data[$index]->getValue(); } public function set(MapItem $object): self { $key = $object->getKey(); - if (!$key instanceof Normalizable) { + if (! $key instanceof Normalizable) { throw new InvalidArgumentException('Invalid key. Shall be normalizable'); } @@ -158,7 +153,7 @@ public function normalize(): array { return array_reduce($this->data, static function (array $carry, MapItem $item): array { $key = $item->getKey(); - if (!$key instanceof Normalizable) { + if (! $key instanceof Normalizable) { throw new InvalidArgumentException('Invalid key. Shall be normalizable'); } $valueObject = $item->getValue(); @@ -183,17 +178,17 @@ public function offsetExists($offset): bool return $this->has($offset); } - public function offsetGet($offset): MapItem + public function offsetGet($offset): CBORObject { return $this->get($offset); } public function offsetSet($offset, $value): void { - if (!$offset instanceof CBORObject) { + if (! $offset instanceof CBORObject) { throw new InvalidArgumentException('Invalid key'); } - if (!$value instanceof CBORObject) { + if (! $value instanceof CBORObject) { throw new InvalidArgumentException('Invalid value'); } diff --git a/src/NegativeIntegerObject.php b/src/NegativeIntegerObject.php index 513dfd7..260ea02 100644 --- a/src/NegativeIntegerObject.php +++ b/src/NegativeIntegerObject.php @@ -2,19 +2,11 @@ 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; use Brick\Math\BigInteger; use InvalidArgumentException; +use const STR_PAD_LEFT; /** * @final @@ -37,7 +29,7 @@ public function __construct(int $additionalInformation, ?string $data) public function __toString(): string { $result = parent::__toString(); - if (null !== $this->data) { + if ($this->data !== null) { $result .= $this->data; } @@ -63,14 +55,16 @@ public static function createFromString(string $value): self public function getValue(): string { - if (null === $this->data) { + if ($this->data === null) { return (string) (-1 - $this->additionalInformation); } $result = Utils::binToBigInteger($this->data); $minusOne = BigInteger::of(-1); - return $minusOne->minus($result)->toBase(10); + return $minusOne->minus($result) + ->toBase(10) + ; } public function normalize(): string @@ -113,7 +107,9 @@ private static function createBigInteger(BigInteger $integer): self $data = self::hex2bin(str_pad($computed_value->toBase(16), 8, '0', STR_PAD_LEFT)); break; default: - throw new InvalidArgumentException('Out of range. Please use NegativeBigIntegerTag tag with ByteStringObject object instead.'); + throw new InvalidArgumentException( + 'Out of range. Please use NegativeBigIntegerTag tag with ByteStringObject object instead.' + ); } return new self($ai, $data); @@ -122,7 +118,7 @@ private static function createBigInteger(BigInteger $integer): self private static function hex2bin(string $data): string { $result = hex2bin($data); - if (false === $result) { + if ($result === false) { throw new InvalidArgumentException('Unable to convert the data'); } diff --git a/src/Normalizable.php b/src/Normalizable.php index 3b77662..7ff12bf 100644 --- a/src/Normalizable.php +++ b/src/Normalizable.php @@ -2,15 +2,6 @@ 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 Normalizable diff --git a/src/OtherObject.php b/src/OtherObject.php index cc86a74..1ff052a 100644 --- a/src/OtherObject.php +++ b/src/OtherObject.php @@ -2,15 +2,6 @@ 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 OtherObject extends AbstractCBORObject @@ -31,7 +22,7 @@ public function __construct(int $additionalInformation, ?string $data) public function __toString(): string { $result = parent::__toString(); - if (null !== $this->data) { + if ($this->data !== null) { $result .= $this->data; } diff --git a/src/OtherObject/BreakObject.php b/src/OtherObject/BreakObject.php index 60c841e..b370cfa 100644 --- a/src/OtherObject/BreakObject.php +++ b/src/OtherObject/BreakObject.php @@ -2,15 +2,6 @@ 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 as Base; diff --git a/src/OtherObject/DoublePrecisionFloatObject.php b/src/OtherObject/DoublePrecisionFloatObject.php index cc2dbf7..966aa07 100644 --- a/src/OtherObject/DoublePrecisionFloatObject.php +++ b/src/OtherObject/DoublePrecisionFloatObject.php @@ -2,22 +2,15 @@ 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 Brick\Math\BigInteger; use CBOR\Normalizable; use CBOR\OtherObject as Base; use CBOR\Utils; +use const INF; use InvalidArgumentException; +use const NAN; final class DoublePrecisionFloatObject extends Base implements Normalizable { @@ -33,7 +26,7 @@ public static function createFromLoadedData(int $additionalInformation, ?string public static function create(string $value): self { - if (8 !== mb_strlen($value, '8bit')) { + if (mb_strlen($value, '8bit') !== 8) { throw new InvalidArgumentException('The value is not a valid double precision floating point'); } @@ -57,12 +50,12 @@ public function normalize() $mantissa = $this->getMantissa(); $sign = $this->getSign(); - if (0 === $exponent) { + if ($exponent === 0) { $val = $mantissa * 2 ** (-(1022 + 52)); - } elseif (0b11111111111 !== $exponent) { + } elseif ($exponent !== 0b11111111111) { $val = ($mantissa + (1 << 52)) * 2 ** ($exponent - (1023 + 52)); } else { - $val = 0 === $mantissa ? INF : NAN; + $val = $mantissa === 0 ? INF : NAN; } return $sign * $val; diff --git a/src/OtherObject/FalseObject.php b/src/OtherObject/FalseObject.php index f08adb2..c4162b4 100644 --- a/src/OtherObject/FalseObject.php +++ b/src/OtherObject/FalseObject.php @@ -2,15 +2,6 @@ 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\Normalizable; diff --git a/src/OtherObject/GenericObject.php b/src/OtherObject/GenericObject.php index 33ce776..e4ffe3e 100644 --- a/src/OtherObject/GenericObject.php +++ b/src/OtherObject/GenericObject.php @@ -2,15 +2,6 @@ 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 as Base; @@ -26,7 +17,7 @@ public static function supportedAdditionalInformation(): array public static function createFromLoadedData(int $additionalInformation, ?string $data): Base { - if (null !== $data && ord($data) < 32) { + if ($data !== null && ord($data) < 32) { throw new InvalidArgumentException('Invalid simple value. Content data should not be present.'); } diff --git a/src/OtherObject/HalfPrecisionFloatObject.php b/src/OtherObject/HalfPrecisionFloatObject.php index 1e7ea72..f93bee5 100644 --- a/src/OtherObject/HalfPrecisionFloatObject.php +++ b/src/OtherObject/HalfPrecisionFloatObject.php @@ -2,22 +2,15 @@ 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 Brick\Math\BigInteger; use CBOR\Normalizable; use CBOR\OtherObject as Base; use CBOR\Utils; +use const INF; use InvalidArgumentException; +use const NAN; final class HalfPrecisionFloatObject extends Base implements Normalizable { @@ -33,7 +26,7 @@ public static function createFromLoadedData(int $additionalInformation, ?string public static function create(string $value): self { - if (2 !== mb_strlen($value, '8bit')) { + if (mb_strlen($value, '8bit') !== 2) { throw new InvalidArgumentException('The value is not a valid half precision floating point'); } @@ -57,12 +50,12 @@ public function normalize() $mantissa = $this->getMantissa(); $sign = $this->getSign(); - if (0 === $exponent) { + if ($exponent === 0) { $val = $mantissa * 2 ** (-24); - } elseif (0b11111 !== $exponent) { + } elseif ($exponent !== 0b11111) { $val = ($mantissa + (1 << 10)) * 2 ** ($exponent - 25); } else { - $val = 0 === $mantissa ? INF : NAN; + $val = $mantissa === 0 ? INF : NAN; } return $sign * $val; diff --git a/src/OtherObject/NullObject.php b/src/OtherObject/NullObject.php index 9ab68d6..eeb90e6 100644 --- a/src/OtherObject/NullObject.php +++ b/src/OtherObject/NullObject.php @@ -2,15 +2,6 @@ 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\Normalizable; diff --git a/src/OtherObject/OtherObjectManager.php b/src/OtherObject/OtherObjectManager.php index 9306f81..ba90664 100644 --- a/src/OtherObject/OtherObjectManager.php +++ b/src/OtherObject/OtherObjectManager.php @@ -2,15 +2,6 @@ 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 function array_key_exists; diff --git a/src/OtherObject/OtherObjectManagerInterface.php b/src/OtherObject/OtherObjectManagerInterface.php index 6c0336d..b0b2f14 100644 --- a/src/OtherObject/OtherObjectManagerInterface.php +++ b/src/OtherObject/OtherObjectManagerInterface.php @@ -2,15 +2,6 @@ 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; diff --git a/src/OtherObject/SimpleObject.php b/src/OtherObject/SimpleObject.php index 892a6a3..067dfdd 100644 --- a/src/OtherObject/SimpleObject.php +++ b/src/OtherObject/SimpleObject.php @@ -2,15 +2,6 @@ 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\Normalizable; @@ -29,18 +20,18 @@ public static function supportedAdditionalInformation(): array public static function createFromLoadedData(int $additionalInformation, ?string $data): Base { - if (24 === $additionalInformation) { - if (null === $data) { + if ($additionalInformation === 24) { + if ($data === null) { throw new InvalidArgumentException('Invalid simple value. Content data is missing.'); } - if (1 !== mb_strlen($data, '8bit')) { + if (mb_strlen($data, '8bit') !== 1) { throw new InvalidArgumentException('Invalid simple value. Content data is too long.'); } if (ord($data) < 32) { throw new InvalidArgumentException('Invalid simple value. Content data must be between 32 and 255.'); } } elseif ($additionalInformation < 20) { - if (null !== $data) { + if ($data !== null) { throw new InvalidArgumentException('Invalid simple value. Content data should not be present.'); } } @@ -50,7 +41,7 @@ public static function createFromLoadedData(int $additionalInformation, ?string public function normalize(): int { - if (null === $this->data) { + if ($this->data === null) { return $this->getAdditionalInformation(); } diff --git a/src/OtherObject/SinglePrecisionFloatObject.php b/src/OtherObject/SinglePrecisionFloatObject.php index 389ab5a..a8e5ac7 100644 --- a/src/OtherObject/SinglePrecisionFloatObject.php +++ b/src/OtherObject/SinglePrecisionFloatObject.php @@ -2,21 +2,14 @@ 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 Brick\Math\BigInteger; use CBOR\OtherObject as Base; use CBOR\Utils; +use const INF; use InvalidArgumentException; +use const NAN; final class SinglePrecisionFloatObject extends Base { @@ -32,7 +25,7 @@ public static function createFromLoadedData(int $additionalInformation, ?string public static function create(string $value): self { - if (4 !== mb_strlen($value, '8bit')) { + if (mb_strlen($value, '8bit') !== 4) { throw new InvalidArgumentException('The value is not a valid single precision floating point'); } @@ -56,12 +49,12 @@ public function normalize() $mantissa = $this->getMantissa(); $sign = $this->getSign(); - if (0 === $exponent) { + if ($exponent === 0) { $val = $mantissa * 2 ** (-(126 + 23)); - } elseif (0b11111111 !== $exponent) { + } elseif ($exponent !== 0b11111111) { $val = ($mantissa + (1 << 23)) * 2 ** ($exponent - (127 + 23)); } else { - $val = 0 === $mantissa ? INF : NAN; + $val = $mantissa === 0 ? INF : NAN; } return $sign * $val; diff --git a/src/OtherObject/TrueObject.php b/src/OtherObject/TrueObject.php index e6f1591..d01d5d6 100644 --- a/src/OtherObject/TrueObject.php +++ b/src/OtherObject/TrueObject.php @@ -2,15 +2,6 @@ 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\Normalizable; diff --git a/src/OtherObject/UndefinedObject.php b/src/OtherObject/UndefinedObject.php index 3743afe..46cb0a3 100644 --- a/src/OtherObject/UndefinedObject.php +++ b/src/OtherObject/UndefinedObject.php @@ -2,15 +2,6 @@ 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 as Base; diff --git a/src/SignedIntegerObject.php b/src/SignedIntegerObject.php index 4526f99..167c59e 100644 --- a/src/SignedIntegerObject.php +++ b/src/SignedIntegerObject.php @@ -2,15 +2,6 @@ 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; /** diff --git a/src/Stream.php b/src/Stream.php index fbaa435..2df6e99 100644 --- a/src/Stream.php +++ b/src/Stream.php @@ -2,15 +2,6 @@ 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 Stream diff --git a/src/StringStream.php b/src/StringStream.php index 4a23cc4..d522813 100644 --- a/src/StringStream.php +++ b/src/StringStream.php @@ -2,15 +2,6 @@ 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; use InvalidArgumentException; @@ -26,15 +17,15 @@ final class StringStream implements Stream public function __construct(string $data) { $resource = fopen('php://memory', 'rb+'); - if (false === $resource) { + if ($resource === false) { throw new RuntimeException('Unable to open the memory'); } $result = fwrite($resource, $data); - if (false === $result) { + if ($result === false) { throw new RuntimeException('Unable to write the memory'); } $result = rewind($resource); - if (false === $result) { + if ($result === false) { throw new RuntimeException('Unable to rewind the memory'); } $this->resource = $resource; @@ -47,7 +38,7 @@ public static function create(string $data): self public function read(int $length): string { - if (0 === $length) { + if ($length === 0) { return ''; } @@ -55,21 +46,29 @@ public function read(int $length): string $data = ''; while ($alreadyRead < $length) { $left = $length - $alreadyRead; - $sizeToRead = $left < 1024 ? $left : 1024; + $sizeToRead = $left < 1024 && $left > 0 ? $left : 1024; $newData = fread($this->resource, $sizeToRead); $alreadyRead += $sizeToRead; - if (false === $newData) { + if ($newData === false) { throw new RuntimeException('Unable to read the memory'); } if (mb_strlen($newData, '8bit') < $sizeToRead) { - throw new InvalidArgumentException(sprintf('Out of range. Expected: %d, read: %d.', $length, mb_strlen($data, '8bit'))); + throw new InvalidArgumentException(sprintf( + 'Out of range. Expected: %d, read: %d.', + $length, + mb_strlen($data, '8bit') + )); } $data .= $newData; } if (mb_strlen($data, '8bit') !== $length) { - throw new InvalidArgumentException(sprintf('Out of range. Expected: %d, read: %d.', $length, mb_strlen($data, '8bit'))); + throw new InvalidArgumentException(sprintf( + 'Out of range. Expected: %d, read: %d.', + $length, + mb_strlen($data, '8bit') + )); } return $data; diff --git a/src/Tag.php b/src/Tag.php index 3d2a223..8bcd121 100644 --- a/src/Tag.php +++ b/src/Tag.php @@ -2,15 +2,6 @@ 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; use InvalidArgumentException; @@ -39,11 +30,11 @@ public function __construct(int $additionalInformation, ?string $data, CBORObjec public function __toString(): string { $result = parent::__toString(); - if (null !== $this->data) { + if ($this->data !== null) { $result .= $this->data; } - return $result.$this->object; + return $result . $this->object; } public function getData(): ?string @@ -53,7 +44,11 @@ public function getData(): ?string abstract public static function getTagId(): int; - abstract public static function createFromLoadedData(int $additionalInformation, ?string $data, CBORObject $object): self; + abstract public static function createFromLoadedData( + int $additionalInformation, + ?string $data, + CBORObject $object + ): self; public function getValue(): CBORObject { @@ -77,14 +72,16 @@ protected static function determineComponents(int $tag): array case $tag < 0xFFFFFFFF: return [26, self::hex2bin(dechex($tag))]; default: - throw new InvalidArgumentException('Out of range. Please use PositiveBigIntegerTag tag with ByteStringObject object instead.'); + throw new InvalidArgumentException( + 'Out of range. Please use PositiveBigIntegerTag tag with ByteStringObject object instead.' + ); } } private static function hex2bin(string $data): string { $result = hex2bin($data); - if (false === $result) { + if ($result === false) { throw new InvalidArgumentException('Unable to convert the data'); } diff --git a/src/Tag/Base16EncodingTag.php b/src/Tag/Base16EncodingTag.php index 1e75ae8..41eb398 100644 --- a/src/Tag/Base16EncodingTag.php +++ b/src/Tag/Base16EncodingTag.php @@ -2,15 +2,6 @@ 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\Tag; use CBOR\ByteStringObject; @@ -48,7 +39,7 @@ public function getNormalizedData(bool $ignoreTags = false) return $this->object->getNormalizedData($ignoreTags); } - if (!$this->object instanceof ByteStringObject && !$this->object instanceof IndefiniteLengthByteStringObject && !$this->object instanceof TextStringObject && !$this->object instanceof IndefiniteLengthTextStringObject) { + if (! $this->object instanceof ByteStringObject && ! $this->object instanceof IndefiniteLengthByteStringObject && ! $this->object instanceof TextStringObject && ! $this->object instanceof IndefiniteLengthTextStringObject) { return $this->object->getNormalizedData($ignoreTags); } diff --git a/src/Tag/Base64EncodingTag.php b/src/Tag/Base64EncodingTag.php index a689700..5a525c0 100644 --- a/src/Tag/Base64EncodingTag.php +++ b/src/Tag/Base64EncodingTag.php @@ -2,15 +2,6 @@ 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\Tag; use CBOR\ByteStringObject; @@ -49,12 +40,12 @@ public function getNormalizedData(bool $ignoreTags = false) return $this->object->getNormalizedData($ignoreTags); } - if (!$this->object instanceof ByteStringObject && !$this->object instanceof IndefiniteLengthByteStringObject && !$this->object instanceof TextStringObject && !$this->object instanceof IndefiniteLengthTextStringObject) { + if (! $this->object instanceof ByteStringObject && ! $this->object instanceof IndefiniteLengthByteStringObject && ! $this->object instanceof TextStringObject && ! $this->object instanceof IndefiniteLengthTextStringObject) { return $this->object->getNormalizedData($ignoreTags); } $result = base64_decode($this->object->getNormalizedData($ignoreTags), true); - if (false === $result) { + if ($result === false) { throw new InvalidArgumentException('Unable to decode the data'); } diff --git a/src/Tag/Base64Tag.php b/src/Tag/Base64Tag.php index fc8178c..cd54e87 100644 --- a/src/Tag/Base64Tag.php +++ b/src/Tag/Base64Tag.php @@ -2,15 +2,6 @@ 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\Tag; use CBOR\CBORObject; @@ -23,7 +14,7 @@ final class Base64Tag extends Tag { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { - if (!$object instanceof TextStringObject && !$object instanceof IndefiniteLengthTextStringObject) { + if (! $object instanceof TextStringObject && ! $object instanceof IndefiniteLengthTextStringObject) { throw new InvalidArgumentException('This tag only accepts a Text String object.'); } diff --git a/src/Tag/Base64UrlEncodingTag.php b/src/Tag/Base64UrlEncodingTag.php index 48fa74f..5e6d620 100644 --- a/src/Tag/Base64UrlEncodingTag.php +++ b/src/Tag/Base64UrlEncodingTag.php @@ -2,15 +2,6 @@ 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\Tag; use CBOR\ByteStringObject; @@ -49,7 +40,7 @@ public function getNormalizedData(bool $ignoreTags = false) return $this->object->getNormalizedData($ignoreTags); } - if (!$this->object instanceof ByteStringObject && !$this->object instanceof IndefiniteLengthByteStringObject && !$this->object instanceof TextStringObject && !$this->object instanceof IndefiniteLengthTextStringObject) { + if (! $this->object instanceof ByteStringObject && ! $this->object instanceof IndefiniteLengthByteStringObject && ! $this->object instanceof TextStringObject && ! $this->object instanceof IndefiniteLengthTextStringObject) { return $this->object->getNormalizedData($ignoreTags); } diff --git a/src/Tag/Base64UrlTag.php b/src/Tag/Base64UrlTag.php index 20e66a6..f4752ee 100644 --- a/src/Tag/Base64UrlTag.php +++ b/src/Tag/Base64UrlTag.php @@ -2,15 +2,6 @@ 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\Tag; use CBOR\CBORObject; @@ -23,7 +14,7 @@ final class Base64UrlTag extends Tag { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { - if (!$object instanceof TextStringObject && !$object instanceof IndefiniteLengthTextStringObject) { + if (! $object instanceof TextStringObject && ! $object instanceof IndefiniteLengthTextStringObject) { throw new InvalidArgumentException('This tag only accepts a Text String object.'); } diff --git a/src/Tag/BigFloatTag.php b/src/Tag/BigFloatTag.php index f26665f..af04acc 100644 --- a/src/Tag/BigFloatTag.php +++ b/src/Tag/BigFloatTag.php @@ -2,15 +2,6 @@ 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\Tag; use CBOR\CBORObject; @@ -28,20 +19,24 @@ final class BigFloatTag extends Tag implements Normalizable { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { - if (!extension_loaded('bcmath')) { + if (! extension_loaded('bcmath')) { throw new RuntimeException('The extension "bcmath" is required to use this tag'); } - if (!$object instanceof ListObject || 2 !== count($object)) { - throw new InvalidArgumentException('This tag only accepts a ListObject object that contains an exponent and a mantissa.'); + if (! $object instanceof ListObject || count($object) !== 2) { + throw new InvalidArgumentException( + 'This tag only accepts a ListObject object that contains an exponent and a mantissa.' + ); } $e = $object->get(0); - if (!$e instanceof UnsignedIntegerObject && !$e instanceof NegativeIntegerObject) { + if (! $e instanceof UnsignedIntegerObject && ! $e instanceof NegativeIntegerObject) { throw new InvalidArgumentException('The exponent must be a Signed Integer or an Unsigned Integer object.'); } $m = $object->get(1); - if (!$m instanceof UnsignedIntegerObject && !$m instanceof NegativeIntegerObject && !$m instanceof NegativeBigIntegerTag && !$m instanceof UnsignedBigIntegerTag) { - throw new InvalidArgumentException('The mantissa must be a Positive or Negative Signed Integer or an Unsigned Integer object.'); + if (! $m instanceof UnsignedIntegerObject && ! $m instanceof NegativeIntegerObject && ! $m instanceof NegativeBigIntegerTag && ! $m instanceof UnsignedBigIntegerTag) { + throw new InvalidArgumentException( + 'The mantissa must be a Positive or Negative Signed Integer or an Unsigned Integer object.' + ); } parent::__construct($additionalInformation, $data, $object); @@ -83,16 +78,7 @@ public function normalize() /** @var UnsignedIntegerObject|NegativeIntegerObject|NegativeBigIntegerTag|UnsignedBigIntegerTag $m */ $m = $object->get(1); - return rtrim( - bcmul( - $m->normalize(), - bcpow( - '2', - $e->normalize(), - 100), - 100), - '0' - ); + return rtrim(bcmul($m->normalize(), bcpow('2', $e->normalize(), 100), 100), '0'); } /** @@ -104,7 +90,7 @@ public function getNormalizedData(bool $ignoreTags = false) return $this->object->getNormalizedData($ignoreTags); } - if (!$this->object instanceof ListObject || 2 !== count($this->object)) { + if (! $this->object instanceof ListObject || count($this->object) !== 2) { return $this->object->getNormalizedData($ignoreTags); } diff --git a/src/Tag/CBOREncodingTag.php b/src/Tag/CBOREncodingTag.php index e9b7fc5..7d082a7 100644 --- a/src/Tag/CBOREncodingTag.php +++ b/src/Tag/CBOREncodingTag.php @@ -2,15 +2,6 @@ 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\Tag; use CBOR\ByteStringObject; @@ -23,7 +14,7 @@ final class CBOREncodingTag extends Tag { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { - if (!$object instanceof ByteStringObject && !$object instanceof IndefiniteLengthByteStringObject) { + if (! $object instanceof ByteStringObject && ! $object instanceof IndefiniteLengthByteStringObject) { throw new InvalidArgumentException('This tag only accepts a Byte String object.'); } diff --git a/src/Tag/CBORTag.php b/src/Tag/CBORTag.php index 2ab74fe..b0af63f 100644 --- a/src/Tag/CBORTag.php +++ b/src/Tag/CBORTag.php @@ -2,15 +2,6 @@ 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\Tag; use CBOR\CBORObject; diff --git a/src/Tag/DatetimeTag.php b/src/Tag/DatetimeTag.php index 8760a3f..710864e 100644 --- a/src/Tag/DatetimeTag.php +++ b/src/Tag/DatetimeTag.php @@ -2,15 +2,6 @@ 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\Tag; use CBOR\CBORObject; @@ -18,6 +9,7 @@ use CBOR\Normalizable; use CBOR\Tag; use CBOR\TextStringObject; +use const DATE_RFC3339; use DateTimeImmutable; use DateTimeInterface; use InvalidArgumentException; @@ -29,7 +21,7 @@ class DatetimeTag extends Tag implements Normalizable { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { - if (!$object instanceof TextStringObject && !$object instanceof IndefiniteLengthTextStringObject) { + if (! $object instanceof TextStringObject && ! $object instanceof IndefiniteLengthTextStringObject) { throw new InvalidArgumentException('This tag only accepts a Byte String object.'); } parent::__construct($additionalInformation, $data, $object); @@ -57,12 +49,12 @@ public function normalize(): DateTimeInterface /** @var TextStringObject|IndefiniteLengthTextStringObject $object */ $object = $this->object; $result = DateTimeImmutable::createFromFormat(DATE_RFC3339, $object->normalize()); - if (false !== $result) { + if ($result !== false) { return $result; } $formatted = DateTimeImmutable::createFromFormat('Y-m-d\TH:i:s.uP', $object->normalize()); - if (false === $formatted) { + if ($formatted === false) { throw new InvalidArgumentException('Invalid data. Cannot be converted into a datetime object'); } diff --git a/src/Tag/DecimalFractionTag.php b/src/Tag/DecimalFractionTag.php index 1e9ca32..dc25fd6 100644 --- a/src/Tag/DecimalFractionTag.php +++ b/src/Tag/DecimalFractionTag.php @@ -2,15 +2,6 @@ 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\Tag; use CBOR\CBORObject; @@ -28,19 +19,23 @@ final class DecimalFractionTag extends Tag implements Normalizable { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { - if (!extension_loaded('bcmath')) { + if (! extension_loaded('bcmath')) { throw new RuntimeException('The extension "bcmath" is required to use this tag'); } - if (!$object instanceof ListObject || 2 !== count($object)) { - throw new InvalidArgumentException('This tag only accepts a ListObject object that contains an exponent and a mantissa.'); + if (! $object instanceof ListObject || count($object) !== 2) { + throw new InvalidArgumentException( + 'This tag only accepts a ListObject object that contains an exponent and a mantissa.' + ); } $e = $object->get(0); - if (!$e instanceof UnsignedIntegerObject && !$e instanceof NegativeIntegerObject) { + if (! $e instanceof UnsignedIntegerObject && ! $e instanceof NegativeIntegerObject) { throw new InvalidArgumentException('The exponent must be a Signed Integer or an Unsigned Integer object.'); } $m = $object->get(1); - if (!$m instanceof UnsignedIntegerObject && !$m instanceof NegativeIntegerObject && !$m instanceof NegativeBigIntegerTag && !$m instanceof UnsignedBigIntegerTag) { - throw new InvalidArgumentException('The mantissa must be a Positive or Negative Signed Integer or an Unsigned Integer object.'); + if (! $m instanceof UnsignedIntegerObject && ! $m instanceof NegativeIntegerObject && ! $m instanceof NegativeBigIntegerTag && ! $m instanceof UnsignedBigIntegerTag) { + throw new InvalidArgumentException( + 'The mantissa must be a Positive or Negative Signed Integer or an Unsigned Integer object.' + ); } parent::__construct($additionalInformation, $data, $object); @@ -82,16 +77,7 @@ public function normalize() /** @var UnsignedIntegerObject|NegativeIntegerObject|NegativeBigIntegerTag|UnsignedBigIntegerTag $m */ $m = $object->get(1); - return rtrim( - bcmul( - $m->normalize(), - bcpow( - '10', - $e->normalize(), - 100), - 100), - '0' - ); + return rtrim(bcmul($m->normalize(), bcpow('10', $e->normalize(), 100), 100), '0'); } /** @@ -103,16 +89,16 @@ public function getNormalizedData(bool $ignoreTags = false) return $this->object->getNormalizedData($ignoreTags); } - if (!$this->object instanceof ListObject || 2 !== count($this->object)) { + if (! $this->object instanceof ListObject || count($this->object) !== 2) { return $this->object->getNormalizedData($ignoreTags); } $e = $this->object->get(0); $m = $this->object->get(1); - if (!$e instanceof UnsignedIntegerObject && !$e instanceof NegativeIntegerObject) { + if (! $e instanceof UnsignedIntegerObject && ! $e instanceof NegativeIntegerObject) { return $this->object->getNormalizedData($ignoreTags); } - if (!$m instanceof UnsignedIntegerObject && !$m instanceof NegativeIntegerObject && !$m instanceof NegativeBigIntegerTag && !$m instanceof UnsignedBigIntegerTag) { + if (! $m instanceof UnsignedIntegerObject && ! $m instanceof NegativeIntegerObject && ! $m instanceof NegativeBigIntegerTag && ! $m instanceof UnsignedBigIntegerTag) { return $this->object->getNormalizedData($ignoreTags); } diff --git a/src/Tag/EpochTag.php b/src/Tag/EpochTag.php index aa00c73..8bc4ce5 100644 --- a/src/Tag/EpochTag.php +++ b/src/Tag/EpochTag.php @@ -2,15 +2,6 @@ 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\Tag; /** diff --git a/src/Tag/GenericTag.php b/src/Tag/GenericTag.php index 8239e9c..d4ca14d 100644 --- a/src/Tag/GenericTag.php +++ b/src/Tag/GenericTag.php @@ -2,15 +2,6 @@ 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\Tag; use CBOR\CBORObject; diff --git a/src/Tag/MimeTag.php b/src/Tag/MimeTag.php index f01a44e..fa45393 100644 --- a/src/Tag/MimeTag.php +++ b/src/Tag/MimeTag.php @@ -2,15 +2,6 @@ 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\Tag; use CBOR\CBORObject; @@ -24,7 +15,7 @@ final class MimeTag extends Tag implements Normalizable { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { - if (!$object instanceof TextStringObject && !$object instanceof IndefiniteLengthTextStringObject) { + if (! $object instanceof TextStringObject && ! $object instanceof IndefiniteLengthTextStringObject) { throw new InvalidArgumentException('This tag only accepts a Byte String object.'); } diff --git a/src/Tag/NegativeBigIntegerTag.php b/src/Tag/NegativeBigIntegerTag.php index 38872bf..e7e9375 100644 --- a/src/Tag/NegativeBigIntegerTag.php +++ b/src/Tag/NegativeBigIntegerTag.php @@ -2,15 +2,6 @@ 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\Tag; use Brick\Math\BigInteger; @@ -25,7 +16,7 @@ final class NegativeBigIntegerTag extends Tag implements Normalizable { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { - if (!$object instanceof ByteStringObject && !$object instanceof IndefiniteLengthByteStringObject) { + if (! $object instanceof ByteStringObject && ! $object instanceof IndefiniteLengthByteStringObject) { throw new InvalidArgumentException('This tag only accepts a Byte String object.'); } @@ -56,7 +47,9 @@ public function normalize(): string $integer = BigInteger::fromBase(bin2hex($object->getValue()), 16); $minusOne = BigInteger::of(-1); - return $minusOne->minus($integer)->toBase(10); + return $minusOne->minus($integer) + ->toBase(10) + ; } /** @@ -68,12 +61,14 @@ public function getNormalizedData(bool $ignoreTags = false) return $this->object->getNormalizedData($ignoreTags); } - if (!$this->object instanceof ByteStringObject) { + if (! $this->object instanceof ByteStringObject) { return $this->object->getNormalizedData($ignoreTags); } $integer = BigInteger::fromBase(bin2hex($this->object->getValue()), 16); $minusOne = BigInteger::of(-1); - return $minusOne->minus($integer)->toBase(10); + return $minusOne->minus($integer) + ->toBase(10) + ; } } diff --git a/src/Tag/PositiveBigIntegerTag.php b/src/Tag/PositiveBigIntegerTag.php index 0a35896..757c873 100644 --- a/src/Tag/PositiveBigIntegerTag.php +++ b/src/Tag/PositiveBigIntegerTag.php @@ -2,15 +2,6 @@ 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\Tag; /** diff --git a/src/Tag/TagManager.php b/src/Tag/TagManager.php index 9dcbad9..369a459 100644 --- a/src/Tag/TagManager.php +++ b/src/Tag/TagManager.php @@ -2,15 +2,6 @@ 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\Tag; use function array_key_exists; diff --git a/src/Tag/TagManagerInterface.php b/src/Tag/TagManagerInterface.php index 37e3862..eead85e 100644 --- a/src/Tag/TagManagerInterface.php +++ b/src/Tag/TagManagerInterface.php @@ -2,15 +2,6 @@ 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\Tag; use CBOR\CBORObject; diff --git a/src/Tag/TagObjectManager.php b/src/Tag/TagObjectManager.php index 9e7ba38..1929d93 100644 --- a/src/Tag/TagObjectManager.php +++ b/src/Tag/TagObjectManager.php @@ -2,15 +2,6 @@ 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\Tag; /** diff --git a/src/Tag/TimestampTag.php b/src/Tag/TimestampTag.php index 0ea5f02..75f8766 100644 --- a/src/Tag/TimestampTag.php +++ b/src/Tag/TimestampTag.php @@ -2,15 +2,6 @@ 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\Tag; use CBOR\CBORObject; @@ -24,12 +15,13 @@ use DateTimeImmutable; use DateTimeInterface; use InvalidArgumentException; +use const STR_PAD_RIGHT; final class TimestampTag extends Tag implements Normalizable { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { - if (!$object instanceof UnsignedIntegerObject && !$object instanceof NegativeIntegerObject && !$object instanceof HalfPrecisionFloatObject && !$object instanceof SinglePrecisionFloatObject && !$object instanceof DoublePrecisionFloatObject) { + if (! $object instanceof UnsignedIntegerObject && ! $object instanceof NegativeIntegerObject && ! $object instanceof HalfPrecisionFloatObject && ! $object instanceof SinglePrecisionFloatObject && ! $object instanceof DoublePrecisionFloatObject) { throw new InvalidArgumentException('This tag only accepts integer-based or float-based objects.'); } parent::__construct($additionalInformation, $data, $object); @@ -81,7 +73,7 @@ public function normalize(): DateTimeInterface throw new InvalidArgumentException('Unable to normalize the object'); } - if (false === $formatted) { + if ($formatted === false) { throw new InvalidArgumentException('Invalid data. Cannot be converted into a datetime object'); } diff --git a/src/Tag/UnsignedBigIntegerTag.php b/src/Tag/UnsignedBigIntegerTag.php index a173493..adf3792 100644 --- a/src/Tag/UnsignedBigIntegerTag.php +++ b/src/Tag/UnsignedBigIntegerTag.php @@ -2,15 +2,6 @@ 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\Tag; use CBOR\ByteStringObject; @@ -28,7 +19,7 @@ class UnsignedBigIntegerTag extends Tag implements Normalizable { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { - if (!$object instanceof ByteStringObject && !$object instanceof IndefiniteLengthByteStringObject) { + if (! $object instanceof ByteStringObject && ! $object instanceof IndefiniteLengthByteStringObject) { throw new InvalidArgumentException('This tag only accepts a Byte String object.'); } @@ -69,7 +60,7 @@ public function getNormalizedData(bool $ignoreTags = false) return $this->object->getNormalizedData($ignoreTags); } - if (!$this->object instanceof ByteStringObject) { + if (! $this->object instanceof ByteStringObject) { return $this->object->getNormalizedData($ignoreTags); } diff --git a/src/Tag/UriTag.php b/src/Tag/UriTag.php index d2b946a..b91d4ba 100644 --- a/src/Tag/UriTag.php +++ b/src/Tag/UriTag.php @@ -2,15 +2,6 @@ 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\Tag; use CBOR\CBORObject; @@ -24,7 +15,7 @@ final class UriTag extends Tag implements Normalizable { public function __construct(int $additionalInformation, ?string $data, CBORObject $object) { - if (!$object instanceof TextStringObject && !$object instanceof IndefiniteLengthTextStringObject) { + if (! $object instanceof TextStringObject && ! $object instanceof IndefiniteLengthTextStringObject) { throw new InvalidArgumentException('This tag only accepts a Text String object.'); } diff --git a/src/TagObject.php b/src/TagObject.php index 0ccb5c8..d4ace1f 100644 --- a/src/TagObject.php +++ b/src/TagObject.php @@ -2,15 +2,6 @@ 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; /** diff --git a/src/TextStringObject.php b/src/TextStringObject.php index be000ab..242b66d 100644 --- a/src/TextStringObject.php +++ b/src/TextStringObject.php @@ -2,15 +2,6 @@ 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; final class TextStringObject extends AbstractCBORObject implements Normalizable @@ -36,19 +27,19 @@ public function __construct(string $data) $this->length = $length; } - public static function create(string $data): self - { - return new self($data); - } - public function __toString(): string { $result = parent::__toString(); - if (null !== $this->length) { + if ($this->length !== null) { $result .= $this->length; } - return $result.$this->data; + return $result . $this->data; + } + + public static function create(string $data): self + { + return new self($data); } public function getValue(): string diff --git a/src/TextStringWithChunkObject.php b/src/TextStringWithChunkObject.php index 0cb6b96..6cf47ec 100644 --- a/src/TextStringWithChunkObject.php +++ b/src/TextStringWithChunkObject.php @@ -2,15 +2,6 @@ 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; /** diff --git a/src/UnsignedIntegerObject.php b/src/UnsignedIntegerObject.php index 750e2ab..a2cbcd2 100644 --- a/src/UnsignedIntegerObject.php +++ b/src/UnsignedIntegerObject.php @@ -2,19 +2,11 @@ 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; use Brick\Math\BigInteger; use InvalidArgumentException; +use const STR_PAD_LEFT; final class UnsignedIntegerObject extends AbstractCBORObject implements Normalizable { @@ -34,7 +26,7 @@ public function __construct(int $additionalInformation, ?string $data) public function __toString(): string { $result = parent::__toString(); - if (null !== $this->data) { + if ($this->data !== null) { $result .= $this->data; } @@ -72,7 +64,7 @@ public function getMajorType(): int public function getValue(): string { - if (null === $this->data) { + if ($this->data === null) { return (string) $this->additionalInformation; } @@ -118,7 +110,9 @@ private static function createBigInteger(BigInteger $integer): self $data = self::hex2bin(str_pad($integer->toBase(16), 8, '0', STR_PAD_LEFT)); break; default: - throw new InvalidArgumentException('Out of range. Please use PositiveBigIntegerTag tag with ByteStringObject object instead.'); + throw new InvalidArgumentException( + 'Out of range. Please use PositiveBigIntegerTag tag with ByteStringObject object instead.' + ); } return new self($ai, $data); @@ -127,7 +121,7 @@ private static function createBigInteger(BigInteger $integer): self private static function hex2bin(string $data): string { $result = hex2bin($data); - if (false === $result) { + if ($result === false) { throw new InvalidArgumentException('Unable to convert the data'); } diff --git a/src/Utils.php b/src/Utils.php index 9b4d029..b75343f 100644 --- a/src/Utils.php +++ b/src/Utils.php @@ -2,15 +2,6 @@ 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; use Brick\Math\BigInteger; @@ -50,7 +41,7 @@ public static function hexToString(string $value): string public static function decode(string $data): string { $decoded = base64_decode(strtr($data, '-_', '+/'), true); - if (false === $decoded) { + if ($decoded === false) { throw new InvalidArgumentException('Invalid data provided'); } @@ -62,7 +53,7 @@ public static function decode(string $data): string */ public static function assertString($data, ?string $message = null): void { - if (!is_string($data)) { + if (! is_string($data)) { throw new InvalidArgumentException($message ?? ''); } } diff --git a/tests/ByteStringObjectTest.php b/tests/ByteStringObjectTest.php index 0b66902..0d5e574 100644 --- a/tests/ByteStringObjectTest.php +++ b/tests/ByteStringObjectTest.php @@ -2,15 +2,6 @@ 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\Test; use CBOR\ByteStringObject; @@ -18,38 +9,42 @@ use CBOR\StringStream; /** - * @covers \CBOR\ByteStringObject - * * @internal */ -final class ByteStringObjectTest extends BaseTestCase +final class ByteStringObjectTest extends CBORTestCase { /** * @test * @dataProvider getData */ - public function aByteStringObjectCanBeCreated(string $string, int $expectedAdditionalInformation, int $expectedLength, string $expectedEncodedObject): void - { + public function aByteStringObjectCanBeCreated( + string $string, + int $expectedAdditionalInformation, + int $expectedLength, + string $expectedEncodedObject + ): void { $object = ByteStringObject::create($string); - static::assertEquals(CBORObject::MAJOR_TYPE_BYTE_STRING, $object->getMajorType()); - static::assertEquals($expectedAdditionalInformation, $object->getAdditionalInformation()); - static::assertEquals($string, $object->getValue()); - static::assertEquals($expectedLength, $object->getLength()); - static::assertEquals($string, $object->getNormalizedData()); + static::assertSame(CBORObject::MAJOR_TYPE_BYTE_STRING, $object->getMajorType()); + static::assertSame($expectedAdditionalInformation, $object->getAdditionalInformation()); + static::assertSame($string, $object->getValue()); + static::assertSame($expectedLength, $object->getLength()); + static::assertSame($string, $object->getNormalizedData()); $binary = (string) $object; - static::assertEquals(hex2bin($expectedEncodedObject), $binary); + static::assertSame(hex2bin($expectedEncodedObject), $binary); $stream = StringStream::create($binary); - $decoded = $this->getDecoder()->decode($stream); + $decoded = $this->getDecoder() + ->decode($stream) + ; static::assertInstanceOf(ByteStringObject::class, $decoded); - static::assertEquals(CBORObject::MAJOR_TYPE_BYTE_STRING, $decoded->getMajorType()); - static::assertEquals($expectedAdditionalInformation, $decoded->getAdditionalInformation()); - static::assertEquals($string, $decoded->getValue()); - static::assertEquals($expectedLength, $decoded->getLength()); - static::assertEquals($string, $decoded->getNormalizedData()); + static::assertSame(CBORObject::MAJOR_TYPE_BYTE_STRING, $decoded->getMajorType()); + static::assertSame($expectedAdditionalInformation, $decoded->getAdditionalInformation()); + static::assertSame($string, $decoded->getValue()); + static::assertSame($expectedLength, $decoded->getLength()); + static::assertSame($string, $decoded->getNormalizedData()); } public function getData(): array diff --git a/tests/BaseTestCase.php b/tests/CBORTestCase.php similarity index 55% rename from tests/BaseTestCase.php rename to tests/CBORTestCase.php index e090f6e..aaa8e7b 100644 --- a/tests/BaseTestCase.php +++ b/tests/CBORTestCase.php @@ -2,15 +2,6 @@ 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\Test; use CBOR\Decoder; @@ -19,7 +10,7 @@ /** * @internal */ -class BaseTestCase extends TestCase +abstract class CBORTestCase extends TestCase { /** * @var Decoder|null @@ -28,7 +19,7 @@ class BaseTestCase extends TestCase protected function getDecoder(): Decoder { - if (null === $this->decoder) { + if ($this->decoder === null) { $this->decoder = Decoder::create(); } diff --git a/tests/DoublePrecisionFloat.php b/tests/DoublePrecisionFloat.php new file mode 100644 index 0000000..009d9a3 --- /dev/null +++ b/tests/DoublePrecisionFloat.php @@ -0,0 +1,22 @@ +getNormalizedData()); + } +} diff --git a/tests/DoublePrecisionFloatTest.php b/tests/DoublePrecisionFloatTest.php deleted file mode 100644 index cdbe154..0000000 --- a/tests/DoublePrecisionFloatTest.php +++ /dev/null @@ -1,31 +0,0 @@ -getNormalizedData()); - } -} diff --git a/tests/FloatTest.php b/tests/FloatTest.php index 75b5ce9..acac9ba 100644 --- a/tests/FloatTest.php +++ b/tests/FloatTest.php @@ -2,15 +2,6 @@ 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\Test; use CBOR\StringStream; @@ -18,7 +9,7 @@ /** * @internal */ -final class FloatTest extends BaseTestCase +final class FloatTest extends CBORTestCase { /** * @test @@ -27,63 +18,23 @@ final class FloatTest extends BaseTestCase public function aFloatCanBeParsed(string $data): void { $stream = StringStream::create(hex2bin($data)); - $object = $this->getDecoder()->decode($stream); + $object = $this->getDecoder() + ->decode($stream) + ; $object->getNormalizedData(); - static::assertEquals($data, bin2hex((string) $object)); + static::assertSame($data, bin2hex((string) $object)); } public function getDataSet(): array { return [ - [ - 'f90000', - ], [ - 'f98000', - ], [ - 'f93c00', - ], [ - 'fb3ff199999999999a', - ], [ - 'f93e00', - ], [ + ['f90000'], ['f98000'], ['f93c00'], ['fb3ff199999999999a'], ['f93e00'], [ 'f97bff', - ], [ - 'fa47c35000', - ], [ - 'fa7f7fffff', - ], [ - 'fb7e37e43c8800759c', - ], [ - 'f90001', - ], [ - 'f90400', - ], [ - 'f9c400', - ], [ + ], ['fa47c35000'], ['fa7f7fffff'], ['fb7e37e43c8800759c'], ['f90001'], ['f90400'], ['f9c400'], [ 'fbc010666666666666', - ], [ - 'f97c00', - ], [ - 'f97e00', - ], [ - 'f9fc00', - ], [ - 'fa7f800000', - ], [ - 'fa7fc00000', - ], [ - 'faff800000', - ], [ + ], ['f97c00'], ['f97e00'], ['f9fc00'], ['fa7f800000'], ['fa7fc00000'], ['faff800000'], [ 'fb7ff0000000000000', - ], [ - 'fb7ff8000000000000', - ], [ - 'fbfff0000000000000', - ], [ - 'c5822003', - ], [ - 'c48221196ab3', - ], + ], ['fb7ff8000000000000'], ['fbfff0000000000000'], ['c5822003'], ['c48221196ab3'], ]; } } diff --git a/tests/GlobalTest.php b/tests/GlobalTest.php index 373415b..53e1d50 100644 --- a/tests/GlobalTest.php +++ b/tests/GlobalTest.php @@ -2,15 +2,6 @@ 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\Test; use CBOR\StringStream; @@ -18,7 +9,7 @@ /** * @internal */ -final class GlobalTest extends BaseTestCase +final class GlobalTest extends CBORTestCase { /** * @test @@ -27,9 +18,11 @@ final class GlobalTest extends BaseTestCase public function aSignedIntegerCanBeParsed(string $data): void { $stream = StringStream::create(hex2bin($data)); - $object = $this->getDecoder()->decode($stream); + $object = $this->getDecoder() + ->decode($stream) + ; $object->getNormalizedData(); - static::assertEquals($data, bin2hex((string) $object)); + static::assertSame($data, bin2hex((string) $object)); } public function getDataSet(): array @@ -37,75 +30,19 @@ public function getDataSet(): array return [ [ 'c074323031332d30332d32315432303a30343a30305a', - ], [ - 'c11a514b67b0', - ], [ - 'c1fb41d452d9ec200000', - ], [ - 'd74401020304', - ], [ - 'd818456449455446', - ], [ + ], ['c11a514b67b0'], ['c1fb41d452d9ec200000'], ['d74401020304'], ['d818456449455446'], [ 'd82076687474703a2f2f7777772e6578616d706c652e636f6d', - ], [ - '40', - ], [ - '4401020304', - ], [ - '60', - ], [ - '6161', - ], [ - '6449455446', - ], [ + ], ['40'], ['4401020304'], ['60'], ['6161'], ['6449455446'], [ '62225c', - ], [ - '62c3bc', - ], [ - '63e6b0b4', - ], [ - '64f0908591', - ], [ - '80', - ], [ - '83010203', - ], [ - '8301820203820405', - ], [ + ], ['62c3bc'], ['63e6b0b4'], ['64f0908591'], ['80'], ['83010203'], ['8301820203820405'], [ '98190102030405060708090a0b0c0d0e0f101112131415161718181819', - ], [ - 'a0', - ], [ - 'a201020304', - ], [ - 'a26161016162820203', - ], [ - '826161a161626163', - ], [ + ], ['a0'], ['a201020304'], ['a26161016162820203'], ['826161a161626163'], [ 'a56161614161626142616361436164614461656145', - ], [ - '5f42010243030405ff', - ], [ - '7f657374726561646d696e67ff', - ], [ - '9fff', - ], [ + ], ['5f42010243030405ff'], ['7f657374726561646d696e67ff'], ['9fff'], [ '9f018202039f0405ffff', - ], [ - '9f01820203820405ff', - ], [ - '83018202039f0405ff', - ], [ - '83019f0203ff820405', - ], [ + ], ['9f01820203820405ff'], ['83018202039f0405ff'], ['83019f0203ff820405'], [ '9f0102030405060708090a0b0c0d0e0f101112131415161718181819ff', - ], [ - 'bf61610161629f0203ffff', - ], [ - '826161bf61626163ff', - ], [ - 'bf6346756ef563416d7421ff', - ], [ + ], ['bf61610161629f0203ffff'], ['826161bf61626163ff'], ['bf6346756ef563416d7421ff'], [ 'a401030339010020590100c5da6f4d9357bde202f5c558cd0a3156d254f2e0ad9ab57931f9826b747de1ac4f29d6070874dce57910e19844499d8e42470339b170d022b501ab88e9c2f4ed302e4719c70debe8842403ed9bdfc22730a61a1b70f616c5f1b700cacf7846137dc4b2d469a8e15aab4fad8657084022d28f44d9075323126b7007c981939fdf724caf4fbe475040431a4ea064430bcb2cfad7d05bdb9f64b5b0e0952ecf8679273d6c6dfa81601f14503316a13d0782c31a3e6bdded3d7bc46bc1fa9bef0dff83b7deaf146b582c4644821a3c62edbaa6be422bf04e43edaf5fd3783086153d7361a203061a6298ab26e1337ca1c9ed06741a5905477988e720304eae189d7f2143010001', ], ]; diff --git a/tests/IndefiniteLengthByteStringObjectTest.php b/tests/IndefiniteLengthByteStringObjectTest.php index 7d8557e..3121a89 100644 --- a/tests/IndefiniteLengthByteStringObjectTest.php +++ b/tests/IndefiniteLengthByteStringObjectTest.php @@ -2,15 +2,6 @@ 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\Test; use CBOR\CBORObject; @@ -18,48 +9,56 @@ use CBOR\StringStream; /** - * @covers \CBOR\IndefiniteLengthByteStringObject - * * @internal */ -final class IndefiniteLengthByteStringObjectTest extends BaseTestCase +final class IndefiniteLengthByteStringObjectTest extends CBORTestCase { /** * @test * @dataProvider getData */ - public function aIndefiniteLengthByteStringObjectCanBeCreated(array $chunks, int $expectedLength, string $expectedValue, string $expectedEncodedObject): void - { + public function aIndefiniteLengthByteStringObjectCanBeCreated( + array $chunks, + int $expectedLength, + string $expectedValue, + string $expectedEncodedObject + ): void { $object = IndefiniteLengthByteStringObject::create(); foreach ($chunks as $chunk) { $object->append($chunk); } - static::assertEquals(CBORObject::MAJOR_TYPE_BYTE_STRING, $object->getMajorType()); - static::assertEquals(CBORObject::LENGTH_INDEFINITE, $object->getAdditionalInformation()); - static::assertEquals($expectedValue, $object->getValue()); - static::assertEquals($expectedLength, $object->getLength()); - static::assertEquals($expectedValue, $object->getNormalizedData()); + static::assertSame(CBORObject::MAJOR_TYPE_BYTE_STRING, $object->getMajorType()); + static::assertSame(CBORObject::LENGTH_INDEFINITE, $object->getAdditionalInformation()); + static::assertSame($expectedValue, $object->getValue()); + static::assertSame($expectedLength, $object->getLength()); + static::assertSame($expectedValue, $object->getNormalizedData()); $binary = (string) $object; - static::assertEquals(hex2bin($expectedEncodedObject), $binary); + static::assertSame(hex2bin($expectedEncodedObject), $binary); $stream = StringStream::create($binary); - $decoded = $this->getDecoder()->decode($stream); + $decoded = $this->getDecoder() + ->decode($stream) + ; static::assertInstanceOf(IndefiniteLengthByteStringObject::class, $decoded); - static::assertEquals(CBORObject::MAJOR_TYPE_BYTE_STRING, $decoded->getMajorType()); - static::assertEquals(CBORObject::LENGTH_INDEFINITE, $decoded->getAdditionalInformation()); - static::assertEquals($expectedValue, $decoded->getValue()); - static::assertEquals($expectedLength, $decoded->getLength()); - static::assertEquals($expectedValue, $decoded->getNormalizedData()); + static::assertSame(CBORObject::MAJOR_TYPE_BYTE_STRING, $decoded->getMajorType()); + static::assertSame(CBORObject::LENGTH_INDEFINITE, $decoded->getAdditionalInformation()); + static::assertSame($expectedValue, $decoded->getValue()); + static::assertSame($expectedLength, $decoded->getLength()); + static::assertSame($expectedValue, $decoded->getNormalizedData()); } public function getData(): array { return [ [['He', 'll', 'o'], 5, 'Hello', '5f424865426c6c416fff'], - [['(', '。', '◕', '‿', '◕', '。', ')'], 17, '(。◕‿◕。)', '5f412843efbda143e2979543e280bf43e2979543efbda14129ff'], + [['(', '。', '◕', '‿', '◕', '。', ')'], + 17, + '(。◕‿◕。)', + '5f412843efbda143e2979543e280bf43e2979543efbda14129ff', + ], ]; } } diff --git a/tests/IndefiniteLengthTextStringObjectTest.php b/tests/IndefiniteLengthTextStringObjectTest.php index ab8a45b..30e37d7 100644 --- a/tests/IndefiniteLengthTextStringObjectTest.php +++ b/tests/IndefiniteLengthTextStringObjectTest.php @@ -2,15 +2,6 @@ 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\Test; use CBOR\CBORObject; @@ -19,48 +10,56 @@ use CBOR\TextStringObject; /** - * @covers \CBOR\IndefiniteLengthTextStringObject - * * @internal */ -final class IndefiniteLengthTextStringObjectTest extends BaseTestCase +final class IndefiniteLengthTextStringObjectTest extends CBORTestCase { /** * @test * @dataProvider getData */ - public function aIndefiniteLengthTextStringObjectCanBeCreated(array $chunks, int $expectedLength, string $expectedValue, string $expectedEncodedObject): void - { + public function aIndefiniteLengthTextStringObjectCanBeCreated( + array $chunks, + int $expectedLength, + string $expectedValue, + string $expectedEncodedObject + ): void { $object = IndefiniteLengthTextStringObject::create(); foreach ($chunks as $chunk) { $object->add(TextStringObject::create($chunk)); } - static::assertEquals(CBORObject::MAJOR_TYPE_TEXT_STRING, $object->getMajorType()); - static::assertEquals(CBORObject::LENGTH_INDEFINITE, $object->getAdditionalInformation()); - static::assertEquals($expectedValue, $object->getValue()); - static::assertEquals($expectedLength, $object->getLength()); - static::assertEquals($expectedValue, $object->getNormalizedData()); + static::assertSame(CBORObject::MAJOR_TYPE_TEXT_STRING, $object->getMajorType()); + static::assertSame(CBORObject::LENGTH_INDEFINITE, $object->getAdditionalInformation()); + static::assertSame($expectedValue, $object->getValue()); + static::assertSame($expectedLength, $object->getLength()); + static::assertSame($expectedValue, $object->getNormalizedData()); $binary = (string) $object; - static::assertEquals(hex2bin($expectedEncodedObject), $binary); + static::assertSame(hex2bin($expectedEncodedObject), $binary); $stream = StringStream::create($binary); - $decoded = $this->getDecoder()->decode($stream); + $decoded = $this->getDecoder() + ->decode($stream) + ; static::assertInstanceOf(IndefiniteLengthTextStringObject::class, $decoded); - static::assertEquals(CBORObject::MAJOR_TYPE_TEXT_STRING, $decoded->getMajorType()); - static::assertEquals(CBORObject::LENGTH_INDEFINITE, $decoded->getAdditionalInformation()); - static::assertEquals($expectedValue, $decoded->getValue()); - static::assertEquals($expectedLength, $decoded->getLength()); - static::assertEquals($expectedValue, $decoded->getNormalizedData()); + static::assertSame(CBORObject::MAJOR_TYPE_TEXT_STRING, $decoded->getMajorType()); + static::assertSame(CBORObject::LENGTH_INDEFINITE, $decoded->getAdditionalInformation()); + static::assertSame($expectedValue, $decoded->getValue()); + static::assertSame($expectedLength, $decoded->getLength()); + static::assertSame($expectedValue, $decoded->getNormalizedData()); } public function getData(): array { return [ [['He', 'll', 'o'], 5, 'Hello', '7f624865626c6c616fff'], - [['(', '。', '◕', '‿', '◕', '。', ')'], 7, '(。◕‿◕。)', '7f612863efbda163e2979563e280bf63e2979563efbda16129ff'], + [['(', '。', '◕', '‿', '◕', '。', ')'], + 7, + '(。◕‿◕。)', + '7f612863efbda163e2979563e280bf63e2979563efbda16129ff', + ], ]; } } diff --git a/tests/InvalidTypeTest.php b/tests/InvalidTypeTest.php index 330ef92..d2269a9 100644 --- a/tests/InvalidTypeTest.php +++ b/tests/InvalidTypeTest.php @@ -2,15 +2,6 @@ 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\Test; use Brick\Math\Exception\IntegerOverflowException; @@ -21,7 +12,7 @@ /** * @internal */ -final class InvalidTypeTest extends BaseTestCase +final class InvalidTypeTest extends CBORTestCase { /** * @test @@ -33,7 +24,9 @@ public function invalidData(string $item, string $class, string $expectedError): $this->expectExceptionMessage($expectedError); $stream = StringStream::create(hex2bin($item)); - $this->getDecoder()->decode($stream); + $this->getDecoder() + ->decode($stream) + ; } /** @@ -42,9 +35,21 @@ public function invalidData(string $item, string $class, string $expectedError): public function getInvalidDataItems(): array { return [ - [base_convert('00000011100', 2, 16), InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011100" (28).'], - [base_convert('00000011101', 2, 16), InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011101" (29).'], - [base_convert('00000011110', 2, 16), InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011110" (30).'], + [ + base_convert('00000011100', 2, 16), + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011100" (28).', + ], + [ + base_convert('00000011101', 2, 16), + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011101" (29).', + ], + [ + base_convert('00000011110', 2, 16), + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011110" (30).', + ], ['18', InvalidArgumentException::class, 'Out of range. Expected: 1, read: 0.'], ['19', InvalidArgumentException::class, 'Out of range. Expected: 2, read: 0.'], ['1a', InvalidArgumentException::class, 'Out of range. Expected: 4, read: 0.'], @@ -66,9 +71,17 @@ public function getInvalidDataItems(): array ['41', InvalidArgumentException::class, 'Out of range. Expected: 1, read: 0.'], ['61', InvalidArgumentException::class, 'Out of range. Expected: 1, read: 0.'], ['5affffffff00', InvalidArgumentException::class, 'Out of range. Expected: 4294967295, read: 0.'], - ['5bffffffffffffffff010203', IntegerOverflowException::class, '18446744073709551615 is out of range -9223372036854775808 to 9223372036854775807 and cannot be represented as an integer.'], + [ + '5bffffffffffffffff010203', + IntegerOverflowException::class, + '18446744073709551615 is out of range -9223372036854775808 to 9223372036854775807 and cannot be represented as an integer.', + ], ['7affffffff00', InvalidArgumentException::class, 'Out of range. Expected: 4294967295, read: 0.'], - ['7b7fffffffffffffff010203', InvalidArgumentException::class, 'Out of range. Expected: 9223372036854775807, read: 0.'], + [ + '7b7fffffffffffffff010203', + InvalidArgumentException::class, + 'Out of range. Expected: 9223372036854775807, read: 0.', + ], ['81', InvalidArgumentException::class, 'Out of range. Expected: 1, read: 0.'], ['818181818181818181', InvalidArgumentException::class, 'Out of range. Expected: 1, read: 0.'], ['8200', InvalidArgumentException::class, 'Out of range. Expected: 1, read: 0.'], @@ -87,45 +100,173 @@ public function getInvalidDataItems(): array ['9f8000', InvalidArgumentException::class, 'Out of range. Expected: 1, read: 0.'], ['9f9f9f9f9fffffffff', InvalidArgumentException::class, 'Out of range. Expected: 1, read: 0.'], ['9f819f819f9fffffff', InvalidArgumentException::class, 'Out of range. Expected: 1, read: 0.'], - ['1c', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011100" (28).'], - ['1d', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011101" (29).'], - ['1e', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011110" (30).'], - ['3c', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011100" (28).'], - ['3d', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011101" (29).'], - ['3e', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011110" (30).'], - ['5c', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011100" (28).'], - ['5d', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011101" (29).'], - ['5e', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011110" (30).'], - ['7c', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011100" (28).'], - ['7d', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011101" (29).'], - ['7e', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011110" (30).'], - ['9c', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011100" (28).'], - ['9d', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011101" (29).'], - ['9e', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011110" (30).'], - ['bc', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011100" (28).'], - ['bd', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011101" (29).'], - ['be', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011110" (30).'], - ['dc', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011100" (28).'], - ['dd', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011101" (29).'], - ['de', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011110" (30).'], - ['fc', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011100" (28).'], - ['fd', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011101" (29).'], - ['fe', InvalidArgumentException::class, 'Cannot parse the data. Found invalid Additional Information "00011110" (30).'], + [ + '1c', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011100" (28).', + ], + [ + '1d', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011101" (29).', + ], + [ + '1e', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011110" (30).', + ], + [ + '3c', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011100" (28).', + ], + [ + '3d', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011101" (29).', + ], + [ + '3e', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011110" (30).', + ], + [ + '5c', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011100" (28).', + ], + [ + '5d', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011101" (29).', + ], + [ + '5e', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011110" (30).', + ], + [ + '7c', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011100" (28).', + ], + [ + '7d', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011101" (29).', + ], + [ + '7e', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011110" (30).', + ], + [ + '9c', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011100" (28).', + ], + [ + '9d', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011101" (29).', + ], + [ + '9e', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011110" (30).', + ], + [ + 'bc', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011100" (28).', + ], + [ + 'bd', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011101" (29).', + ], + [ + 'be', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011110" (30).', + ], + [ + 'dc', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011100" (28).', + ], + [ + 'dd', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011101" (29).', + ], + [ + 'de', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011110" (30).', + ], + [ + 'fc', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011100" (28).', + ], + [ + 'fd', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011101" (29).', + ], + [ + 'fe', + InvalidArgumentException::class, + 'Cannot parse the data. Found invalid Additional Information "00011110" (30).', + ], ['f800', InvalidArgumentException::class, 'Invalid simple value. Content data must be between 32 and 255.'], ['f801', InvalidArgumentException::class, 'Invalid simple value. Content data must be between 32 and 255.'], ['f818', InvalidArgumentException::class, 'Invalid simple value. Content data must be between 32 and 255.'], ['f81f', InvalidArgumentException::class, 'Invalid simple value. Content data must be between 32 and 255.'], - ['5f00ff', RuntimeException::class, 'Unable to parse the data. Infinite Byte String object can only get Byte String objects.'], - ['5f21ff', RuntimeException::class, 'Unable to parse the data. Infinite Byte String object can only get Byte String objects.'], - ['5f6100ff', RuntimeException::class, 'Unable to parse the data. Infinite Byte String object can only get Byte String objects.'], - ['5f80ff', RuntimeException::class, 'Unable to parse the data. Infinite Byte String object can only get Byte String objects.'], - ['5fa0ff', RuntimeException::class, 'Unable to parse the data. Infinite Byte String object can only get Byte String objects.'], + [ + '5f00ff', + RuntimeException::class, + 'Unable to parse the data. Infinite Byte String object can only get Byte String objects.', + ], + [ + '5f21ff', + RuntimeException::class, + 'Unable to parse the data. Infinite Byte String object can only get Byte String objects.', + ], + [ + '5f6100ff', + RuntimeException::class, + 'Unable to parse the data. Infinite Byte String object can only get Byte String objects.', + ], + [ + '5f80ff', + RuntimeException::class, + 'Unable to parse the data. Infinite Byte String object can only get Byte String objects.', + ], + [ + '5fa0ff', + RuntimeException::class, + 'Unable to parse the data. Infinite Byte String object can only get Byte String objects.', + ], ['5fc000ff', InvalidArgumentException::class, 'This tag only accepts a Byte String object.'], - ['5fe0ff', RuntimeException::class, 'Unable to parse the data. Infinite Byte String object can only get Byte String objects.'], - ['7f4100ff', RuntimeException::class, 'Unable to parse the data. Infinite Text String object can only get Text String objects.'], + [ + '5fe0ff', + RuntimeException::class, + 'Unable to parse the data. Infinite Byte String object can only get Byte String objects.', + ], + [ + '7f4100ff', + RuntimeException::class, + 'Unable to parse the data. Infinite Text String object can only get Text String objects.', + ], ['5f5f4100', InvalidArgumentException::class, 'Out of range. Expected: 1, read: 0.'], ['ffff', InvalidArgumentException::class, 'Cannot parse the data. No enclosing indefinite.'], - ['7f7f6100ffff', RuntimeException::class, 'Unable to parse the data. Infinite Text String object can only get Text String objects.'], + [ + '7f7f6100ffff', + RuntimeException::class, + 'Unable to parse the data. Infinite Text String object can only get Text String objects.', + ], ['ff', InvalidArgumentException::class, 'Cannot parse the data. No enclosing indefinite.'], ['81ff', InvalidArgumentException::class, 'Cannot parse the data. No enclosing indefinite.'], ['8200ff', InvalidArgumentException::class, 'Cannot parse the data. No enclosing indefinite.'], @@ -134,12 +275,28 @@ public function getInvalidDataItems(): array ['a100ff', InvalidArgumentException::class, 'Cannot parse the data. No enclosing indefinite.'], ['a20000ff', InvalidArgumentException::class, 'Cannot parse the data. No enclosing indefinite.'], ['9f81ff', InvalidArgumentException::class, 'Cannot parse the data. No enclosing indefinite.'], - ['9f829f819f9fffffffff', InvalidArgumentException::class, 'Cannot parse the data. No enclosing indefinite.'], + [ + '9f829f819f9fffffffff', + InvalidArgumentException::class, + 'Cannot parse the data. No enclosing indefinite.', + ], ['bf00ff', InvalidArgumentException::class, 'Cannot parse the data. No enclosing indefinite.'], ['bf000000ff', InvalidArgumentException::class, 'Cannot parse the data. No enclosing indefinite.'], - ['1f', InvalidArgumentException::class, 'Cannot parse the data. Found infinite length for Major Type "00000" (0).'], - ['3f', InvalidArgumentException::class, 'Cannot parse the data. Found infinite length for Major Type "00001" (1).'], - ['df', InvalidArgumentException::class, 'Cannot parse the data. Found infinite length for Major Type "00110" (6).'], + [ + '1f', + InvalidArgumentException::class, + 'Cannot parse the data. Found infinite length for Major Type "00000" (0).', + ], + [ + '3f', + InvalidArgumentException::class, + 'Cannot parse the data. Found infinite length for Major Type "00001" (1).', + ], + [ + 'df', + InvalidArgumentException::class, + 'Cannot parse the data. Found infinite length for Major Type "00110" (6).', + ], ]; } } diff --git a/tests/ListObjectTest.php b/tests/ListObjectTest.php index 4512503..a30d637 100644 --- a/tests/ListObjectTest.php +++ b/tests/ListObjectTest.php @@ -2,15 +2,6 @@ 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\Test; use CBOR\IndefiniteLengthListObject; @@ -21,7 +12,7 @@ /** * @internal */ -final class ListObjectTest extends BaseTestCase +final class ListObjectTest extends CBORTestCase { /** * @test @@ -47,11 +38,15 @@ public function aListActsAsAnArray(): void static::assertCount(3, $object1); static::assertCount(3, $object2); - static::assertEquals(['Hello', 'World', 3], $object2->normalize()); - static::assertEquals($object1->normalize(), $object2->normalize()); - static::assertEquals((string) $object1, (string) $object2); + static::assertSame(['Hello', 'World', '3'], $object2->normalize()); + static::assertSame($object1->normalize(), $object2->normalize()); + static::assertSame((string) $object1, (string) $object2); static::assertTrue(isset($object2[0])); - static::assertEquals(TextStringObject::create('World'), $object2[1]); + static::assertTrue(isset($object2[1])); + static::assertTrue(isset($object2[2])); + static::assertSame($object2[0]->normalize(), 'Hello'); + static::assertSame($object2[1]->normalize(), 'World'); + static::assertSame($object2[2]->normalize(), '3'); } /** @@ -78,10 +73,14 @@ public function anIndefiniteLengthListActsAsAnArray(): void static::assertCount(3, $object1); static::assertCount(3, $object2); - static::assertEquals(['Hello', 'World', 3], $object2->normalize()); - static::assertEquals($object1->normalize(), $object2->normalize()); - static::assertEquals((string) $object1, (string) $object2); + static::assertSame(['Hello', 'World', '3'], $object2->normalize()); + static::assertSame($object1->normalize(), $object2->normalize()); + static::assertSame((string) $object1, (string) $object2); static::assertTrue(isset($object2[0])); - static::assertEquals(TextStringObject::create('World'), $object2[1]); + static::assertTrue(isset($object2[1])); + static::assertTrue(isset($object2[2])); + static::assertSame($object2[0]->normalize(), 'Hello'); + static::assertSame($object2[1]->normalize(), 'World'); + static::assertSame($object2[2]->normalize(), '3'); } } diff --git a/tests/MapObjectTest.php b/tests/MapObjectTest.php index 994b416..203468e 100644 --- a/tests/MapObjectTest.php +++ b/tests/MapObjectTest.php @@ -2,15 +2,6 @@ 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\Test; use CBOR\ByteStringObject; @@ -24,7 +15,7 @@ /** * @internal */ -final class MapObjectTest extends BaseTestCase +final class MapObjectTest extends CBORTestCase { /** * @test @@ -50,22 +41,22 @@ public function aMapActsAsAnArray(): void static::assertCount(4, $object1); static::assertCount(4, $object2); - static::assertEquals([ + static::assertSame([ 10 => 'Hello', -150 => 'World', - 'AZERTY' => 1, - 'Test' => 3, + 'AZERTY' => '1', + 'Test' => '3', ], $object2->normalize()); - static::assertEquals($object1->normalize(), $object2->normalize()); - static::assertEquals((string) $object1, (string) $object2); + static::assertSame($object1->normalize(), $object2->normalize()); + static::assertSame((string) $object1, (string) $object2); static::assertTrue(isset($object2[10])); static::assertTrue(isset($object2[-150])); static::assertTrue(isset($object2['AZERTY'])); static::assertTrue(isset($object2['Test'])); - static::assertEquals( - MapItem::create(NegativeIntegerObject::create(-150), TextStringObject::create('World')), - $object2[-150] - ); + static::assertSame($object2[10]->normalize(), 'Hello'); + static::assertSame($object2[-150]->normalize(), 'World'); + static::assertSame($object2['AZERTY']->normalize(), '1'); + static::assertSame($object2['Test']->normalize(), '3'); } /** @@ -92,21 +83,21 @@ public function anIndefiniteLengthMapActsAsAnArray(): void static::assertCount(4, $object1); static::assertCount(4, $object2); - static::assertEquals([ + static::assertSame([ 10 => 'Hello', -150 => 'World', - 'AZERTY' => 1, - 'Test' => 3, + 'AZERTY' => '1', + 'Test' => '3', ], $object2->normalize()); - static::assertEquals($object1->normalize(), $object2->normalize()); - static::assertEquals((string) $object1, (string) $object2); + static::assertSame($object1->normalize(), $object2->normalize()); + static::assertSame((string) $object1, (string) $object2); static::assertTrue(isset($object2[10])); static::assertTrue(isset($object2[-150])); static::assertTrue(isset($object2['AZERTY'])); static::assertTrue(isset($object2['Test'])); - static::assertEquals( - MapItem::create(NegativeIntegerObject::create(-150), TextStringObject::create('World')), - $object2[-150] - ); + static::assertSame($object2[10]->normalize(), 'Hello'); + static::assertSame($object2[-150]->normalize(), 'World'); + static::assertSame($object2['AZERTY']->normalize(), '1'); + static::assertSame($object2['Test']->normalize(), '3'); } } diff --git a/tests/OtherObject/AllTest.php b/tests/OtherObject/All.php similarity index 51% rename from tests/OtherObject/AllTest.php rename to tests/OtherObject/All.php index c6f3769..238fa35 100644 --- a/tests/OtherObject/AllTest.php +++ b/tests/OtherObject/All.php @@ -2,15 +2,6 @@ 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\Test\OtherObject; use CBOR\CBORObject; @@ -25,14 +16,17 @@ use CBOR\OtherObject\TrueObject; use CBOR\OtherObject\UndefinedObject; use CBOR\StringStream; -use CBOR\Test\BaseTestCase; +use CBOR\Test\CBORTestCase; use function chr; +use const INF; use InvalidArgumentException; +use const M_PI; +use const STR_PAD_LEFT; /** * @internal */ -final class AllTest extends BaseTestCase +final class All extends CBORTestCase { /** * @test @@ -41,15 +35,17 @@ public function createValidFalseObject(): void { $object = FalseObject::create(); - static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); - static::assertEquals(CBORObject::OBJECT_FALSE, $object->getAdditionalInformation()); + static::assertSame(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); + static::assertSame(CBORObject::OBJECT_FALSE, $object->getAdditionalInformation()); static::assertNull($object->getContent()); $stream = StringStream::create($object->__toString()); - $decoded = $this->getDecoder()->decode($stream); + $decoded = $this->getDecoder() + ->decode($stream) + ; - static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $decoded->getMajorType()); - static::assertEquals(CBORObject::OBJECT_FALSE, $decoded->getAdditionalInformation()); + static::assertSame(CBORObject::MAJOR_TYPE_OTHER_TYPE, $decoded->getMajorType()); + static::assertSame(CBORObject::OBJECT_FALSE, $decoded->getAdditionalInformation()); static::assertNull($decoded->getContent()); static::assertFalse($decoded->normalize()); } @@ -61,14 +57,16 @@ public function createValidTrueObject(): void { $object = TrueObject::create(); - static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); - static::assertEquals(CBORObject::OBJECT_TRUE, $object->getAdditionalInformation()); + static::assertSame(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); + static::assertSame(CBORObject::OBJECT_TRUE, $object->getAdditionalInformation()); $stream = StringStream::create($object->__toString()); - $decoded = $this->getDecoder()->decode($stream); + $decoded = $this->getDecoder() + ->decode($stream) + ; - static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $decoded->getMajorType()); - static::assertEquals(CBORObject::OBJECT_TRUE, $decoded->getAdditionalInformation()); + static::assertSame(CBORObject::MAJOR_TYPE_OTHER_TYPE, $decoded->getMajorType()); + static::assertSame(CBORObject::OBJECT_TRUE, $decoded->getAdditionalInformation()); static::assertNull($decoded->getContent()); static::assertTrue($decoded->normalize()); } @@ -80,14 +78,16 @@ public function createValidNullObject(): void { $object = NullObject::create(); - static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); - static::assertEquals(CBORObject::OBJECT_NULL, $object->getAdditionalInformation()); + static::assertSame(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); + static::assertSame(CBORObject::OBJECT_NULL, $object->getAdditionalInformation()); $stream = StringStream::create($object->__toString()); - $decoded = $this->getDecoder()->decode($stream); + $decoded = $this->getDecoder() + ->decode($stream) + ; - static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $decoded->getMajorType()); - static::assertEquals(CBORObject::OBJECT_NULL, $decoded->getAdditionalInformation()); + static::assertSame(CBORObject::MAJOR_TYPE_OTHER_TYPE, $decoded->getMajorType()); + static::assertSame(CBORObject::OBJECT_NULL, $decoded->getAdditionalInformation()); static::assertNull($decoded->getContent()); static::assertNull($decoded->normalize()); } @@ -99,14 +99,16 @@ public function createValidUndefinedObject(): void { $object = UndefinedObject::create(); - static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); - static::assertEquals(CBORObject::OBJECT_UNDEFINED, $object->getAdditionalInformation()); + static::assertSame(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); + static::assertSame(CBORObject::OBJECT_UNDEFINED, $object->getAdditionalInformation()); $stream = StringStream::create($object->__toString()); - $decoded = $this->getDecoder()->decode($stream); + $decoded = $this->getDecoder() + ->decode($stream) + ; - static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $decoded->getMajorType()); - static::assertEquals(CBORObject::OBJECT_UNDEFINED, $decoded->getAdditionalInformation()); + static::assertSame(CBORObject::MAJOR_TYPE_OTHER_TYPE, $decoded->getMajorType()); + static::assertSame(CBORObject::OBJECT_UNDEFINED, $decoded->getAdditionalInformation()); static::assertNull($decoded->getContent()); static::assertNotInstanceOf(Normalizable::class, $decoded); } @@ -118,8 +120,8 @@ public function createValidBreakObject(): void { $object = BreakObject::create(); - static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); - static::assertEquals(CBORObject::OBJECT_BREAK, $object->getAdditionalInformation()); + static::assertSame(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); + static::assertSame(CBORObject::OBJECT_BREAK, $object->getAdditionalInformation()); static::assertNotInstanceOf(Normalizable::class, $object); } @@ -131,15 +133,17 @@ public function createValidSimpleObjectWithoutContent(int $value): void { $object = SimpleObject::create($value); - static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); - static::assertEquals($value, $object->getAdditionalInformation()); + static::assertSame(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); + static::assertSame($value, $object->getAdditionalInformation()); static::assertNull($object->getContent()); $stream = StringStream::create($object->__toString()); - $decoded = $this->getDecoder()->decode($stream); + $decoded = $this->getDecoder() + ->decode($stream) + ; - static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $decoded->getMajorType()); - static::assertEquals($value, $decoded->getAdditionalInformation()); + static::assertSame(CBORObject::MAJOR_TYPE_OTHER_TYPE, $decoded->getMajorType()); + static::assertSame($value, $decoded->getAdditionalInformation()); static::assertNull($decoded->getContent()); //static::assertEquals($value, $decoded->normalize()); } @@ -152,9 +156,9 @@ public function createValidHalfPrecisionFloatObject(string $value, float $expect { $object = HalfPrecisionFloatObject::create($value); - static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); - static::assertEquals(CBORObject::OBJECT_HALF_PRECISION_FLOAT, $object->getAdditionalInformation()); - if (INF === $expected || $expected === -INF) { + static::assertSame(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); + static::assertSame(CBORObject::OBJECT_HALF_PRECISION_FLOAT, $object->getAdditionalInformation()); + if ($expected === INF || $expected === -INF) { static::assertInfinite($object->normalize()); } else { static::assertEqualsWithDelta($expected, $object->normalize(), $delta); @@ -169,9 +173,9 @@ public function createValidSinglePrecisionFloatObject(string $value, float $expe { $object = SinglePrecisionFloatObject::create($value); - static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); - static::assertEquals(CBORObject::OBJECT_SINGLE_PRECISION_FLOAT, $object->getAdditionalInformation()); - if (INF === $expected || $expected === -INF) { + static::assertSame(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); + static::assertSame(CBORObject::OBJECT_SINGLE_PRECISION_FLOAT, $object->getAdditionalInformation()); + if ($expected === INF || $expected === -INF) { static::assertInfinite($object->normalize()); } else { static::assertEqualsWithDelta($expected, $object->normalize(), $delta); @@ -186,9 +190,9 @@ public function createValidDoublePrecisionFloatObject(string $value, float $expe { $object = DoublePrecisionFloatObject::create($value); - static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); - static::assertEquals(CBORObject::OBJECT_DOUBLE_PRECISION_FLOAT, $object->getAdditionalInformation()); - if (INF === $expected || $expected === -INF) { + static::assertSame(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); + static::assertSame(CBORObject::OBJECT_DOUBLE_PRECISION_FLOAT, $object->getAdditionalInformation()); + if ($expected === INF || $expected === -INF) { static::assertInfinite($object->normalize()); } else { static::assertEqualsWithDelta($expected, $object->normalize(), $delta); @@ -203,17 +207,19 @@ public function createValidSimpleObjectWithContent(int $value): void { $object = SimpleObject::create($value); - static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); - static::assertEquals(CBORObject::OBJECT_SIMPLE_VALUE, $object->getAdditionalInformation()); - static::assertEquals(chr($value), $object->getContent()); + static::assertSame(CBORObject::MAJOR_TYPE_OTHER_TYPE, $object->getMajorType()); + static::assertSame(CBORObject::OBJECT_SIMPLE_VALUE, $object->getAdditionalInformation()); + static::assertSame(chr($value), $object->getContent()); //static::assertEquals($value, $object->normalize()); $stream = StringStream::create($object->__toString()); - $decoded = $this->getDecoder()->decode($stream); + $decoded = $this->getDecoder() + ->decode($stream) + ; - static::assertEquals(CBORObject::MAJOR_TYPE_OTHER_TYPE, $decoded->getMajorType()); - static::assertEquals(CBORObject::OBJECT_SIMPLE_VALUE, $decoded->getAdditionalInformation()); - static::assertEquals(chr($value), $decoded->getContent()); + static::assertSame(CBORObject::MAJOR_TYPE_OTHER_TYPE, $decoded->getMajorType()); + static::assertSame(CBORObject::OBJECT_SIMPLE_VALUE, $decoded->getAdditionalInformation()); + static::assertSame(chr($value), $decoded->getContent()); //static::assertEquals($value, $decoded->normalize()); } @@ -241,19 +247,12 @@ public function createInvalidSimpleObjectOutOfRange(): void public function getSimpleObjectWithoutContent(): array { - return [ - [0], - [18], - [19], - ]; + return [[0], [18], [19]]; } public function getSimpleObjectWithContent(): array { - return [ - [32], - [255], - ]; + return [[32], [255]]; } /** @@ -262,71 +261,19 @@ public function getSimpleObjectWithContent(): array public function getHalfPrecisionFloatObject(): array { return [ - [ - $this->bin('0000000000000001', 2), - 0.000000059604645, - 0.000000000000001, - ], - [ - $this->bin('0000001111111111', 2), - 0.000060975552, - 0.000000000001, - ], - [ - $this->bin('0000010000000000', 2), - 0.00006103515625, - 0.00000000000001, - ], - [ - $this->bin('0111101111111111', 2), - 65504, - 1, - ], - [ - $this->bin('0011101111111111', 2), - 0.99951172, - 0.00000001, - ], - [ - $this->bin('0011110000000000', 2), - 1, - 1, - ], - [ - $this->bin('0011110000000001', 2), - 1.00097656, - 0.00000001, - ], - [ - $this->bin('0011010101010101', 2), - 0.333251953125, - 0.000000000001, - ], - [ - $this->bin('1100000000000000', 2), - -1, - 1, - ], - [ - $this->bin('0000000000000000', 2), - 0, - 1, - ], - [ - $this->bin('1000000000000000', 2), - -0, - 1, - ], - [ - $this->bin('0111110000000000', 2), - INF, - 1, - ], - [ - $this->bin('1111110000000000', 2), - -INF, - 1, - ], + [$this->bin('0000000000000001', 2), 0.000000059604645, 0.000000000000001], + [$this->bin('0000001111111111', 2), 0.000060975552, 0.000000000001], + [$this->bin('0000010000000000', 2), 0.00006103515625, 0.00000000000001], + [$this->bin('0111101111111111', 2), 65504, 1], + [$this->bin('0011101111111111', 2), 0.99951172, 0.00000001], + [$this->bin('0011110000000000', 2), 1, 1], + [$this->bin('0011110000000001', 2), 1.00097656, 0.00000001], + [$this->bin('0011010101010101', 2), 0.333251953125, 0.000000000001], + [$this->bin('1100000000000000', 2), -1, 1], + [$this->bin('0000000000000000', 2), 0, 1], + [$this->bin('1000000000000000', 2), -0, 1], + [$this->bin('0111110000000000', 2), INF, 1], + [$this->bin('1111110000000000', 2), -INF, 1], ]; } @@ -336,76 +283,20 @@ public function getHalfPrecisionFloatObject(): array public function getSinglePrecisionFloatObject(): array { return [ - [ - $this->bin('00000000000000000000000000000001', 4), - 2 ** -149, - 10 ** -149, - ], - [ - $this->bin('00000000011111111111111111111111', 4), - 1.1754942107 * 10 ** -38, - 10 ** -38, - ], - [ - $this->bin('00000000100000000000000000000000', 4), - 1.1754943508 * 10 ** -38, - 10 ** -38, - ], - [ - $this->bin('01111111011111111111111111111111', 4), - 3.4028234664 * 10 ** 38, - 10 ** 38, - ], - [ - $this->bin('00111111011111111111111111111111', 4), - 0.999999940395355225, - 0.000000000000000001, - ], - [ - $this->bin('00111111100000000000000000000000', 4), - 1, - 1, - ], - [ - $this->bin('00111111100000000000000000000001', 4), - 1.00000011920928955, - 0.00000000000000001, - ], - [ - $this->bin('11000000000000000000000000000000', 4), - -2, - 1, - ], - [ - $this->bin('00000000000000000000000000000000', 4), - 0, - 0, - ], - [ - $this->bin('10000000000000000000000000000000', 4), - -0, - 0, - ], - [ - $this->bin('01111111100000000000000000000000', 4), - INF, - 0, - ], - [ - $this->bin('11111111100000000000000000000000', 4), - -INF, - 0, - ], - [ - $this->bin('01000000010010010000111111011011', 4), - 3.14159274101257324, - 0.00000000000000001, - ], - [ - $this->bin('00111110101010101010101010101011', 4), - 0.333333343267440796, - 0.000000000000000001, - ], + [$this->bin('00000000000000000000000000000001', 4), 2 ** -149, 10 ** -149], + [$this->bin('00000000011111111111111111111111', 4), 1.1754942107 * 10 ** -38, 10 ** -38], + [$this->bin('00000000100000000000000000000000', 4), 1.1754943508 * 10 ** -38, 10 ** -38], + [$this->bin('01111111011111111111111111111111', 4), 3.4028234664 * 10 ** 38, 10 ** 38], + [$this->bin('00111111011111111111111111111111', 4), 0.999999940395355225, 0.000000000000000001], + [$this->bin('00111111100000000000000000000000', 4), 1, 1], + [$this->bin('00111111100000000000000000000001', 4), 1.00000011920928955, 0.00000000000000001], + [$this->bin('11000000000000000000000000000000', 4), -2, 1], + [$this->bin('00000000000000000000000000000000', 4), 0, 0], + [$this->bin('10000000000000000000000000000000', 4), -0, 0], + [$this->bin('01111111100000000000000000000000', 4), INF, 0], + [$this->bin('11111111100000000000000000000000', 4), -INF, 0], + [$this->bin('01000000010010010000111111011011', 4), 3.14159274101257324, 0.00000000000000001], + [$this->bin('00111110101010101010101010101011', 4), 0.333333343267440796, 0.000000000000000001], ]; } @@ -415,11 +306,7 @@ public function getSinglePrecisionFloatObject(): array public function getDoublePrecisionFloatObject(): array { return [ - [ - $this->bin('0011111111110000000000000000000000000000000000000000000000000000', 8), - 1, - 1, - ], + [$this->bin('0011111111110000000000000000000000000000000000000000000000000000', 8), 1, 1], [ $this->bin('0011111111110000000000000000000000000000000000000000000000000001', 8), 1.0000000000000002, @@ -500,16 +387,8 @@ public function getDoublePrecisionFloatObject(): array -0, 0.0000000000000001, ], - [ - $this->bin('0111111111110000000000000000000000000000000000000000000000000000', 8), - INF, - 1, - ], - [ - $this->bin('1111111111110000000000000000000000000000000000000000000000000000', 8), - -INF, - 1, - ], + [$this->bin('0111111111110000000000000000000000000000000000000000000000000000', 8), INF, 1], + [$this->bin('1111111111110000000000000000000000000000000000000000000000000000', 8), -INF, 1], [ $this->bin('0011111111010101010101010101010101010101010101010101010101010101', 8), 1 / 3, @@ -517,7 +396,7 @@ public function getDoublePrecisionFloatObject(): array ], [ $this->bin('0100000000001001001000011111101101010100010001000010110100011000', 8), - pi(), + M_PI, 0.0000000000000001, ], ]; @@ -526,14 +405,7 @@ public function getDoublePrecisionFloatObject(): array private function bin(string $binary, int $length): string { return str_pad( - hex2bin( - str_pad( - base_convert($binary, 2, 16), - $length * 2, - '0', - STR_PAD_LEFT - ) - ), + hex2bin(str_pad(base_convert($binary, 2, 16), $length * 2, '0', STR_PAD_LEFT)), $length, '0', STR_PAD_LEFT diff --git a/tests/OtherTest.php b/tests/OtherTest.php index d91e2e4..7a0b4e8 100644 --- a/tests/OtherTest.php +++ b/tests/OtherTest.php @@ -2,15 +2,6 @@ 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\Test; use CBOR\StringStream; @@ -18,7 +9,7 @@ /** * @internal */ -final class OtherTest extends BaseTestCase +final class OtherTest extends CBORTestCase { /** * @test @@ -27,21 +18,15 @@ final class OtherTest extends BaseTestCase public function aSignedIntegerCanBeParsed(string $data): void { $stream = StringStream::create(hex2bin($data)); - $object = $this->getDecoder()->decode($stream); + $object = $this->getDecoder() + ->decode($stream) + ; $object->getNormalizedData(); - static::assertEquals($data, bin2hex((string) $object)); + static::assertSame($data, bin2hex((string) $object)); } public function getDataSet(): array { - return [ - ['f4'], - ['f5'], - ['f6'], - ['f7'], - ['f0'], - ['f820'], - ['f8ff'], - ]; + return [['f4'], ['f5'], ['f6'], ['f7'], ['f0'], ['f820'], ['f8ff']]; } } diff --git a/tests/SignedIntegerTest.php b/tests/SignedIntegerTest.php index 5188dcb..53a0e11 100644 --- a/tests/SignedIntegerTest.php +++ b/tests/SignedIntegerTest.php @@ -2,15 +2,6 @@ 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\Test; use CBOR\NegativeIntegerObject; @@ -19,53 +10,32 @@ /** * @internal */ -final class SignedIntegerTest extends BaseTestCase +final class SignedIntegerTest extends CBORTestCase { /** * @test * @dataProvider getValidValue */ - public function createOnValidValue(int $intValue, string $expectedIntValue, int $expectedMajorType, int $expectedAdditionalInformation): void - { + public function createOnValidValue( + int $intValue, + string $expectedIntValue, + int $expectedMajorType, + int $expectedAdditionalInformation + ): void { $unsignedInteger = NegativeIntegerObject::create($intValue); - static::assertEquals($expectedIntValue, $unsignedInteger->getValue()); - static::assertEquals($expectedMajorType, $unsignedInteger->getMajorType()); - static::assertEquals($expectedAdditionalInformation, $unsignedInteger->getAdditionalInformation()); + static::assertSame($expectedIntValue, $unsignedInteger->getValue()); + static::assertSame($expectedMajorType, $unsignedInteger->getMajorType()); + static::assertSame($expectedAdditionalInformation, $unsignedInteger->getAdditionalInformation()); } public function getValidValue(): array { return [ - [ - -12345678, - '-12345678', - 1, - 26, - ], - [ - -255, - '-255', - 1, - 24, - ], - [ - -254, - '-254', - 1, - 24, - ], - [ - -65535, - '-65535', - 1, - 25, - ], - [ - -18, - '-18', - 1, - 17, - ], + [-12345678, '-12345678', 1, 26], + [-255, '-255', 1, 24], + [-254, '-254', 1, 24], + [-65535, '-65535', 1, 25], + [-18, '-18', 1, 17], ]; } @@ -85,7 +55,9 @@ public function ceateOnNegativeValue(): void public function createOnOutOfRangeValue(): void { $this->expectException('InvalidArgumentException'); - $this->expectExceptionMessage('Out of range. Please use NegativeBigIntegerTag tag with ByteStringObject object instead.'); + $this->expectExceptionMessage( + 'Out of range. Please use NegativeBigIntegerTag tag with ByteStringObject object instead.' + ); NegativeIntegerObject::create(-4294967297); } @@ -96,34 +68,21 @@ public function createOnOutOfRangeValue(): void public function anUnsignedIntegerCanBeEncodedAndDecoded(string $data, string $expectedNormalizedData): void { $stream = StringStream::create(hex2bin($data)); - $object = $this->getDecoder()->decode($stream); + $object = $this->getDecoder() + ->decode($stream) + ; $object->getNormalizedData(); - static::assertEquals($data, bin2hex((string) $object)); - static::assertEquals($expectedNormalizedData, $object->getNormalizedData()); + static::assertSame($data, bin2hex((string) $object)); + static::assertSame($expectedNormalizedData, $object->getNormalizedData()); } public function getDataSet(): array { return [ - [ - '20', - '-1', - ], [ - '29', - '-10', - ], [ - '3863', - '-100', - ], [ - '3903e7', - '-1000', - ], [ + ['20', '-1'], ['29', '-10'], ['3863', '-100'], ['3903e7', '-1000'], [ 'c349010000000000000000', '-18446744073709551617', - ], [ - '3bffffffffffffffff', - '-18446744073709551616', - ], + ], ['3bffffffffffffffff', '-18446744073709551616'], ]; } } diff --git a/tests/Tag/DatetimeTagTest.php b/tests/Tag/DatetimeTagTest.php index cdbd354..7ea5c4f 100644 --- a/tests/Tag/DatetimeTagTest.php +++ b/tests/Tag/DatetimeTagTest.php @@ -2,15 +2,6 @@ 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\Test\Tag; use CBOR\ByteStringObject; @@ -38,7 +29,7 @@ final class DatetimeTagTest extends TestCase public function createValidDatetimeTag(CBORObject $object, string $expectedTimestamp): void { $tag = DatetimeTag::create($object); - static::assertEquals($expectedTimestamp, $tag->getNormalizedData()->format('U.u')); + static::assertSame($expectedTimestamp, $tag->getNormalizedData()->format('U.u')); } /** @@ -49,9 +40,7 @@ public function createInvalidDatetimeTag(): void $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('This tag only accepts a Byte String object.'); - DatetimeTag::create( - ByteStringObject::create('data') - ); + DatetimeTag::create(ByteStringObject::create('data')); } /** @@ -59,10 +48,8 @@ public function createInvalidDatetimeTag(): void */ public function createValidTimestampTagWithUnsignedInteger(): void { - $tag = TimestampTag::create( - UnsignedIntegerObject::create(0) - ); - static::assertEquals('0.000000', $tag->getNormalizedData()->format('U.u')); + $tag = TimestampTag::create(UnsignedIntegerObject::create(0)); + static::assertSame('0.000000', $tag->getNormalizedData()->format('U.u')); } /** @@ -70,10 +57,8 @@ public function createValidTimestampTagWithUnsignedInteger(): void */ public function createValidTimestampTagWithNegativeInteger(): void { - $tag = TimestampTag::create( - NegativeIntegerObject::create(-10) - ); - static::assertEquals('-10.000000', $tag->getNormalizedData()->format('U.u')); + $tag = TimestampTag::create(NegativeIntegerObject::create(-10)); + static::assertSame('-10.000000', $tag->getNormalizedData()->format('U.u')); } /** @@ -84,7 +69,7 @@ public function createValidTimestampTagWithHalfPrecisionFloat(): void $tag = TimestampTag::create( HalfPrecisionFloatObject::create(hex2bin(base_convert('0011010101010101', 2, 16))) ); - static::assertEquals('0.333251', $tag->normalize()->format('U.u')); + static::assertSame('0.333251', $tag->normalize()->format('U.u')); } /** @@ -95,7 +80,7 @@ public function createValidTimestampTagWithSinglePrecisionFloat(): void $tag = TimestampTag::create( SinglePrecisionFloatObject::create(hex2bin(base_convert('00111111011111111111111111111111', 2, 16))) ); - static::assertEquals('0.999999', $tag->normalize()->format('U.u')); + static::assertSame('0.999999', $tag->normalize()->format('U.u')); } /** @@ -104,18 +89,17 @@ public function createValidTimestampTagWithSinglePrecisionFloat(): void public function createValidTimestampTagWithDoublePrecisionFloat(): void { $tag = TimestampTag::create( - DoublePrecisionFloatObject::create(hex2bin(base_convert('0100000000001001001000011111101101010100010001000010110100011000', 2, 16))) + DoublePrecisionFloatObject::create( + hex2bin(base_convert('0100000000001001001000011111101101010100010001000010110100011000', 2, 16)) + ) ); - static::assertEquals('3.141592', $tag->normalize()->format('U.u')); + static::assertSame('3.141592', $tag->normalize()->format('U.u')); } public function getDatetimes(): array { $buildTestEntry = static function (string $datetime, string $timestamp): array { - return [ - TextStringObject::create($datetime), - $timestamp, - ]; + return [TextStringObject::create($datetime), $timestamp]; }; return [ diff --git a/tests/Tag/EncodingTagsTest.php b/tests/Tag/EncodingTagsTest.php index 7f6b10b..3f61ab7 100644 --- a/tests/Tag/EncodingTagsTest.php +++ b/tests/Tag/EncodingTagsTest.php @@ -2,15 +2,6 @@ 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\Test\Tag; use CBOR\ByteStringObject; @@ -33,13 +24,11 @@ final class EncodingTagsTest extends TestCase */ public function createValidBase16EncodingTag(): void { - $tag = Base16EncodingTag::create( - TextStringObject::create('Text') - ); + $tag = Base16EncodingTag::create(TextStringObject::create('Text')); static::assertInstanceOf(TextStringObject::class, $tag->getValue()); - static::assertEquals(CBORObject::MAJOR_TYPE_TAG, $tag->getMajorType()); - static::assertEquals(CBORObject::TAG_ENCODED_BASE16, $tag->getAdditionalInformation()); + static::assertSame(CBORObject::MAJOR_TYPE_TAG, $tag->getMajorType()); + static::assertSame(CBORObject::TAG_ENCODED_BASE16, $tag->getAdditionalInformation()); } /** @@ -47,13 +36,11 @@ public function createValidBase16EncodingTag(): void */ public function createValidBase64EncodingTag(): void { - $tag = Base64EncodingTag::create( - TextStringObject::create('Text') - ); + $tag = Base64EncodingTag::create(TextStringObject::create('Text')); static::assertInstanceOf(TextStringObject::class, $tag->getValue()); - static::assertEquals(CBORObject::MAJOR_TYPE_TAG, $tag->getMajorType()); - static::assertEquals(CBORObject::TAG_ENCODED_BASE64, $tag->getAdditionalInformation()); + static::assertSame(CBORObject::MAJOR_TYPE_TAG, $tag->getMajorType()); + static::assertSame(CBORObject::TAG_ENCODED_BASE64, $tag->getAdditionalInformation()); } /** @@ -61,13 +48,11 @@ public function createValidBase64EncodingTag(): void */ public function createValidBase64UrlEncodingTag(): void { - $tag = Base64UrlEncodingTag::create( - TextStringObject::create('Text') - ); + $tag = Base64UrlEncodingTag::create(TextStringObject::create('Text')); static::assertInstanceOf(TextStringObject::class, $tag->getValue()); - static::assertEquals(CBORObject::MAJOR_TYPE_TAG, $tag->getMajorType()); - static::assertEquals(CBORObject::TAG_ENCODED_BASE64_URL, $tag->getAdditionalInformation()); + static::assertSame(CBORObject::MAJOR_TYPE_TAG, $tag->getMajorType()); + static::assertSame(CBORObject::TAG_ENCODED_BASE64_URL, $tag->getAdditionalInformation()); } /** @@ -75,13 +60,11 @@ public function createValidBase64UrlEncodingTag(): void */ public function createValidCBOREncodingTag(): void { - $tag = CBOREncodingTag::create( - ByteStringObject::create('Text') - ); + $tag = CBOREncodingTag::create(ByteStringObject::create('Text')); static::assertInstanceOf(ByteStringObject::class, $tag->getValue()); - static::assertEquals(CBORObject::MAJOR_TYPE_TAG, $tag->getMajorType()); - static::assertEquals(CBORObject::TAG_ENCODED_CBOR, $tag->getAdditionalInformation()); + static::assertSame(CBORObject::MAJOR_TYPE_TAG, $tag->getMajorType()); + static::assertSame(CBORObject::TAG_ENCODED_CBOR, $tag->getAdditionalInformation()); } /** @@ -91,8 +74,6 @@ public function createInvalidCBOREncodingTag(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('This tag only accepts a Byte String object.'); - CBOREncodingTag::create( - TextStringObject::create('Text') - ); + CBOREncodingTag::create(TextStringObject::create('Text')); } } diff --git a/tests/Tag/MimeTagTest.php b/tests/Tag/MimeTagTest.php index 55d2328..f11af50 100644 --- a/tests/Tag/MimeTagTest.php +++ b/tests/Tag/MimeTagTest.php @@ -2,15 +2,6 @@ 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\Test\Tag; use CBOR\IndefiniteLengthTextStringObject; @@ -30,10 +21,8 @@ final class MimeTagTest extends TestCase */ public function createValidTagFromTextStringObject(): void { - $tag = MimeTag::create( - TextStringObject::create('text/plain') - ); - static::assertEquals('text/plain', $tag->normalize()); + $tag = MimeTag::create(TextStringObject::create('text/plain')); + static::assertSame('text/plain', $tag->normalize()); } /** @@ -47,7 +36,7 @@ public function createValidTagFromTIndefiniteLengthTextStringObject(): void ->append('/') ->append('plain') ); - static::assertEquals('text/plain', $tag->normalize()); + static::assertSame('text/plain', $tag->normalize()); } /** @@ -58,8 +47,6 @@ public function createInvalidTag(): void static::expectException(InvalidArgumentException::class); static::expectExceptionMessage('This tag only accepts a Byte String object.'); - MimeTag::create( - BreakObject::create() - ); + MimeTag::create(BreakObject::create()); } } diff --git a/tests/Tag/SimpleTagsTest.php b/tests/Tag/SimpleTagsTest.php index 1845486..d511126 100644 --- a/tests/Tag/SimpleTagsTest.php +++ b/tests/Tag/SimpleTagsTest.php @@ -2,15 +2,6 @@ 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\Test\Tag; use CBOR\ByteStringObject; @@ -33,14 +24,12 @@ final class SimpleTagsTest extends TestCase */ public function createValidUriTag(): void { - $tag = UriTag::create( - TextStringObject::create('Text') - ); + $tag = UriTag::create(TextStringObject::create('Text')); static::assertInstanceOf(TextStringObject::class, $tag->getValue()); - static::assertEquals(CBORObject::MAJOR_TYPE_TAG, $tag->getMajorType()); - static::assertEquals(CBORObject::LENGTH_1_BYTE, $tag->getAdditionalInformation()); - static::assertEquals(hex2bin(dechex(CBORObject::TAG_URI)), $tag->getData()); + static::assertSame(CBORObject::MAJOR_TYPE_TAG, $tag->getMajorType()); + static::assertSame(CBORObject::LENGTH_1_BYTE, $tag->getAdditionalInformation()); + static::assertSame(hex2bin(dechex(CBORObject::TAG_URI)), $tag->getData()); } /** @@ -50,9 +39,7 @@ public function createInvalidUriTag(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('This tag only accepts a Text String object.'); - UriTag::create( - ByteStringObject::create('Text') - ); + UriTag::create(ByteStringObject::create('Text')); } /** @@ -60,14 +47,12 @@ public function createInvalidUriTag(): void */ public function createValidBase64Tag(): void { - $tag = Base64Tag::create( - TextStringObject::create('Text') - ); + $tag = Base64Tag::create(TextStringObject::create('Text')); static::assertInstanceOf(TextStringObject::class, $tag->getValue()); - static::assertEquals(CBORObject::MAJOR_TYPE_TAG, $tag->getMajorType()); - static::assertEquals(CBORObject::LENGTH_1_BYTE, $tag->getAdditionalInformation()); - static::assertEquals(hex2bin(dechex(CBORObject::TAG_BASE64)), $tag->getData()); + static::assertSame(CBORObject::MAJOR_TYPE_TAG, $tag->getMajorType()); + static::assertSame(CBORObject::LENGTH_1_BYTE, $tag->getAdditionalInformation()); + static::assertSame(hex2bin(dechex(CBORObject::TAG_BASE64)), $tag->getData()); } /** @@ -77,9 +62,7 @@ public function createInvalidBase64Tag(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('This tag only accepts a Text String object.'); - Base64Tag::create( - ByteStringObject::create('Text') - ); + Base64Tag::create(ByteStringObject::create('Text')); } /** @@ -87,14 +70,12 @@ public function createInvalidBase64Tag(): void */ public function createValidBase64UrlTag(): void { - $tag = Base64UrlTag::create( - TextStringObject::create('Text') - ); + $tag = Base64UrlTag::create(TextStringObject::create('Text')); static::assertInstanceOf(TextStringObject::class, $tag->getValue()); - static::assertEquals(CBORObject::MAJOR_TYPE_TAG, $tag->getMajorType()); - static::assertEquals(CBORObject::LENGTH_1_BYTE, $tag->getAdditionalInformation()); - static::assertEquals(hex2bin(dechex(CBORObject::TAG_BASE64_URL)), $tag->getData()); + static::assertSame(CBORObject::MAJOR_TYPE_TAG, $tag->getMajorType()); + static::assertSame(CBORObject::LENGTH_1_BYTE, $tag->getAdditionalInformation()); + static::assertSame(hex2bin(dechex(CBORObject::TAG_BASE64_URL)), $tag->getData()); } /** @@ -104,9 +85,7 @@ public function createInvalidBase64UrlTag(): void { $this->expectException(InvalidArgumentException::class); $this->expectExceptionMessage('This tag only accepts a Text String object.'); - Base64UrlTag::create( - ByteStringObject::create('Text') - ); + Base64UrlTag::create(ByteStringObject::create('Text')); } /** @@ -114,13 +93,11 @@ public function createInvalidBase64UrlTag(): void */ public function createValidCBORTag(): void { - $tag = CBORTag::create( - ByteStringObject::create('Text') - ); + $tag = CBORTag::create(ByteStringObject::create('Text')); static::assertInstanceOf(ByteStringObject::class, $tag->getValue()); - static::assertEquals(CBORObject::MAJOR_TYPE_TAG, $tag->getMajorType()); - static::assertEquals(CBORObject::LENGTH_2_BYTES, $tag->getAdditionalInformation()); - static::assertEquals(hex2bin(dechex(CBORObject::TAG_CBOR)), $tag->getData()); + static::assertSame(CBORObject::MAJOR_TYPE_TAG, $tag->getMajorType()); + static::assertSame(CBORObject::LENGTH_2_BYTES, $tag->getAdditionalInformation()); + static::assertSame(hex2bin(dechex(CBORObject::TAG_CBOR)), $tag->getData()); } } diff --git a/tests/TextStringObjectTest.php b/tests/TextStringObjectTest.php index a2df9e4..fac97f3 100644 --- a/tests/TextStringObjectTest.php +++ b/tests/TextStringObjectTest.php @@ -2,15 +2,6 @@ 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\Test; use CBOR\CBORObject; @@ -18,38 +9,42 @@ use CBOR\TextStringObject; /** - * @covers \CBOR\TextStringObject - * * @internal */ -final class TextStringObjectTest extends BaseTestCase +final class TextStringObjectTest extends CBORTestCase { /** * @test * @dataProvider getData */ - public function aTextStringObjectCanBeCreated(string $string, int $expectedAdditionalInformation, int $expectedLength, string $expectedEncodedObject): void - { + public function aTextStringObjectCanBeCreated( + string $string, + int $expectedAdditionalInformation, + int $expectedLength, + string $expectedEncodedObject + ): void { $object = TextStringObject::create($string); - static::assertEquals(CBORObject::MAJOR_TYPE_TEXT_STRING, $object->getMajorType()); - static::assertEquals($expectedAdditionalInformation, $object->getAdditionalInformation()); - static::assertEquals($string, $object->getValue()); - static::assertEquals($expectedLength, $object->getLength()); - static::assertEquals($string, $object->getNormalizedData()); + static::assertSame(CBORObject::MAJOR_TYPE_TEXT_STRING, $object->getMajorType()); + static::assertSame($expectedAdditionalInformation, $object->getAdditionalInformation()); + static::assertSame($string, $object->getValue()); + static::assertSame($expectedLength, $object->getLength()); + static::assertSame($string, $object->getNormalizedData()); $binary = (string) $object; - static::assertEquals(hex2bin($expectedEncodedObject), $binary); + static::assertSame(hex2bin($expectedEncodedObject), $binary); $stream = StringStream::create($binary); - $decoded = $this->getDecoder()->decode($stream); + $decoded = $this->getDecoder() + ->decode($stream) + ; static::assertInstanceOf(TextStringObject::class, $decoded); - static::assertEquals(CBORObject::MAJOR_TYPE_TEXT_STRING, $decoded->getMajorType()); - static::assertEquals($expectedAdditionalInformation, $decoded->getAdditionalInformation()); - static::assertEquals($string, $decoded->getValue()); - static::assertEquals($expectedLength, $decoded->getLength()); - static::assertEquals($string, $decoded->getNormalizedData()); + static::assertSame(CBORObject::MAJOR_TYPE_TEXT_STRING, $decoded->getMajorType()); + static::assertSame($expectedAdditionalInformation, $decoded->getAdditionalInformation()); + static::assertSame($string, $decoded->getValue()); + static::assertSame($expectedLength, $decoded->getLength()); + static::assertSame($string, $decoded->getNormalizedData()); } public function getData(): array diff --git a/tests/UnsignedIntegerTest.php b/tests/UnsignedIntegerTest.php index f30a241..8cc6f08 100644 --- a/tests/UnsignedIntegerTest.php +++ b/tests/UnsignedIntegerTest.php @@ -2,15 +2,6 @@ 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\Test; use CBOR\StringStream; @@ -19,48 +10,27 @@ /** * @internal */ -final class UnsignedIntegerTest extends BaseTestCase +final class UnsignedIntegerTest extends CBORTestCase { /** * @test * @dataProvider getValidValue */ - public function createOnValidValue(int $intValue, string $expectedIntValue, int $expectedMajorType, int $expectedAdditionalInformation): void - { + public function createOnValidValue( + int $intValue, + string $expectedIntValue, + int $expectedMajorType, + int $expectedAdditionalInformation + ): void { $unsignedInteger = UnsignedIntegerObject::create($intValue); - static::assertEquals($expectedIntValue, $unsignedInteger->getValue()); - static::assertEquals($expectedMajorType, $unsignedInteger->getMajorType()); - static::assertEquals($expectedAdditionalInformation, $unsignedInteger->getAdditionalInformation()); + static::assertSame($expectedIntValue, $unsignedInteger->getValue()); + static::assertSame($expectedMajorType, $unsignedInteger->getMajorType()); + static::assertSame($expectedAdditionalInformation, $unsignedInteger->getAdditionalInformation()); } public function getValidValue(): array { - return [ - [ - 12345678, - '12345678', - 0, - 26, - ], - [ - 255, - '255', - 0, - 25, - ], - [ - 254, - '254', - 0, - 24, - ], - [ - 18, - '18', - 0, - 18, - ], - ]; + return [[12345678, '12345678', 0, 26], [255, '255', 0, 25], [254, '254', 0, 24], [18, '18', 0, 18]]; } /** @@ -79,7 +49,9 @@ public function createOnNegativeValue(): void public function createOnOutOfRangeValue(): void { $this->expectException('InvalidArgumentException'); - $this->expectExceptionMessage('Out of range. Please use PositiveBigIntegerTag tag with ByteStringObject object instead.'); + $this->expectExceptionMessage( + 'Out of range. Please use PositiveBigIntegerTag tag with ByteStringObject object instead.' + ); UnsignedIntegerObject::create(4294967296); } @@ -90,51 +62,23 @@ public function createOnOutOfRangeValue(): void public function anUnsignedIntegerCanBeParsed(string $data, string $expectedNormalizedData): void { $stream = StringStream::create(hex2bin($data)); - $object = $this->getDecoder()->decode($stream); - static::assertEquals($data, bin2hex((string) $object)); - static::assertEquals($expectedNormalizedData, $object->getNormalizedData()); + $object = $this->getDecoder() + ->decode($stream) + ; + static::assertSame($data, bin2hex((string) $object)); + static::assertSame($expectedNormalizedData, $object->getNormalizedData()); } public function getDataSet(): array { return [ - [ - '00', - '0', - ], [ - '01', - '1', - ], [ - '0a', - '10', - ], [ - '17', - '23', - ], [ + ['00', '0'], ['01', '1'], ['0a', '10'], ['17', '23'], [ '1818', '24', - ], [ - '1819', - '25', - ], [ - '1864', - '100', - ], [ - '1903e8', - '1000', - ], [ - '1a000f4240', - '1000000', - ], [ + ], ['1819', '25'], ['1864', '100'], ['1903e8', '1000'], ['1a000f4240', '1000000'], [ '1b000000e8d4a51000', '1000000000000', - ], [ - '1bffffffffffffffff', - '18446744073709551615', - ], [ - 'c249010000000000000000', - '18446744073709551616', - ], + ], ['1bffffffffffffffff', '18446744073709551615'], ['c249010000000000000000', '18446744073709551616'], ]; } } diff --git a/tests/VectorTest.php b/tests/VectorTest.php index 4eff27b..367faff 100644 --- a/tests/VectorTest.php +++ b/tests/VectorTest.php @@ -2,23 +2,15 @@ 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\Test; use CBOR\StringStream; +use const JSON_THROW_ON_ERROR; /** * @internal */ -final class VectorTest extends BaseTestCase +final class VectorTest extends CBORTestCase { /** * @test @@ -27,13 +19,15 @@ final class VectorTest extends BaseTestCase public function createOnValidValue(string $cbor, string $hex): void { $stream = StringStream::create(base64_decode($cbor, true)); - $result = $this->getDecoder()->decode($stream); + $result = $this->getDecoder() + ->decode($stream) + ; - static::assertEquals(hex2bin($hex), (string) $result); + static::assertSame(hex2bin($hex), (string) $result); } public function getVectors(): array { - return json_decode(file_get_contents(__DIR__.'/vectors.json'), true, 512, JSON_THROW_ON_ERROR); + return json_decode(file_get_contents(__DIR__ . '/vectors.json'), true, 512, JSON_THROW_ON_ERROR); } }