diff --git a/.docheader b/.docheader index a755722..962142e 100644 --- a/.docheader +++ b/.docheader @@ -1,8 +1,5 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ diff --git a/.github/workflows/continuous-integration.yml b/.github/workflows/continuous-integration.yml new file mode 100644 index 0000000..301cad5 --- /dev/null +++ b/.github/workflows/continuous-integration.yml @@ -0,0 +1,87 @@ +name: Continuous Integration + +on: + push: ~ + pull_request: ~ + +jobs: + tests: + name: Tests + + runs-on: ubuntu-latest + + strategy: + matrix: + php-version: + - "8.1" + - "8.2" + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php-version }} + coverage: none + + - name: Install Dependencies + run: composer install --prefer-dist + + - name: Run Unit Tests + run: ./vendor/bin/phpunit --testsuite=unit + + - name: Run Integration Tests + run: ./vendor/bin/phpunit --testsuite=integration + + code-coverage: + name: Code coverage + + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.1 + coverage: pcov + + - name: Install Dependencies + run: composer install --prefer-dist + + - name: Generating Code Coverage Report + run: ./vendor/bin/phpunit --testsuite=unit --coverage-clover=coverage.xml + + - name: Send Code Coverage Report to Codecov.io + uses: codecov/codecov-action@v3 + + static-analysis: + name: Static Analysis + + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v3 + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.1 + coverage: none + + - name: Install dependencies + run: composer install --prefer-dist + + - name: Run DocHeader + run: vendor/bin/docheader check src/ tests/ + + - name: Run PHP_CodeSniffer + run: vendor/bin/phpcs + + - name: Run PHPStan + run: vendor/bin/phpstan diff --git a/.gitignore b/.gitignore index f94e4ec..1364696 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ .phpcs.cache +.phpunit.cache composer.lock phpcs.xml phpunit.xml diff --git a/.scrutinizer.yml b/.scrutinizer.yml deleted file mode 100644 index cf98f62..0000000 --- a/.scrutinizer.yml +++ /dev/null @@ -1,25 +0,0 @@ -filter: - excluded_paths: - - tests/* - -checks: - php: - code_rating: true - -tools: - php_sim: true - php_pdepend: true - php_analyzer: true - php_cpd: true - php_cs_fixer: true - php_loc: true - php_code_sniffer: - config: - standard: "PSR2" - php_mess_detector: true - php_pdepend: true - php_analyzer: true - sensiolabs_security_checker: true - php_changetracking: true - external_code_coverage: true - diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index fba25c0..0000000 --- a/.travis.yml +++ /dev/null @@ -1,45 +0,0 @@ -sudo: false - -language: php - -php: - - 7.1 - - 7.2 - - 7.3 - - nightly - -cache: - directories: - - $HOME/.composer/cache - -before_install: - - mv ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/xdebug.ini{,.disabled} || echo "xdebug not available" - -install: travis_retry composer install - -script: - - vendor/bin/phpunit --testsuite=unit - - vendor/bin/phpunit --testsuite=integration - -jobs: - allow_failures: - - php: nightly - - include: - - stage: Quality assurance - env: CODE_COVERAGE - before_script: - - 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: vendor/bin/phpunit --coverage-clover ./clover.xml - after_script: - - wget https://scrutinizer-ci.com/ocular.phar - - php ocular.phar code-coverage:upload --format=php-clover ./clover.xml - - - stage: Quality assurance - env: DOCHEADER - script: vendor/bin/docheader check src/ tests/ - - - stage: Quality assurance - env: CODE_STANDARD - script: vendor/bin/phpcs diff --git a/LICENSE.md b/LICENSE similarity index 85% rename from LICENSE.md rename to LICENSE index b7df61c..b408c9b 100644 --- a/LICENSE.md +++ b/LICENSE @@ -1,13 +1,13 @@ -# License +MIT License -Copyright (c) [Henrique Moody](http://github.com/henriquemoody). +Copyright (c) Henrique Moody Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is furnished -to do so, subject to the following conditions: +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. @@ -17,5 +17,5 @@ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -THE SOFTWARE. +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index c880904..914bfcd 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,7 @@ # Respect\Stringifier -[![Build Status](https://img.shields.io/travis/Respect/Stringifier/master.svg?style=flat-square)](http://travis-ci.org/Respect/Stringifier) -[![Scrutinizer Code Quality](https://img.shields.io/scrutinizer/g/Respect/Stringifier/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/Respect/Stringifier/?branch=master) -[![Code Coverage](https://img.shields.io/scrutinizer/coverage/g/Respect/Stringifier/master.svg?style=flat-square)](https://scrutinizer-ci.com/g/Respect/Stringifier/?branch=master) +[![Build Status](https://img.shields.io/github/actions/workflow/status/Respect/Stringifier/continuous-integration.yml?branch=master&style=flat-square)](https://github.com/Respect/Stringifier/actions/workflows/continuous-integration.yml) +[![Code Coverage](https://img.shields.io/codecov/c/github/Respect/Stringifier?style=flat-square)](https://codecov.io/gh/Respect/Stringifier) [![Latest Stable Version](https://img.shields.io/packagist/v/respect/stringifier.svg?style=flat-square)](https://packagist.org/packages/respect/stringifier) [![Total Downloads](https://img.shields.io/packagist/dt/respect/stringifier.svg?style=flat-square)](https://packagist.org/packages/respect/stringifier) [![License](https://img.shields.io/packagist/l/respect/stringifier.svg?style=flat-square)](https://packagist.org/packages/respect/stringifier) @@ -18,7 +17,7 @@ using [Composer](http://getcomposer.org). composer require respect/stringifier ``` -This library requires PHP >= 7.1. +This library requires PHP >= 8.1. ## Feature Guide diff --git a/composer.json b/composer.json index 036c52f..7aac156 100644 --- a/composer.json +++ b/composer.json @@ -3,7 +3,6 @@ "description": "Converts any value to a string", "keywords": ["respect", "stringifier", "stringify"], "type": "library", - "homepage": "http://respect.github.io/Stringifier/", "license": "MIT", "authors": [ { @@ -12,18 +11,23 @@ } ], "require": { - "php": ">=7.1" + "php": "^8.1" }, "require-dev": { "malukenho/docheader": "^0.1.7", - "phpunit/phpunit": "^6.4", - "respect/coding-standard": "^1.0", - "squizlabs/php_codesniffer": "^3.4" + "phpstan/phpstan": "^1.10", + "phpstan/phpstan-deprecation-rules": "^1.1", + "phpstan/phpstan-phpunit": "^1.3", + "phpstan/phpstan-strict-rules": "^1.5", + "phpunit/phpunit": "^10.0", + "respect/coding-standard": "^4.0", + "squizlabs/php_codesniffer": "^3.7" }, "autoload": { "psr-4": { "Respect\\Stringifier\\": "src/", - "Respect\\Stringifier\\Test\\": "tests/src/" + "Respect\\Stringifier\\Test\\": "tests/src/", + "Respect\\Stringifier\\Test\\Unit\\": "tests/unit" }, "files": [ "src/stringify.php" @@ -31,11 +35,22 @@ }, "scripts": { "docheader": "vendor/bin/docheader check src/ tests/", - "test": "vendor/bin/phpunit", - "test-unit": "vendor/bin/phpunit --testsuite=unit", - "test-integration": "vendor/bin/phpunit --testsuite=integration" + "phpcs": "vendor/bin/phpcs", + "phpstan": "vendor/bin/phpstan", + "phpunit": "vendor/bin/phpunit", + "phpunit-integration": "vendor/bin/phpunit --testsuite=integration", + "phpunit-unit": "vendor/bin/phpunit --testsuite=unit", + "qa": [ + "@docheader", + "@phpcs", + "@phpstan", + "@phpunit" + ] }, "config": { - "sort-packages": true + "sort-packages": true, + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } } } diff --git a/phpstan.neon.dist b/phpstan.neon.dist new file mode 100644 index 0000000..2aa5e2b --- /dev/null +++ b/phpstan.neon.dist @@ -0,0 +1,8 @@ +parameters: + level: max + paths: + - src/ + - tests/ + fileExtensions: + - php + - phpt diff --git a/phpunit.xml.dist b/phpunit.xml.dist index f7d29be..6951bb6 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,26 +1,19 @@ - - - - tests/unit/ - - - tests/integration/ - - - - - src/ - - + + + + + src/ + + + + + tests/integration/ + + + tests/unit/ + + diff --git a/src/Quoter.php b/src/Quoter.php index 5d1e8f7..f58a661 100644 --- a/src/Quoter.php +++ b/src/Quoter.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -15,12 +12,5 @@ interface Quoter { - /** - * Should add quotes to the given string. - * - * @param string $string The string to add quotes to - * @param int $depth The current depth - * - */ public function quote(string $string, int $depth): string; } diff --git a/src/Quoters/CodeQuoter.php b/src/Quoters/CodeQuoter.php index 99aa164..cacd454 100644 --- a/src/Quoters/CodeQuoter.php +++ b/src/Quoters/CodeQuoter.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -14,18 +11,11 @@ namespace Respect\Stringifier\Quoters; use Respect\Stringifier\Quoter; + use function sprintf; -/** - * Add "`" quotes around a string depending on its level. - * - * @author Henrique Moody - */ final class CodeQuoter implements Quoter { - /** - * {@inheritdoc} - */ public function quote(string $string, int $depth): string { if ($depth === 0) { diff --git a/src/Stringifier.php b/src/Stringifier.php index f562f63..c8d423d 100644 --- a/src/Stringifier.php +++ b/src/Stringifier.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -15,13 +12,5 @@ interface Stringifier { - /** - * Converts the value into string if possible. - * - * @param mixed $raw the raw value to be converted - * @param int $depth the current depth of the conversion - * - * @return null|string returns NULL when the conversion is not possible - */ - public function stringify($raw, int $depth): ?string; + public function stringify(mixed $raw, int $depth): ?string; } diff --git a/src/Stringifiers/ArrayStringifier.php b/src/Stringifiers/ArrayStringifier.php index be189cf..45064c3 100644 --- a/src/Stringifiers/ArrayStringifier.php +++ b/src/Stringifiers/ArrayStringifier.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -15,6 +12,7 @@ use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifier; + use function array_keys; use function count; use function implode; @@ -22,45 +20,17 @@ use function range; use function sprintf; -/** - * Converts an array value into a string. - * - * @author Henrique Moody - */ final class ArrayStringifier implements Stringifier { - /** - * @var Stringifier - */ - private $stringifier; - - /** - * @var Quoter - */ - private $quoter; - - /** - * @var int - */ - private $maximumDepth; - - /** - * @var int - */ - private $itemsLimit; - - public function __construct(Stringifier $stringifier, Quoter $quoter, int $maximumDepth, int $itemsLimit) - { - $this->stringifier = $stringifier; - $this->quoter = $quoter; - $this->maximumDepth = $maximumDepth; - $this->itemsLimit = $itemsLimit; + public function __construct( + private readonly Stringifier $stringifier, + private readonly Quoter $quoter, + private readonly int $maximumDepth, + private readonly int $itemsLimit + ) { } - /** - * {@inheritdoc} - */ - public function stringify($raw, int $depth): ?string + public function stringify(mixed $raw, int $depth): ?string { if (!is_array($raw)) { return null; diff --git a/src/Stringifiers/BoolStringifier.php b/src/Stringifiers/BoolStringifier.php index 04ea7b1..47fc84e 100644 --- a/src/Stringifiers/BoolStringifier.php +++ b/src/Stringifiers/BoolStringifier.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -15,29 +12,17 @@ use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifier; + use function is_bool; -/** - * Converts a boolean value into a string. - * - * @author Henrique Moody - */ final class BoolStringifier implements Stringifier { - /** - * @var Quoter - */ - private $quoter; - - public function __construct(Quoter $quoter) - { - $this->quoter = $quoter; + public function __construct( + private readonly Quoter $quoter + ) { } - /** - * {@inheritdoc} - */ - public function stringify($raw, int $depth): ?string + public function stringify(mixed $raw, int $depth): ?string { if (!is_bool($raw)) { return null; diff --git a/src/Stringifiers/ClusterStringifier.php b/src/Stringifiers/ClusterStringifier.php index 1226c09..3f30ad4 100644 --- a/src/Stringifiers/ClusterStringifier.php +++ b/src/Stringifiers/ClusterStringifier.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -16,17 +13,12 @@ use Respect\Stringifier\Quoters\CodeQuoter; use Respect\Stringifier\Stringifier; -/** - * Converts a value into a string using the defined Stringifiers. - * - * @author Henrique Moody - */ final class ClusterStringifier implements Stringifier { /** * @var Stringifier[] */ - private $stringifiers; + private array $stringifiers = []; /** * @param Stringifier[] ...$stringifiers @@ -36,13 +28,6 @@ public function __construct(Stringifier ...$stringifiers) $this->setStringifiers($stringifiers); } - /** - * Create a default instance of the class. - * - * This instance includes all possible stringifiers. - * - * @return ClusterStringifier - */ public static function createDefault(): self { $quoter = new CodeQuoter(); @@ -84,13 +69,10 @@ public function addStringifier(Stringifier $stringifier): void $this->stringifiers[] = $stringifier; } - /** - * {@inheritdoc} - */ - public function stringify($value, int $depth): ?string + public function stringify(mixed $raw, int $depth): ?string { foreach ($this->stringifiers as $stringifier) { - $string = $stringifier->stringify($value, $depth); + $string = $stringifier->stringify($raw, $depth); if ($string === null) { continue; } diff --git a/src/Stringifiers/DateTimeStringifier.php b/src/Stringifiers/DateTimeStringifier.php index 681d0ef..60de024 100644 --- a/src/Stringifiers/DateTimeStringifier.php +++ b/src/Stringifiers/DateTimeStringifier.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -16,42 +13,19 @@ use DateTimeInterface; use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifier; -use function get_class; + use function sprintf; -/** - * Converts an instance of DateTimeInterface into a string. - * - * @author Henrique Moody - */ final class DateTimeStringifier implements Stringifier { - /** - * @var Stringifier - */ - private $stringifier; - - /** - * @var Quoter - */ - private $quoter; - - /** - * @var string - */ - private $format; - - public function __construct(Stringifier $stringifier, Quoter $quoter, string $format) - { - $this->stringifier = $stringifier; - $this->quoter = $quoter; - $this->format = $format; + public function __construct( + private readonly Stringifier $stringifier, + private readonly Quoter $quoter, + private readonly string $format + ) { } - /** - * {@inheritdoc} - */ - public function stringify($raw, int $depth): ?string + public function stringify(mixed $raw, int $depth): ?string { if (!$raw instanceof DateTimeInterface) { return null; @@ -60,7 +34,7 @@ public function stringify($raw, int $depth): ?string return $this->quoter->quote( sprintf( '[date-time] (%s: %s)', - get_class($raw), + $raw::class, $this->stringifier->stringify($raw->format($this->format), $depth + 1) ), $depth diff --git a/src/Stringifiers/InfiniteStringifier.php b/src/Stringifiers/InfiniteStringifier.php index 904d550..9d0e596 100644 --- a/src/Stringifiers/InfiniteStringifier.php +++ b/src/Stringifiers/InfiniteStringifier.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -15,30 +12,18 @@ use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifier; + use function is_float; use function is_infinite; -/** - * Converts an infinite float value into a string. - * - * @author Henrique Moody - */ final class InfiniteStringifier implements Stringifier { - /** - * @var Quoter - */ - private $quoter; - - public function __construct(Quoter $quoter) - { - $this->quoter = $quoter; + public function __construct( + private readonly Quoter $quoter + ) { } - /** - * {@inheritdoc} - */ - public function stringify($raw, int $depth): ?string + public function stringify(mixed $raw, int $depth): ?string { if (!is_float($raw)) { return null; @@ -48,6 +33,6 @@ public function stringify($raw, int $depth): ?string return null; } - return $this->quoter->quote(($raw > 0 ? '' : '-').'INF', $depth); + return $this->quoter->quote(($raw > 0 ? '' : '-') . 'INF', $depth); } } diff --git a/src/Stringifiers/JsonParsableStringifier.php b/src/Stringifiers/JsonParsableStringifier.php index 205264b..1c5ec42 100644 --- a/src/Stringifiers/JsonParsableStringifier.php +++ b/src/Stringifiers/JsonParsableStringifier.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -14,22 +11,16 @@ namespace Respect\Stringifier\Stringifiers; use Respect\Stringifier\Stringifier; + use function json_encode; + use const JSON_PRESERVE_ZERO_FRACTION; use const JSON_UNESCAPED_SLASHES; use const JSON_UNESCAPED_UNICODE; -/** - * Converts any value into JSON parsable string representation. - * - * @author Henrique Moody - */ final class JsonParsableStringifier implements Stringifier { - /** - * {@inheritdoc} - */ - public function stringify($raw, int $depth): ?string + public function stringify(mixed $raw, int $depth): ?string { $string = json_encode($raw, (JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES | JSON_PRESERVE_ZERO_FRACTION)); if ($string === false) { diff --git a/src/Stringifiers/JsonSerializableStringifier.php b/src/Stringifiers/JsonSerializableStringifier.php index 0235c4b..55c5c6a 100644 --- a/src/Stringifiers/JsonSerializableStringifier.php +++ b/src/Stringifiers/JsonSerializableStringifier.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -16,36 +13,18 @@ use JsonSerializable; use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifier; -use function get_class; + use function sprintf; -/** - * Converts an instance of JsonSerializable into a string. - * - * @author Henrique Moody - */ final class JsonSerializableStringifier implements Stringifier { - /** - * @var Stringifier - */ - private $stringifier; - - /** - * @var Quoter - */ - private $quoter; - - public function __construct(Stringifier $stringifier, Quoter $quoter) - { - $this->stringifier = $stringifier; - $this->quoter = $quoter; + public function __construct( + private readonly Stringifier $stringifier, + private readonly Quoter $quoter + ) { } - /** - * {@inheritdoc} - */ - public function stringify($raw, int $depth): ?string + public function stringify(mixed $raw, int $depth): ?string { if (!$raw instanceof JsonSerializable) { return null; @@ -54,7 +33,7 @@ public function stringify($raw, int $depth): ?string return $this->quoter->quote( sprintf( '[json-serializable] (%s: %s)', - get_class($raw), + $raw::class, $this->stringifier->stringify($raw->jsonSerialize(), $depth + 1) ), $depth diff --git a/src/Stringifiers/NanStringifier.php b/src/Stringifiers/NanStringifier.php index 6ac4b08..20751e9 100644 --- a/src/Stringifiers/NanStringifier.php +++ b/src/Stringifiers/NanStringifier.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -15,30 +12,18 @@ use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifier; + use function is_float; use function is_nan; -/** - * Converts a NaN value into a string. - * - * @author Henrique Moody - */ final class NanStringifier implements Stringifier { - /** - * @var Quoter - */ - private $quoter; - - public function __construct(Quoter $quoter) - { - $this->quoter = $quoter; + public function __construct( + private readonly Quoter $quoter + ) { } - /** - * {@inheritdoc} - */ - public function stringify($raw, int $depth): ?string + public function stringify(mixed $raw, int $depth): ?string { if (!is_float($raw)) { return null; diff --git a/src/Stringifiers/NullStringifier.php b/src/Stringifiers/NullStringifier.php index 9cbc121..e9957cd 100644 --- a/src/Stringifiers/NullStringifier.php +++ b/src/Stringifiers/NullStringifier.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -16,27 +13,14 @@ use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifier; -/** - * Converts a NULL value into a string. - * - * @author Henrique Moody - */ final class NullStringifier implements Stringifier { - /** - * @var Quoter - */ - private $quoter; - - public function __construct(Quoter $quoter) - { - $this->quoter = $quoter; + public function __construct( + private readonly Quoter $quoter + ) { } - /** - * {@inheritdoc} - */ - public function stringify($raw, int $depth): ?string + public function stringify(mixed $raw, int $depth): ?string { if ($raw !== null) { return null; diff --git a/src/Stringifiers/ObjectStringifier.php b/src/Stringifiers/ObjectStringifier.php index 82b6dcc..7ae9663 100644 --- a/src/Stringifiers/ObjectStringifier.php +++ b/src/Stringifiers/ObjectStringifier.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -15,38 +12,20 @@ use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifier; -use function get_class; + use function get_object_vars; use function is_object; use function sprintf; -/** - * Converts an object into a string. - * - * @author Henrique Moody - */ final class ObjectStringifier implements Stringifier { - /** - * @var Stringifier - */ - private $stringifier; - - /** - * @var Quoter - */ - private $quoter; - - public function __construct(Stringifier $stringifier, Quoter $quoter) - { - $this->stringifier = $stringifier; - $this->quoter = $quoter; + public function __construct( + private readonly Stringifier $stringifier, + private readonly Quoter $quoter + ) { } - /** - * {@inheritdoc} - */ - public function stringify($raw, int $depth): ?string + public function stringify(mixed $raw, int $depth): ?string { if (!is_object($raw)) { return null; @@ -55,7 +34,7 @@ public function stringify($raw, int $depth): ?string return $this->quoter->quote( sprintf( '[object] (%s: %s)', - get_class($raw), + $raw::class, $this->stringifier->stringify(get_object_vars($raw), $depth + 1) ), $depth diff --git a/src/Stringifiers/ResourceStringifier.php b/src/Stringifiers/ResourceStringifier.php index 679dd5e..232ef5a 100644 --- a/src/Stringifiers/ResourceStringifier.php +++ b/src/Stringifiers/ResourceStringifier.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -15,31 +12,19 @@ use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifier; + use function get_resource_type; use function is_resource; use function sprintf; -/** - * Converts a resource value into a string. - * - * @author Henrique Moody - */ final class ResourceStringifier implements Stringifier { - /** - * @var Quoter - */ - private $quoter; - - public function __construct(Quoter $quoter) - { - $this->quoter = $quoter; + public function __construct( + private readonly Quoter $quoter + ) { } - /** - * {@inheritdoc} - */ - public function stringify($raw, int $depth): ?string + public function stringify(mixed $raw, int $depth): ?string { if (!is_resource($raw)) { return null; diff --git a/src/Stringifiers/StringableObjectStringifier.php b/src/Stringifiers/StringableObjectStringifier.php index 6151ffe..7b7ea29 100644 --- a/src/Stringifiers/StringableObjectStringifier.php +++ b/src/Stringifiers/StringableObjectStringifier.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -14,30 +11,18 @@ namespace Respect\Stringifier\Stringifiers; use Respect\Stringifier\Stringifier; + use function is_object; use function method_exists; -/** - * Converts a object that implements the __toString() magic method into a string. - * - * @author Henrique Moody - */ final class StringableObjectStringifier implements Stringifier { - /** - * @var Stringifier - */ - private $stringifier; - - public function __construct(Stringifier $stringifier) - { - $this->stringifier = $stringifier; + public function __construct( + private readonly Stringifier $stringifier + ) { } - /** - * {@inheritdoc} - */ - public function stringify($raw, int $depth): ?string + public function stringify(mixed $raw, int $depth): ?string { if (!is_object($raw)) { return null; diff --git a/src/Stringifiers/ThrowableStringifier.php b/src/Stringifiers/ThrowableStringifier.php index 081978f..9adc984 100644 --- a/src/Stringifiers/ThrowableStringifier.php +++ b/src/Stringifiers/ThrowableStringifier.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -16,38 +13,20 @@ use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifier; use Throwable; -use function get_class; + use function getcwd; use function sprintf; use function str_replace; -/** - * Converts an instance of Throwable into a string. - * - * @author Henrique Moody - */ final class ThrowableStringifier implements Stringifier { - /** - * @var Stringifier - */ - private $stringifier; - - /** - * @var Quoter - */ - private $quoter; - - public function __construct(Stringifier $stringifier, Quoter $quoter) - { - $this->stringifier = $stringifier; - $this->quoter = $quoter; + public function __construct( + private readonly Stringifier $stringifier, + private readonly Quoter $quoter + ) { } - /** - * {@inheritdoc} - */ - public function stringify($raw, int $depth): ?string + public function stringify(mixed $raw, int $depth): ?string { if (!$raw instanceof Throwable) { return null; @@ -56,7 +35,7 @@ public function stringify($raw, int $depth): ?string return $this->quoter->quote( sprintf( '[throwable] (%s: %s)', - get_class($raw), + $raw::class, $this->stringifier->stringify($this->getData($raw), $depth + 1) ), $depth @@ -73,7 +52,7 @@ private function getData(Throwable $throwable): array 'code' => $throwable->getCode(), 'file' => sprintf( '%s:%d', - str_replace(getcwd().'/', '', $throwable->getFile()), + str_replace(getcwd() . '/', '', $throwable->getFile()), $throwable->getLine() ), ]; diff --git a/src/Stringifiers/TraversableStringifier.php b/src/Stringifiers/TraversableStringifier.php index c10023c..f91614e 100644 --- a/src/Stringifiers/TraversableStringifier.php +++ b/src/Stringifiers/TraversableStringifier.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -16,37 +13,19 @@ use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifier; use Traversable; -use function get_class; + use function iterator_to_array; use function sprintf; -/** - * Converts an instance of Traversable into a string. - * - * @author Henrique Moody - */ final class TraversableStringifier implements Stringifier { - /** - * @var Stringifier - */ - private $stringifier; - - /** - * @var Quoter - */ - private $quoter; - - public function __construct(Stringifier $stringifier, Quoter $quoter) - { - $this->stringifier = $stringifier; - $this->quoter = $quoter; + public function __construct( + private readonly Stringifier $stringifier, + private readonly Quoter $quoter + ) { } - /** - * {@inheritdoc} - */ - public function stringify($raw, int $depth): ?string + public function stringify(mixed $raw, int $depth): ?string { if (!$raw instanceof Traversable) { return null; @@ -55,7 +34,7 @@ public function stringify($raw, int $depth): ?string return $this->quoter->quote( sprintf( '[traversable] (%s: %s)', - get_class($raw), + $raw::class, $this->stringifier->stringify(iterator_to_array($raw), $depth + 1) ), $depth diff --git a/src/stringify.php b/src/stringify.php index e1c6786..89645f3 100644 --- a/src/stringify.php +++ b/src/stringify.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -15,10 +12,7 @@ use Respect\Stringifier\Stringifiers\ClusterStringifier; -/** - * @param mixed $value - */ -function stringify($value): string +function stringify(mixed $value): string { static $stringifier; diff --git a/tests/integration/stringify-object-dateTime.phpt b/tests/integration/stringify-object-dateTime.phpt index 879cf7f..5faad4e 100644 --- a/tests/integration/stringify-object-dateTime.phpt +++ b/tests/integration/stringify-object-dateTime.phpt @@ -7,7 +7,7 @@ require 'vendor/autoload.php'; use function Respect\Stringifier\stringify; -$dateTime = DateTime::createFromFormat('Y-m-d\TH:i:sP', '2017-12-31T23:59:59+00:00'); +$dateTime = new DateTime('2017-12-31T23:59:59+00:00'); $dateTimeImmutable = DateTimeImmutable::createFromMutable($dateTime); echo implode( diff --git a/tests/src/MyJsonSerializable.php b/tests/src/MyJsonSerializable.php index 6d9760a..e0fb63c 100644 --- a/tests/src/MyJsonSerializable.php +++ b/tests/src/MyJsonSerializable.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); diff --git a/tests/src/MyObject.php b/tests/src/MyObject.php index 767c4b6..569b130 100644 --- a/tests/src/MyObject.php +++ b/tests/src/MyObject.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -15,18 +12,12 @@ final class MyObject { - /** - * @var bool - */ - public $foo = true; + public bool $foo = true; - /** - * @var float - */ - private $bar = .3; + private float $bar = .3; // @phpstan-ignore-line /** * @var int[] */ - private $baz = [1, 2, 3]; + private array $baz = [1, 2, 3]; // @phpstan-ignore-line } diff --git a/tests/src/MyStringable.php b/tests/src/MyStringable.php index 9e74eb6..8344f53 100644 --- a/tests/src/MyStringable.php +++ b/tests/src/MyStringable.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); diff --git a/tests/src/MyTraversable.php b/tests/src/MyTraversable.php index 205ff45..3ff285a 100644 --- a/tests/src/MyTraversable.php +++ b/tests/src/MyTraversable.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -17,6 +14,9 @@ use IteratorAggregate; use Traversable; +/** + * @implements IteratorAggregate + */ final class MyTraversable implements IteratorAggregate { public function getIterator(): Traversable diff --git a/tests/unit/Quoters/CodeQuoterTest.php b/tests/unit/Quoters/CodeQuoterTest.php index d6b5e3a..92609af 100644 --- a/tests/unit/Quoters/CodeQuoterTest.php +++ b/tests/unit/Quoters/CodeQuoterTest.php @@ -2,30 +2,23 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); -namespace Respect\Stringifier\Test\Quoters; +namespace Respect\Stringifier\Test\Unit\Quoters; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Respect\Stringifier\Quoters\CodeQuoter; -/** - * @covers \Respect\Stringifier\Quoters\CodeQuoter - * - * @author Henrique Moody - */ +#[CoversClass(CodeQuoter::class)] final class CodeQuoterTest extends TestCase { - /** - * @test - */ + #[Test] public function shouldQuoteStringWhenInZeroDepth(): void { $quoter = new CodeQuoter(); @@ -36,9 +29,7 @@ public function shouldQuoteStringWhenInZeroDepth(): void self::assertSame($expectedValue, $actualValue); } - /** - * @test - */ + #[Test] public function shouldNotQuoteStringDepthIsBiggerThanZero(): void { $quoter = new CodeQuoter(); diff --git a/tests/unit/Stringifiers/ArrayStringifierTest.php b/tests/unit/Stringifiers/ArrayStringifierTest.php index 1366d79..1f06db1 100644 --- a/tests/unit/Stringifiers/ArrayStringifierTest.php +++ b/tests/unit/Stringifiers/ArrayStringifierTest.php @@ -2,28 +2,27 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); -namespace Respect\Stringifier\Test\Stringifiers; +namespace Respect\Stringifier\Test\Unit\Stringifiers; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifier; use Respect\Stringifier\Stringifiers\ArrayStringifier; + use function is_array; +#[CoversClass(ArrayStringifier::class)] final class ArrayStringifierTest extends TestCase { - /** - * @test - */ + #[Test] public function shouldNotConvertToStringWhenRawValueIsNotAnArray(): void { $raw = false; @@ -44,9 +43,7 @@ public function shouldNotConvertToStringWhenRawValueIsNotAnArray(): void self::assertNull($arrayStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldReturnPlaceHolderWhenDepthIsEqualsToMaximumDepth(): void { $raw = [1, 2, 3]; @@ -70,9 +67,7 @@ public function shouldReturnPlaceHolderWhenDepthIsEqualsToMaximumDepth(): void self::assertSame($expected, $arrayStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldReturnPlaceHolderWhenDepthIsBiggerThanMaximumDepth(): void { $raw = [1, 2, 3]; @@ -96,9 +91,7 @@ public function shouldReturnPlaceHolderWhenDepthIsBiggerThanMaximumDepth(): void self::assertSame($expected, $arrayStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldReturnPlaceHolderWhenRawValueIsAnEmptyArray(): void { $raw = []; @@ -123,9 +116,7 @@ public function shouldReturnPlaceHolderWhenRawValueIsAnEmptyArray(): void self::assertSame($expected, $arrayStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldReturnPlaceHolderWhenRawValueIsAnEmptyArrayEvenThenReachedTheMaximumDepth(): void { $raw = []; @@ -150,9 +141,7 @@ public function shouldReturnPlaceHolderWhenRawValueIsAnEmptyArrayEvenThenReached self::assertSame($expected, $arrayStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldConvertToStringWhenRawValueIsAnArray(): void { $raw = [1, 2, 3]; @@ -180,9 +169,7 @@ public function shouldConvertToStringWhenRawValueIsAnArray(): void self::assertSame($expected, $arrayStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldConvertToStringWhenRawValueIsNested(): void { $raw = [1, [2, 3], 4, 5, [6]]; @@ -214,9 +201,7 @@ public function shouldConvertToStringWhenRawValueIsNested(): void self::assertSame($expected, $arrayStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldConvertToStringWhenKeysAreNotSequential(): void { $raw = [1, 2, 3 => 3]; @@ -244,9 +229,7 @@ public function shouldConvertToStringWhenKeysAreNotSequential(): void self::assertSame($expected, $arrayStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldConvertToStringWhenKeysAreNotInteger(): void { $raw = ['foo' => 1, 'bar' => 2]; @@ -274,9 +257,7 @@ public function shouldConvertToStringWhenKeysAreNotInteger(): void self::assertSame($expected, $arrayStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldUsePlaceholderWhenLimitOfItemsIsReached(): void { $itemsLimit = 5; diff --git a/tests/unit/Stringifiers/BoolStringifierTest.php b/tests/unit/Stringifiers/BoolStringifierTest.php index d33a8b1..bd7d830 100644 --- a/tests/unit/Stringifiers/BoolStringifierTest.php +++ b/tests/unit/Stringifiers/BoolStringifierTest.php @@ -2,31 +2,24 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); -namespace Respect\Stringifier\Test\Stringifiers; +namespace Respect\Stringifier\Test\Unit\Stringifiers; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifiers\BoolStringifier; -/** - * @covers \Respect\Stringifier\Stringifiers\BoolStringifier - * - * @author Henrique Moody - */ +#[CoversClass(BoolStringifier::class)] final class BoolStringifierTest extends TestCase { - /** - * @test - */ + #[Test] public function shouldNotConvertToStringWhenRawValueIsNotBoolean(): void { $raw = 1; @@ -42,9 +35,7 @@ public function shouldNotConvertToStringWhenRawValueIsNotBoolean(): void self::assertNull($boolStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldConvertToStringWhenRawValueIsTrue(): void { $raw = true; @@ -64,9 +55,7 @@ public function shouldConvertToStringWhenRawValueIsTrue(): void self::assertSame($expected, $boolStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldConvertToStringWhenRawValueIsFalse(): void { $raw = false; diff --git a/tests/unit/Stringifiers/ClusterStringifierTest.php b/tests/unit/Stringifiers/ClusterStringifierTest.php index b0d9358..cc28a4a 100644 --- a/tests/unit/Stringifiers/ClusterStringifierTest.php +++ b/tests/unit/Stringifiers/ClusterStringifierTest.php @@ -2,32 +2,25 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); -namespace Respect\Stringifier\Test\Stringifiers; +namespace Respect\Stringifier\Test\Unit\Stringifiers; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Respect\Stringifier\Stringifier; use Respect\Stringifier\Stringifiers\ClusterStringifier; use stdClass; -/** - * @covers \Respect\Stringifier\Stringifiers\ClusterStringifier - * - * @author Henrique Moody - */ +#[CoversClass(ClusterStringifier::class)] final class ClusterStringifierTest extends TestCase { - /** - * @test - */ + #[Test] public function shouldReturnNullWhenNoStringifiersAreDefined(): void { $raw = new stdClass(); @@ -38,9 +31,7 @@ public function shouldReturnNullWhenNoStringifiersAreDefined(): void self::assertNull($clusterStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldUseAllStringifierToStringifyUntilNonFails(): void { $raw = new stdClass(); @@ -72,9 +63,7 @@ public function shouldUseAllStringifierToStringifyUntilNonFails(): void self::assertSame($expected, $clusterStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldReturnNullWhenAllStringifierCannotConvertToString(): void { $raw = new stdClass(); @@ -106,9 +95,7 @@ public function shouldReturnNullWhenAllStringifierCannotConvertToString(): void self::assertNull($clusterStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldCreateDefaultClusterStringifier(): void { self::assertInstanceOf(ClusterStringifier::class, ClusterStringifier::createDefault()); diff --git a/tests/unit/Stringifiers/DateTimeStringifierTest.php b/tests/unit/Stringifiers/DateTimeStringifierTest.php index f90316c..70a5752 100644 --- a/tests/unit/Stringifiers/DateTimeStringifierTest.php +++ b/tests/unit/Stringifiers/DateTimeStringifierTest.php @@ -2,11 +2,8 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); @@ -16,21 +13,18 @@ use DateTime; use DateTimeImmutable; use DateTimeInterface; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifier; use Respect\Stringifier\Stringifiers\DateTimeStringifier; -/** - * @covers \Respect\Stringifier\Stringifiers\DateTimeStringifier - * - * @author Henrique Moody - */ +#[CoversClass(DateTimeStringifier::class)] final class DateTimeStringifierTest extends TestCase { - /** - * @test - */ + #[Test] public function shouldNotConvertWhenNotInstanceOfDateTimeInterface(): void { $stringifierMock = $this->createMock(Stringifier::class); @@ -48,11 +42,8 @@ public function shouldNotConvertWhenNotInstanceOfDateTimeInterface(): void self::assertNull($dateTimeStringifier->stringify('NotDateTimeInterface', 0)); } - /** - * @test - * - * @dataProvider validValuesProvider - */ + #[Test] + #[DataProvider('validValuesProvider')] public function shouldConvertDateTimeInterfaceToString( DateTimeInterface $raw, string $format, @@ -84,9 +75,9 @@ public function shouldConvertDateTimeInterfaceToString( /** * @return mixed[][] */ - public function validValuesProvider(): array + public static function validValuesProvider(): array { - $dateTime = DateTime::createFromFormat('Y-m-d\TH:i:sP', '2017-12-31T23:59:59+00:00'); + $dateTime = new DateTime('2017-12-31T23:59:59+00:00'); $dateTimeImmutable = DateTimeImmutable::createFromMutable($dateTime); return [ diff --git a/tests/unit/Stringifiers/InfiniteStringifierTest.php b/tests/unit/Stringifiers/InfiniteStringifierTest.php index 5ead265..20e22b3 100644 --- a/tests/unit/Stringifiers/InfiniteStringifierTest.php +++ b/tests/unit/Stringifiers/InfiniteStringifierTest.php @@ -2,32 +2,26 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); -namespace Respect\Stringifier\Test\Stringifiers; +namespace Respect\Stringifier\Test\Unit\Stringifiers; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifiers\InfiniteStringifier; + use const INF; -/** - * @covers \Respect\Stringifier\Stringifiers\InfiniteStringifier - * - * @author Henrique Moody - */ +#[CoversClass(InfiniteStringifier::class)] final class InfiniteStringifierTest extends TestCase { - /** - * @test - */ + #[Test] public function shouldNotConvertToStringWhenRawValueIsNotFloat(): void { $raw = 1; @@ -43,9 +37,7 @@ public function shouldNotConvertToStringWhenRawValueIsNotFloat(): void self::assertNull($infiniteStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldNotConvertToStringWhenRawValueIsFiniteFloat(): void { $raw = 1.0; @@ -61,9 +53,7 @@ public function shouldNotConvertToStringWhenRawValueIsFiniteFloat(): void self::assertNull($infiniteStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldConvertToStringWhenRawValueIsInfinitePositive(): void { $raw = INF; @@ -83,9 +73,7 @@ public function shouldConvertToStringWhenRawValueIsInfinitePositive(): void self::assertSame($expected, $infiniteStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldConvertToStringWhenRawValueIsInfiniteNegative(): void { $raw = -1 * INF; diff --git a/tests/unit/Stringifiers/JsonParsableStringifierTest.php b/tests/unit/Stringifiers/JsonParsableStringifierTest.php index b4c635a..ed05830 100644 --- a/tests/unit/Stringifiers/JsonParsableStringifierTest.php +++ b/tests/unit/Stringifiers/JsonParsableStringifierTest.php @@ -2,31 +2,25 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); -namespace Respect\Stringifier\Test\Stringifiers; +namespace Respect\Stringifier\Test\Unit\Stringifiers; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Respect\Stringifier\Stringifiers\JsonParsableStringifier; + use function tmpfile; -/** - * @covers \Respect\Stringifier\Stringifiers\JsonParsableStringifier - * - * @author Henrique Moody - */ +#[CoversClass(JsonParsableStringifier::class)] final class JsonParsableStringifierTest extends TestCase { - /** - * @test - */ + #[Test] public function shouldReturnNullWhenNotPossibleToConvertToParsableJsonString(): void { $raw = tmpfile(); @@ -37,9 +31,7 @@ public function shouldReturnNullWhenNotPossibleToConvertToParsableJsonString(): self::assertNull($jsonSerializableStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldConvertRawValueToParsableJsonString(): void { $raw = 'É uma \' " string'; diff --git a/tests/unit/Stringifiers/JsonSerializableStringifierTest.php b/tests/unit/Stringifiers/JsonSerializableStringifierTest.php index 6c4ad54..45411d3 100644 --- a/tests/unit/Stringifiers/JsonSerializableStringifierTest.php +++ b/tests/unit/Stringifiers/JsonSerializableStringifierTest.php @@ -2,17 +2,16 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); -namespace Respect\Stringifier\Test\Stringifiers; +namespace Respect\Stringifier\Test\Unit\Stringifiers; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifier; @@ -20,16 +19,10 @@ use Respect\Stringifier\Test\MyJsonSerializable; use stdClass; -/** - * @covers \Respect\Stringifier\Stringifiers\JsonSerializableStringifier - * - * @author Henrique Moody - */ +#[CoversClass(JsonSerializableStringifier::class)] final class JsonSerializableStringifierTest extends TestCase { - /** - * @test - */ + #[Test] public function shouldConvertToStringWhenRawValueIsJsonSerializableObject(): void { $raw = new MyJsonSerializable(); @@ -37,7 +30,7 @@ public function shouldConvertToStringWhenRawValueIsJsonSerializableObject(): voi $stringifiedData = '-stringified-'; - $expectedValue = '[json-serializable] ('.MyJsonSerializable::class.': '.$stringifiedData.')'; + $expectedValue = '[json-serializable] (' . MyJsonSerializable::class . ': ' . $stringifiedData . ')'; $stringifierMock = $this->createMock(Stringifier::class); $stringifierMock @@ -58,9 +51,7 @@ public function shouldConvertToStringWhenRawValueIsJsonSerializableObject(): voi self::assertSame($expectedValue, $jsonSerializableStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldNotConvertToStringWhenRawValueIsNotTraversable(): void { $raw = new stdClass(); diff --git a/tests/unit/Stringifiers/NanStringifierTest.php b/tests/unit/Stringifiers/NanStringifierTest.php index 4dfc83b..dd68f12 100644 --- a/tests/unit/Stringifiers/NanStringifierTest.php +++ b/tests/unit/Stringifiers/NanStringifierTest.php @@ -2,32 +2,26 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); -namespace Respect\Stringifier\Test\Stringifiers; +namespace Respect\Stringifier\Test\Unit\Stringifiers; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifiers\NanStringifier; + use function acos; -/** - * @covers \Respect\Stringifier\Stringifiers\NanStringifier - * - * @author Henrique Moody - */ +#[CoversClass(NanStringifier::class)] final class NanStringifierTest extends TestCase { - /** - * @test - */ + #[Test] public function shouldNotConvertToStringWhenRawValueIsNotFloat(): void { $raw = 'string'; @@ -43,9 +37,7 @@ public function shouldNotConvertToStringWhenRawValueIsNotFloat(): void self::assertNull($nanStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldNotConvertToStringWhenRawValueIsNumber(): void { $raw = 1.00000000002; @@ -61,9 +53,7 @@ public function shouldNotConvertToStringWhenRawValueIsNumber(): void self::assertNull($nanStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldConvertToStringWhenRawValueIsNotNumber(): void { $raw = acos(8); diff --git a/tests/unit/Stringifiers/NullStringifierTest.php b/tests/unit/Stringifiers/NullStringifierTest.php index 5919b15..b79e834 100644 --- a/tests/unit/Stringifiers/NullStringifierTest.php +++ b/tests/unit/Stringifiers/NullStringifierTest.php @@ -2,31 +2,24 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); -namespace Respect\Stringifier\Test\Stringifiers; +namespace Respect\Stringifier\Test\Unit\Stringifiers; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifiers\NullStringifier; -/** - * @covers \Respect\Stringifier\Stringifiers\NullStringifier - * - * @author Henrique Moody - */ +#[CoversClass(NullStringifier::class)] final class NullStringifierTest extends TestCase { - /** - * @test - */ + #[Test] public function shouldNotConvertToStringWhenRawValueIsNotNull(): void { $raw = 1; @@ -42,9 +35,7 @@ public function shouldNotConvertToStringWhenRawValueIsNotNull(): void self::assertNull($nullStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldConvertToStringWhenRawValueIsNull(): void { $raw = null; diff --git a/tests/unit/Stringifiers/ObjectStringifierTest.php b/tests/unit/Stringifiers/ObjectStringifierTest.php index 7147a41..2de9252 100644 --- a/tests/unit/Stringifiers/ObjectStringifierTest.php +++ b/tests/unit/Stringifiers/ObjectStringifierTest.php @@ -2,32 +2,25 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); -namespace Respect\Stringifier\Test\Stringifiers; +namespace Respect\Stringifier\Test\Unit\Stringifiers; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifier; use Respect\Stringifier\Stringifiers\ObjectStringifier; -/** - * @covers \Respect\Stringifier\Stringifiers\ObjectStringifier - * - * @author Henrique Moody - */ +#[CoversClass(ObjectStringifier::class)] final class ObjectStringifierTest extends TestCase { - /** - * @test - */ + #[Test] public function shouldConvertToStringWhenRawValueIsAnObject(): void { $data = ['foo' => 1, 'bar' => false]; @@ -37,7 +30,7 @@ public function shouldConvertToStringWhenRawValueIsAnObject(): void $stringifiedData = '-stringified-'; - $expectedValue = '[object] (stdClass: '.$stringifiedData.')'; + $expectedValue = '[object] (stdClass: ' . $stringifiedData . ')'; $stringifierMock = $this->createMock(Stringifier::class); $stringifierMock @@ -58,9 +51,7 @@ public function shouldConvertToStringWhenRawValueIsAnObject(): void self::assertSame($expectedValue, $objectStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldNotConvertToStringWhenRawValueIsNotTraversable(): void { $raw = true; diff --git a/tests/unit/Stringifiers/ResourceStringifierTest.php b/tests/unit/Stringifiers/ResourceStringifierTest.php index d5c9d53..5a1954f 100644 --- a/tests/unit/Stringifiers/ResourceStringifierTest.php +++ b/tests/unit/Stringifiers/ResourceStringifierTest.php @@ -2,32 +2,26 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); -namespace Respect\Stringifier\Test\Stringifiers; +namespace Respect\Stringifier\Test\Unit\Stringifiers; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifiers\ResourceStringifier; + use function tmpfile; -/** - * @covers \Respect\Stringifier\Stringifiers\ResourceStringifier - * - * @author Henrique Moody - */ +#[CoversClass(ResourceStringifier::class)] final class ResourceStringifierTest extends TestCase { - /** - * @test - */ + #[Test] public function shouldNotConvertToStringWhenRawValueIsNotResource(): void { $raw = true; @@ -43,9 +37,7 @@ public function shouldNotConvertToStringWhenRawValueIsNotResource(): void self::assertNull($resourceStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldConvertToStringWhenRawValueIsNotResource(): void { $raw = tmpfile(); diff --git a/tests/unit/Stringifiers/StringableObjectStringifierTest.php b/tests/unit/Stringifiers/StringableObjectStringifierTest.php index 2722533..a1d50fe 100644 --- a/tests/unit/Stringifiers/StringableObjectStringifierTest.php +++ b/tests/unit/Stringifiers/StringableObjectStringifierTest.php @@ -2,33 +2,26 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); -namespace Respect\Stringifier\Test\Stringifiers; +namespace Respect\Stringifier\Test\Unit\Stringifiers; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Respect\Stringifier\Stringifier; use Respect\Stringifier\Stringifiers\StringableObjectStringifier; use Respect\Stringifier\Test\MyStringable; use stdClass; -/** - * @covers \Respect\Stringifier\Stringifiers\StringableObjectStringifier - * - * @author Henrique Moody - */ +#[CoversClass(StringableObjectStringifier::class)] final class StringableObjectStringifierTest extends TestCase { - /** - * @test - */ + #[Test] public function shouldNotConvertToStringWhenValueIsNotAnObject(): void { $raw = 'not-an-object'; @@ -44,9 +37,7 @@ public function shouldNotConvertToStringWhenValueIsNotAnObject(): void self::assertNull($stringableObjectStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldNotConvertToStringWhenValueIsNonStringableObject(): void { $raw = new stdClass(); @@ -62,9 +53,7 @@ public function shouldNotConvertToStringWhenValueIsNonStringableObject(): void self::assertNull($stringableObjectStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldConvertToStringWhenValueIsAnStringableObject(): void { $raw = new MyStringable(); diff --git a/tests/unit/Stringifiers/ThrowableStringifierTest.php b/tests/unit/Stringifiers/ThrowableStringifierTest.php index 37a3dad..7a94fcb 100644 --- a/tests/unit/Stringifiers/ThrowableStringifierTest.php +++ b/tests/unit/Stringifiers/ThrowableStringifierTest.php @@ -2,20 +2,20 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); -namespace Respect\Stringifier\Test\Stringifiers; +namespace Respect\Stringifier\Test\Unit\Stringifiers; use Error; use ErrorException; use Exception; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifier; @@ -23,21 +23,14 @@ use stdClass; use Throwable; use TypeError; -use function get_class; + use function sprintf; -/** - * @covers \Respect\Stringifier\Stringifiers\ThrowableStringifier - * - * @author Henrique Moody - */ +#[CoversClass(ThrowableStringifier::class)] final class ThrowableStringifierTest extends TestCase { - /** - * @test - * - * @dataProvider validValuesProvider - */ + #[Test] + #[DataProvider('validValuesProvider')] public function shouldConvertThrowableToString(Throwable $raw, int $line): void { $depth = 1; @@ -46,14 +39,14 @@ public function shouldConvertThrowableToString(Throwable $raw, int $line): void $expectedValue = sprintf( '[throwable] (%s: %s)', - get_class($raw), + $raw::class, $stringifiedData ); $expectedData = [ 'message' => $raw->getMessage(), 'code' => $raw->getCode(), - 'file' => 'tests/unit/Stringifiers/ThrowableStringifierTest.php:'.$line, + 'file' => 'tests/unit/Stringifiers/ThrowableStringifierTest.php:' . $line, ]; $stringifierMock = $this->createMock(Stringifier::class); @@ -75,22 +68,7 @@ public function shouldConvertThrowableToString(Throwable $raw, int $line): void self::assertSame($expectedValue, $throwableStringifier->stringify($raw, $depth)); } - /** - * @return mixed[][] - */ - public function validValuesProvider(): array - { - return [ - [new Exception('Message for Exception', 0), __LINE__], - [new ErrorException('Message for ErrorException', 102), __LINE__], - [new Error('Message for Error', 78), __LINE__], - [new TypeError('Message for TypeError', 1009), __LINE__], - ]; - } - - /** - * @test - */ + #[Test] public function shouldReturnNullWhenNotInstanceOfThrowable(): void { $stringifierMock = $this->createMock(Stringifier::class); @@ -107,4 +85,17 @@ public function shouldReturnNullWhenNotInstanceOfThrowable(): void self::assertNull($throwableStringifier->stringify(new stdClass(), 0)); } + + /** + * @return mixed[][] + */ + public static function validValuesProvider(): array + { + return [ + [new Exception('Message for Exception', 0), __LINE__], + [new ErrorException('Message for ErrorException', 102), __LINE__], + [new Error('Message for Error', 78), __LINE__], + [new TypeError('Message for TypeError', 1009), __LINE__], + ]; + } } diff --git a/tests/unit/Stringifiers/TraversableStringifierTest.php b/tests/unit/Stringifiers/TraversableStringifierTest.php index 66c37be..3740daf 100644 --- a/tests/unit/Stringifiers/TraversableStringifierTest.php +++ b/tests/unit/Stringifiers/TraversableStringifierTest.php @@ -2,33 +2,26 @@ /* * This file is part of Respect/Stringifier. - * - * (c) Henrique Moody - * - * For the full copyright and license information, please view the "LICENSE.md" - * file that was distributed with this source code. + * Copyright (c) Henrique Moody + * SPDX-License-Identifier: MIT */ declare(strict_types=1); -namespace Respect\Stringifier\Test\Stringifiers; +namespace Respect\Stringifier\Test\Unit\Stringifiers; use ArrayIterator; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\Test; use PHPUnit\Framework\TestCase; use Respect\Stringifier\Quoter; use Respect\Stringifier\Stringifier; use Respect\Stringifier\Stringifiers\TraversableStringifier; -/** - * @covers \Respect\Stringifier\Stringifiers\TraversableStringifier - * - * @author Henrique Moody - */ +#[CoversClass(TraversableStringifier::class)] final class TraversableStringifierTest extends TestCase { - /** - * @test - */ + #[Test] public function shouldConvertToStringWhenValueIsTraversable(): void { $raw = new ArrayIterator([1, 2, 3]); @@ -36,7 +29,7 @@ public function shouldConvertToStringWhenValueIsTraversable(): void $stringifiedData = '-stringified-'; - $expectedValue = '[traversable] (ArrayIterator: '.$stringifiedData.')'; + $expectedValue = '[traversable] (ArrayIterator: ' . $stringifiedData . ')'; $stringifierMock = $this->createMock(Stringifier::class); $stringifierMock @@ -57,9 +50,7 @@ public function shouldConvertToStringWhenValueIsTraversable(): void self::assertSame($expectedValue, $traversableStringifier->stringify($raw, $depth)); } - /** - * @test - */ + #[Test] public function shouldNotConvertToStringWhenRawValueIsNotTraversable(): void { $raw = [1, 2, 3, 4];