diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..9722a98 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,18 @@ +; This file is for unifying the coding style for different editors and IDEs. +; More information at https://editorconfig.org + +root = true + +[*] +charset = utf-8 +indent_size = 4 +indent_style = space +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true + +[*.md] +trim_trailing_whitespace = false + +[*.{yml,yaml}] +indent_size = 2 diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..7282a7c --- /dev/null +++ b/.gitattributes @@ -0,0 +1,18 @@ +# Path-based git attributes +# https://www.kernel.org/pub/software/scm/git/docs/gitattributes.html + +# Ignore all test and documentation with "export-ignore". +/.ddev export-ignore +/.editorconfig export-ignore +/.gitattributes export-ignore +/.github export-ignore +/.gitignore export-ignore +/.scrutinizer.yml export-ignore +/PULL_REQUEST_TEMPLATE.md export-ignore +/ISSUE_TEMPLATE.md export-ignore +/phpstan.types.neon.dist export-ignore +/phpunit.xml export-ignore +/phpunit.xml.dist export-ignore +/tests export-ignore +/types export-ignore +/docs export-ignore diff --git a/.github/workflows/codestyle.yml b/.github/workflows/codestyle.yml deleted file mode 100644 index b673c01..0000000 --- a/.github/workflows/codestyle.yml +++ /dev/null @@ -1,16 +0,0 @@ -name: Check code style - -on: [ push, pull_request ] - -jobs: - php-style: - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v4 - - - name: Check style - uses: docker://oskarstark/php-cs-fixer-ga - with: - args: -v --dry-run --using-cache=no diff --git a/.github/workflows/fix-php-code-style-issues.yml b/.github/workflows/fix-php-code-style-issues.yml new file mode 100644 index 0000000..a8f4837 --- /dev/null +++ b/.github/workflows/fix-php-code-style-issues.yml @@ -0,0 +1,28 @@ +name: Fix PHP code style issues + +on: + push: + paths: + - '**.php' + +permissions: + contents: write + +jobs: + php-code-styling: + runs-on: ubuntu-latest + timeout-minutes: 5 + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + ref: ${{ github.head_ref }} + + - name: Fix PHP code style issues + uses: aglipanci/laravel-pint-action@2.5 + + - name: Commit changes + uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: "style: fix styling" diff --git a/.github/workflows/static-analysis.yml b/.github/workflows/static-analysis.yml new file mode 100644 index 0000000..ddf1aaa --- /dev/null +++ b/.github/workflows/static-analysis.yml @@ -0,0 +1,26 @@ +name: Run static analysis + +on: + - push + - pull_request + +jobs: + types: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: 8.3 + tools: composer:v2 + coverage: none + + - name: Install dependencies + run: composer update --prefer-stable --prefer-dist --no-interaction --no-progress + + - name: Execute type checking + run: vendor/bin/phpstan --configuration="phpstan.types.neon.dist" --no-progress diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index 8091b06..c3e59fb 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -1,75 +1,81 @@ -name: Run tests - -on: [ push, pull_request ] - -jobs: - php-tests: - runs-on: ubuntu-latest - - strategy: - fail-fast: false - matrix: - php: [ 7.4, '8.0', 8.1, 8.2, 8.3 ] - laravel: [ 8.*, 9.*, 10.*, 11.* ] - stability: [ prefer-lowest, prefer-stable ] - exclude: - - laravel: 8.* - php: 8.2 - - laravel: 8.* - php: 8.3 - - laravel: 9.* - php: 7.4 - - laravel: 9.* - php: 8.3 - - laravel: 10.* - php: 7.4 - - laravel: 10.* - php: '8.0' - - laravel: 11.* - php: 7.4 - - laravel: 11.* - php: '8.0' - - laravel: 11.* - php: 8.1 - - name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - - steps: - - name: Checkout code - uses: actions/checkout@v4 - with: - # Fetch 10 commits or Scrutinizer will throw - fetch-depth: 10 - - - name: Setup PHP - uses: shivammathur/setup-php@v2 - with: - php-version: ${{ matrix.php }} - coverage: xdebug - tools: composer:v2 - - - name: Get composer cache directory - id: composer-cache - run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT - - - name: Cache composer dependencies - uses: actions/cache@v4 - with: - path: ${{ steps.composer-cache.outputs.dir }} - key: php-${{ matrix.php }}-laravel-${{ matrix.laravel }}-composer-${{ matrix.stability }}-${{ hashFiles('**/composer.json') }} - restore-keys: php-${{ matrix.php }}-laravel-${{ matrix.laravel }}-composer-${{ matrix.stability }}- - - - name: Install dependencies - run: | - composer require "illuminate/collections:${{ matrix.laravel }}" "illuminate/contracts:${{ matrix.laravel }}" --no-interaction --no-update - composer update --${{ matrix.stability }} --prefer-dist --no-interaction - - - name: Execute tests - run: XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover - - - name: Upload Scrutinizer coverage - uses: sudo-bot/action-scrutinizer@latest - # Do not run this step on forked versions of the main repository (example: contributor forks) - if: matrix.laravel == '9.*' && github.repository == 'swisnl/json-api-client' - with: - cli-args: "--format=php-clover coverage.clover --revision=${{ github.event.pull_request.head.sha || github.sha }}" +name: Run tests + +on: + - push + - pull_request + +jobs: + php-tests: + runs-on: ubuntu-latest + + strategy: + fail-fast: false + matrix: + php: ['8.0', '8.1', '8.2', '8.3', '8.4'] + laravel: ['8.*', '9.*', '10.*', '11.*', '12.*'] + stability: [prefer-lowest, prefer-stable] + exclude: + - laravel: 8.* + php: '8.2' + - laravel: 8.* + php: '8.3' + - laravel: 8.* + php: '8.4' + - laravel: 9.* + php: '8.3' + - laravel: 9.* + php: '8.4' + - laravel: 10.* + php: '8.0' + - laravel: 10.* + php: '8.4' + - laravel: 11.* + php: '8.0' + - laravel: 11.* + php: '8.1' + - laravel: 12.* + php: '8.0' + - laravel: 12.* + php: '8.1' + + name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} + + steps: + - name: Checkout code + uses: actions/checkout@v4 + with: + # Fetch 10 commits or Scrutinizer will throw + fetch-depth: 10 + + - name: Setup PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ matrix.php }} + coverage: xdebug + tools: composer:v2 + + - name: Get composer cache directory + id: composer-cache + run: echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT + + - name: Cache composer dependencies + uses: actions/cache@v4 + with: + path: ${{ steps.composer-cache.outputs.dir }} + key: php-${{ matrix.php }}-laravel-${{ matrix.laravel }}-composer-${{ matrix.stability }}-${{ hashFiles('**/composer.json') }} + restore-keys: php-${{ matrix.php }}-laravel-${{ matrix.laravel }}-composer-${{ matrix.stability }}- + + - name: Install dependencies + run: | + composer require "illuminate/collections:${{ matrix.laravel }}" "illuminate/contracts:${{ matrix.laravel }}" --no-interaction --no-update + composer update --${{ matrix.stability }} --prefer-dist --no-interaction + + - name: Execute tests + run: XDEBUG_MODE=coverage vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover + + - name: Upload Scrutinizer coverage + uses: sudo-bot/action-scrutinizer@latest + # Do not run this step on forked versions of the main repository (example: contributor forks) + if: "matrix.laravel == '9.*' && github.repository == 'swisnl/json-api-client'" + with: + cli-args: --format=php-clover coverage.clover --revision=${{ github.event.pull_request.head.sha || github.sha }} diff --git a/.gitignore b/.gitignore index 5c113a1..65bab2d 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,5 @@ composer.phar composer.lock /vendor/ /.idea/ -/.php-cs-fixer.php -/.php-cs-fixer.cache /.phpunit.result.cache +/.phpunit.cache/test-results diff --git a/.php-cs-fixer.dist.php b/.php-cs-fixer.dist.php deleted file mode 100644 index e8ce3e5..0000000 --- a/.php-cs-fixer.dist.php +++ /dev/null @@ -1,25 +0,0 @@ -in([ - __DIR__.'/src', - __DIR__.'/tests' - ]) -; - -return (new PhpCsFixer\Config()) - ->setFinder($finder) - ->setRiskyAllowed(true) - ->setRules([ - '@Symfony' => true, - 'array_syntax' => ['syntax' => 'short'], - 'linebreak_after_opening_tag' => true, - 'ordered_imports' => true, - 'phpdoc_add_missing_param_annotation' => true, - 'phpdoc_order' => true, - 'yoda_style' => false, - 'no_superfluous_phpdoc_tags' => false, - 'fully_qualified_strict_types' => false, - 'declare_strict_types' => true, - ]) -; diff --git a/CHANGELOG.md b/CHANGELOG.md index 7bf842f..128b833 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,35 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] -* Nothing +* Nothing. + +## [2.6.1] - 2025-10-08 + +### Fixed + +* Properly handle responses with an unknown size [#111](https://github.com/swisnl/json-api-client/issues/111). + +## [2.6.0] - 2025-05-28 + +### Added + +* Added support for generics [#108](https://github.com/swisnl/json-api-client/issues/108). + +## [2.5.0] - 2025-02-24 + +### Added + +* Added support for Laravel 12 [#104](https://github.com/swisnl/json-api-client/issues/104). + +### Removed + +* Dropped PHP <8.0 support. + +## [2.4.0] - 2024-05-15 + +### Added + +* Added the ability to send headers through repositories [#103](https://github.com/swisnl/json-api-client/issues/103). ## [2.3.2] - 2024-03-05 diff --git a/README.md b/README.md index e2e8661..b2e0db0 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,7 @@ [![PHP from Packagist](https://img.shields.io/packagist/php-v/swisnl/json-api-client.svg)](https://packagist.org/packages/swisnl/json-api-client) [![Latest Version on Packagist](https://img.shields.io/packagist/v/swisnl/json-api-client.svg)](https://packagist.org/packages/swisnl/json-api-client) -[![Software License](https://img.shields.io/packagist/l/swisnl/json-api-client.svg)](LICENSE) +[![Software License](https://img.shields.io/packagist/l/swisnl/json-api-client.svg)](LICENSE.md) [![Buy us a tree](https://img.shields.io/badge/Treeware-%F0%9F%8C%B3-lightgreen.svg)](https://plant.treeware.earth/swisnl/json-api-client) [![Build Status](https://img.shields.io/github/checks-status/swisnl/json-api-client/master?label=tests)](https://github.com/swisnl/json-api-client/actions/workflows/tests.yml) [![Scrutinizer Coverage](https://img.shields.io/scrutinizer/coverage/g/swisnl/json-api-client.svg)](https://scrutinizer-ci.com/g/swisnl/json-api-client/?branch=master) @@ -477,6 +477,52 @@ N.B. This example uses our [swisnl/php-http-fixture-client](https://github.com/s This package allows you to easily mock requests with static fixtures. Definitely worth a try! +## Using generics + +This package provides support for generic types in repositories and relationships, +so your IDE can provide type hinting and auto-completion for the items you are working with. + +This is achieved by using [generics in PHPDoc annotations](https://phpstan.org/blog/generics-in-php-using-phpdocs). + +### Repositories + +```php + +/** @extends \Swis\JsonApi\Client\Repository */ +class BlogRepository extends \Swis\JsonApi\Client\Repository {...} + +``` + +Now, when you use the `BlogRepository` class, your IDE understands the correct return types for the `all()`, `find()` and `save()` methods. + +### Relationships + +You can also use generics in your relationships to specify the type of the related item. +Just use the `OneRelationInterface` or `ManyRelationInterface` interfaces in your relation method and specify the type of the related item: + +```php + +/** @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface */ +public function author(): OneRelationInterface +{ + return $this->hasOne(AuthorItem::class); +} + +``` + +This way, when accessing the `$blog->author()->getData()`, your IDE will understand that it returns an `AuthorItem` instance. + +The same can be achieved for ManyRelations (`HasMany`, `MorphToMany`): + +```php + +/** @return \Swis\JsonApi\Client\Interfaces\ManyRelationInterface */ +public function comments(): ManyRelationInterface +{ + return $this->hasMany(CommentItem::class); +} + +``` ## Advanced usage @@ -507,7 +553,7 @@ If you discover any security related issues, please email security@swis.nl inste ## License -The MIT License (MIT). Please see [License File](LICENSE) for more information. +The MIT License (MIT). Please see [License File](LICENSE.md) for more information. This package is [Treeware](https://treeware.earth). If you use it in production, then we ask that you [**buy the world a tree**](https://plant.treeware.earth/swisnl/json-api-client) to thank us for our work. By contributing to the Treeware forest you’ll be creating employment for local families and restoring wildlife habitats. diff --git a/composer.json b/composer.json index 973469f..af34252 100644 --- a/composer.json +++ b/composer.json @@ -3,10 +3,10 @@ "type": "library", "description": "A PHP package for mapping remote JSON:API resources to Eloquent like models and collections.", "require": { - "php": "^7.4|^8.0", + "php": "^8.0", "ext-json": "*", - "illuminate/collections": "^8.0|^9.0|^10.0|^11.0", - "illuminate/contracts": "^8.0|^9.0|^10.0|^11.0", + "illuminate/collections": "^8.0|^9.0|^10.0|^11.0|^12.0", + "illuminate/contracts": "^8.0|^9.0|^10.0|^11.0|^12.0", "php-http/discovery": "^1.9", "psr/http-client": "^1.0", "psr/http-client-implementation": "^1.0", @@ -15,10 +15,11 @@ "psr/http-message": "^1.0|^2.0" }, "require-dev": { - "friendsofphp/php-cs-fixer": "^3.0", "guzzlehttp/guzzle": "^7.3", - "phpunit/phpunit": "^9.5", - "php-http/mock-client": "^1.2" + "laravel/pint": "^1.5", + "php-http/mock-client": "^1.2", + "phpstan/phpstan": "^2.1", + "phpunit/phpunit": "^9.5" }, "suggest": { "swisnl/php-http-fixture-client": "Allows for easily mocking API calls with fixtures in your tests" @@ -29,17 +30,13 @@ } }, "autoload-dev": { - "psr-4" : { - "Swis\\JsonApi\\Client\\Tests\\" : "tests/", - "Swis\\JsonApi\\Client\\Tests\\Mocks\\" : "tests/_mocks/" + "psr-4": { + "Swis\\JsonApi\\Client\\Tests\\": "tests/", + "Swis\\JsonApi\\Client\\Tests\\Mocks\\": "tests/_mocks/" } }, "license": "MIT", "authors": [ - { - "name": "Hendrik Nijmeijer", - "email": "hendrik@swis.nl" - }, { "name": "Jasper Zonneveld", "email": "jasper@swis.nl", @@ -52,18 +49,20 @@ } ], "scripts": { - "test": "phpunit", - "check-style": "php-cs-fixer fix --dry-run -v", - "fix-style": "php-cs-fixer fix" + "check-style": "pint --test", + "check-types": "phpstan --configuration=\"phpstan.types.neon.dist\"", + "fix-style": "pint", + "test": "phpunit" }, "extra": { "branch-alias": { - "dev-master": "2.3.x-dev" + "dev-master": "2.5.x-dev" } }, "minimum-stability": "dev", "prefer-stable": true, "config": { + "sort-packages": true, "allow-plugins": { "php-http/discovery": true } diff --git a/phpstan.types.neon.dist b/phpstan.types.neon.dist new file mode 100644 index 0000000..e0db3e6 --- /dev/null +++ b/phpstan.types.neon.dist @@ -0,0 +1,5 @@ +parameters: + level: max + paths: + - tests\_mocks + - types diff --git a/src/Actions/Create.php b/src/Actions/Create.php index aa2599c..09a8886 100644 --- a/src/Actions/Create.php +++ b/src/Actions/Create.php @@ -6,19 +6,20 @@ use Swis\JsonApi\Client\Interfaces\ItemInterface; +/** + * @template TItem of \Swis\JsonApi\Client\Interfaces\ItemInterface + */ trait Create { /** - * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item - * @param array $parameters - * - * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface + * @return \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface */ - public function create(ItemInterface $item, array $parameters = []) + public function create(ItemInterface $item, array $parameters = [], array $headers = []) { return $this->getClient()->post( $this->getEndpoint().'?'.http_build_query($parameters), - $this->documentFactory->make($item) + $this->documentFactory->make($item), + $headers, ); } } diff --git a/src/Actions/Delete.php b/src/Actions/Delete.php index b55908d..612f96e 100644 --- a/src/Actions/Delete.php +++ b/src/Actions/Delete.php @@ -7,13 +7,10 @@ trait Delete { /** - * @param string $id - * @param array $parameters - * * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface */ - public function delete(string $id, array $parameters = []) + public function delete(string $id, array $parameters = [], array $headers = []) { - return $this->getClient()->delete($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters)); + return $this->getClient()->delete($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters), $headers); } } diff --git a/src/Actions/FetchMany.php b/src/Actions/FetchMany.php index 2af56e8..2a1e72b 100644 --- a/src/Actions/FetchMany.php +++ b/src/Actions/FetchMany.php @@ -4,15 +4,16 @@ namespace Swis\JsonApi\Client\Actions; +/** + * @template TItem of \Swis\JsonApi\Client\Interfaces\ItemInterface + */ trait FetchMany { /** - * @param array $parameters - * - * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface + * @return \Swis\JsonApi\Client\Interfaces\CollectionDocumentInterface */ - public function all(array $parameters = []) + public function all(array $parameters = [], array $headers = []) { - return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters)); + return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters), $headers); } } diff --git a/src/Actions/FetchOne.php b/src/Actions/FetchOne.php index 05b673f..c6e8d2e 100644 --- a/src/Actions/FetchOne.php +++ b/src/Actions/FetchOne.php @@ -4,16 +4,16 @@ namespace Swis\JsonApi\Client\Actions; +/** + * @template TItem of \Swis\JsonApi\Client\Interfaces\ItemInterface + */ trait FetchOne { /** - * @param string $id - * @param array $parameters - * - * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface + * @return \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface */ - public function find(string $id, array $parameters = []) + public function find(string $id, array $parameters = [], array $headers = []) { - return $this->getClient()->get($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters)); + return $this->getClient()->get($this->getEndpoint().'/'.urlencode($id).'?'.http_build_query($parameters), $headers); } } diff --git a/src/Actions/Save.php b/src/Actions/Save.php index 00d14c8..c9e0c4d 100644 --- a/src/Actions/Save.php +++ b/src/Actions/Save.php @@ -6,23 +6,26 @@ use Swis\JsonApi\Client\Interfaces\ItemInterface; +/** + * @template TItem of \Swis\JsonApi\Client\Interfaces\ItemInterface + */ trait Save { + /** @use Create */ use Create { create as protected saveNew; } + + /** @use Update */ use Update { update as protected saveExisting; } /** - * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item - * @param array $parameters - * - * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface + * @return \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface */ - public function save(ItemInterface $item, array $parameters = []) + public function save(ItemInterface $item, array $parameters = [], array $headers = []) { if ($item->isNew()) { - return $this->saveNew($item, $parameters); + return $this->saveNew($item, $parameters, $headers); } - return $this->saveExisting($item, $parameters); + return $this->saveExisting($item, $parameters, $headers); } } diff --git a/src/Actions/TakeOne.php b/src/Actions/TakeOne.php index 171eac8..4ec0b5e 100644 --- a/src/Actions/TakeOne.php +++ b/src/Actions/TakeOne.php @@ -4,15 +4,16 @@ namespace Swis\JsonApi\Client\Actions; +/** + * @template TItem of \Swis\JsonApi\Client\Interfaces\ItemInterface + */ trait TakeOne { /** - * @param array $parameters - * - * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface + * @return \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface */ - public function take(array $parameters = []) + public function take(array $parameters = [], array $headers = []) { - return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters)); + return $this->getClient()->get($this->getEndpoint().'?'.http_build_query($parameters), $headers); } } diff --git a/src/Actions/Update.php b/src/Actions/Update.php index 0aa775b..f9e7d13 100644 --- a/src/Actions/Update.php +++ b/src/Actions/Update.php @@ -6,19 +6,20 @@ use Swis\JsonApi\Client\Interfaces\ItemInterface; +/** + * @template TItem of \Swis\JsonApi\Client\Interfaces\ItemInterface + */ trait Update { /** - * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item - * @param array $parameters - * - * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface + * @return \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface */ - public function update(ItemInterface $item, array $parameters = []) + public function update(ItemInterface $item, array $parameters = [], array $headers = []) { return $this->getClient()->patch( $this->getEndpoint().'/'.urlencode($item->getId()).'?'.http_build_query($parameters), - $this->documentFactory->make($item) + $this->documentFactory->make($item), + $headers, ); } } diff --git a/src/BaseRepository.php b/src/BaseRepository.php index d676677..1559c81 100644 --- a/src/BaseRepository.php +++ b/src/BaseRepository.php @@ -23,10 +23,6 @@ abstract class BaseRepository */ protected $endpoint = ''; - /** - * @param \Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client - * @param \Swis\JsonApi\Client\DocumentFactory $documentFactory - */ public function __construct(DocumentClientInterface $client, DocumentFactory $documentFactory) { $this->client = $client; diff --git a/src/Client.php b/src/Client.php index 5e50f7f..6d1f08e 100644 --- a/src/Client.php +++ b/src/Client.php @@ -32,52 +32,35 @@ class Client implements ClientInterface public function __construct( ?HttpClientInterface $client = null, ?RequestFactoryInterface $requestFactory = null, - ?StreamFactoryInterface $streamFactory = null + ?StreamFactoryInterface $streamFactory = null, ) { $this->client = $client ?: Psr18ClientDiscovery::find(); $this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory(); $this->streamFactory = $streamFactory ?: Psr17FactoryDiscovery::findStreamFactory(); } - /** - * @return string - */ public function getBaseUri(): string { return $this->baseUri; } - /** - * @param string $baseUri - */ public function setBaseUri(string $baseUri): void { $this->baseUri = $baseUri; } - /** - * @return array - */ public function getDefaultHeaders(): array { return $this->defaultHeaders; } - /** - * @param array $defaultHeaders - */ public function setDefaultHeaders(array $defaultHeaders): void { $this->defaultHeaders = $defaultHeaders; } /** - * @param string $endpoint - * @param array $headers - * * @throws \Psr\Http\Client\ClientExceptionInterface - * - * @return \Psr\Http\Message\ResponseInterface */ public function get(string $endpoint, array $headers = []): ResponseInterface { @@ -85,13 +68,9 @@ public function get(string $endpoint, array $headers = []): ResponseInterface } /** - * @param string $endpoint - * @param string|resource|\Psr\Http\Message\StreamInterface|null $body - * @param array $headers + * @param string|resource|\Psr\Http\Message\StreamInterface|null $body * * @throws \Psr\Http\Client\ClientExceptionInterface - * - * @return \Psr\Http\Message\ResponseInterface */ public function post(string $endpoint, $body, array $headers = []): ResponseInterface { @@ -99,13 +78,9 @@ public function post(string $endpoint, $body, array $headers = []): ResponseInte } /** - * @param string $endpoint - * @param string|resource|\Psr\Http\Message\StreamInterface|null $body - * @param array $headers + * @param string|resource|\Psr\Http\Message\StreamInterface|null $body * * @throws \Psr\Http\Client\ClientExceptionInterface - * - * @return \Psr\Http\Message\ResponseInterface */ public function patch(string $endpoint, $body, array $headers = []): ResponseInterface { @@ -113,12 +88,7 @@ public function patch(string $endpoint, $body, array $headers = []): ResponseInt } /** - * @param string $endpoint - * @param array $headers - * * @throws \Psr\Http\Client\ClientExceptionInterface - * - * @return \Psr\Http\Message\ResponseInterface */ public function delete(string $endpoint, array $headers = []): ResponseInterface { @@ -126,14 +96,9 @@ public function delete(string $endpoint, array $headers = []): ResponseInterface } /** - * @param string $method - * @param string $endpoint - * @param string|resource|\Psr\Http\Message\StreamInterface|null $body - * @param array $headers + * @param string|resource|\Psr\Http\Message\StreamInterface|null $body * * @throws \Psr\Http\Client\ClientExceptionInterface - * - * @return \Psr\Http\Message\ResponseInterface */ public function request(string $method, string $endpoint, $body = null, array $headers = []): ResponseInterface { @@ -141,12 +106,7 @@ public function request(string $method, string $endpoint, $body = null, array $h } /** - * @param string $method - * @param string $endpoint - * @param string|resource|\Psr\Http\Message\StreamInterface|null $body - * @param array $headers - * - * @return \Psr\Http\Message\RequestInterface + * @param string|resource|\Psr\Http\Message\StreamInterface|null $body */ protected function buildRequest(string $method, string $endpoint, $body = null, array $headers = []): RequestInterface { @@ -156,7 +116,7 @@ protected function buildRequest(string $method, string $endpoint, $body = null, if (is_resource($body)) { $body = $this->streamFactory->createStreamFromResource($body); } - if (!($body instanceof StreamInterface)) { + if (! ($body instanceof StreamInterface)) { $body = $this->streamFactory->createStream($body); } @@ -170,11 +130,6 @@ protected function buildRequest(string $method, string $endpoint, $body = null, return $request; } - /** - * @param string $endpoint - * - * @return string - */ protected function getEndpoint(string $endpoint): string { if (strpos($endpoint, 'http://') === 0 || strpos($endpoint, 'https://') === 0) { @@ -184,11 +139,6 @@ protected function getEndpoint(string $endpoint): string return $this->baseUri.$endpoint; } - /** - * @param array $headers - * - * @return array - */ protected function mergeHeaders(array $headers): array { return array_merge($this->defaultHeaders, $headers); diff --git a/src/Collection.php b/src/Collection.php index 6eedff5..c93675f 100644 --- a/src/Collection.php +++ b/src/Collection.php @@ -6,12 +6,16 @@ use Swis\JsonApi\Client\Interfaces\DataInterface; +/** + * @template TKey of int + * @template TValue of \Swis\JsonApi\Client\Interfaces\ItemInterface + * + * @extends \Illuminate\Support\Collection + */ class Collection extends \Illuminate\Support\Collection implements DataInterface { /** * Get the collection of items as a plain json api array. - * - * @return array */ public function toJsonApiArray(): array { diff --git a/src/CollectionDocument.php b/src/CollectionDocument.php index c51e994..10307d1 100644 --- a/src/CollectionDocument.php +++ b/src/CollectionDocument.php @@ -6,6 +6,4 @@ use Swis\JsonApi\Client\Interfaces\CollectionDocumentInterface; -class CollectionDocument extends Document implements CollectionDocumentInterface -{ -} +class CollectionDocument extends Document implements CollectionDocumentInterface {} diff --git a/src/Concerns/GuardsAttributes.php b/src/Concerns/GuardsAttributes.php index 59040f2..ad0b2f2 100644 --- a/src/Concerns/GuardsAttributes.php +++ b/src/Concerns/GuardsAttributes.php @@ -40,7 +40,6 @@ public function getFillable() /** * Set the fillable attributes for the model. * - * @param array $fillable * * @return $this */ @@ -54,7 +53,6 @@ public function fillable(array $fillable) /** * Merge new fillable attributes with existing fillable attributes on the model. * - * @param array $fillable * * @return $this */ @@ -78,8 +76,7 @@ public function getGuarded() /** * Set the guarded attributes for the model. * - * @param string[] $guarded - * + * @param string[] $guarded * @return $this */ public function guard(array $guarded) @@ -92,7 +89,6 @@ public function guard(array $guarded) /** * Merge new guarded attributes with existing guarded attributes on the model. * - * @param array $guarded * * @return $this */ @@ -106,8 +102,7 @@ public function mergeGuarded(array $guarded) /** * Disable all mass assignable restrictions. * - * @param bool $state - * + * @param bool $state * @return void */ public static function unguard($state = true) @@ -138,7 +133,6 @@ public static function isUnguarded() /** * Run the given callable while being unguarded. * - * @param callable $callback * * @return mixed */ @@ -160,8 +154,7 @@ public static function unguarded(callable $callback) /** * Determine if the given attribute may be mass assigned. * - * @param string $key - * + * @param string $key * @return bool */ public function isFillable($key) @@ -190,8 +183,7 @@ public function isFillable($key) /** * Determine if the given key is guarded. * - * @param string $key - * + * @param string $key * @return bool */ public function isGuarded($key) @@ -201,7 +193,7 @@ public function isGuarded($key) } return $this->getGuarded() == ['*'] - || !empty(preg_grep('/^'.preg_quote($key, '/').'$/i', $this->getGuarded())); + || ! empty(preg_grep('/^'.preg_quote($key, '/').'$/i', $this->getGuarded())); } /** @@ -217,13 +209,12 @@ public function totallyGuarded() /** * Get the fillable attributes of a given array. * - * @param array $attributes * * @return array */ protected function fillableFromArray(array $attributes) { - if (count($this->getFillable()) > 0 && !static::$unguarded) { + if (count($this->getFillable()) > 0 && ! static::$unguarded) { return array_intersect_key($attributes, array_flip($this->getFillable())); } diff --git a/src/Concerns/HasAttributes.php b/src/Concerns/HasAttributes.php index 88a652c..82a56f8 100644 --- a/src/Concerns/HasAttributes.php +++ b/src/Concerns/HasAttributes.php @@ -62,10 +62,6 @@ abstract public function getHidden(); /** * Get the relationship data (included). - * - * @param string $name - * - * @return \Swis\JsonApi\Client\Interfaces\DataInterface|null */ abstract public function getRelationValue(string $name): ?DataInterface; @@ -102,8 +98,6 @@ public function attributesToArray() /** * Add the mutated attributes to the attributes array. * - * @param array $attributes - * @param array $mutatedAttributes * * @return array */ @@ -113,7 +107,7 @@ protected function addMutatedAttributesToArray(array $attributes, array $mutated // We want to spin through all the mutated attributes for this model and call // the mutator for the attribute. We cache off every mutated attributes so // we don't have to constantly check on attributes that actually change. - if (!array_key_exists($key, $attributes)) { + if (! array_key_exists($key, $attributes)) { continue; } @@ -131,15 +125,13 @@ protected function addMutatedAttributesToArray(array $attributes, array $mutated /** * Add the casted attributes to the attributes array. * - * @param array $attributes - * @param array $mutatedAttributes * * @return array */ protected function addCastAttributesToArray(array $attributes, array $mutatedAttributes) { foreach ($this->getCasts() as $key => $value) { - if (!array_key_exists($key, $attributes) + if (! array_key_exists($key, $attributes) || in_array($key, $mutatedAttributes)) { continue; } @@ -176,7 +168,7 @@ protected function getArrayableAttributes() */ protected function getArrayableAppends() { - if (!count($this->appends)) { + if (! count($this->appends)) { return []; } @@ -188,7 +180,6 @@ protected function getArrayableAppends() /** * Get an attribute array of all arrayable values. * - * @param array $values * * @return array */ @@ -208,13 +199,12 @@ protected function getArrayableItems(array $values) /** * Get an attribute from the model. * - * @param string $key - * + * @param string $key * @return mixed */ public function getAttribute($key) { - if (!$key) { + if (! $key) { return; } @@ -240,8 +230,7 @@ public function getAttribute($key) /** * Get a plain attribute (not a relationship). * - * @param string $key - * + * @param string $key * @return mixed */ public function getAttributeValue($key) @@ -252,7 +241,6 @@ public function getAttributeValue($key) /** * Get an attribute from the $attributes array. * - * @param string $key * * @return mixed */ @@ -264,8 +252,7 @@ protected function getAttributeFromArray(string $key) /** * Determine if a get mutator exists for an attribute. * - * @param string $key - * + * @param string $key * @return bool */ public function hasGetMutator($key) @@ -276,9 +263,8 @@ public function hasGetMutator($key) /** * Get the value of an attribute using its mutator. * - * @param string $key - * @param mixed $value - * + * @param string $key + * @param mixed $value * @return mixed */ protected function mutateAttribute($key, $value) @@ -289,9 +275,8 @@ protected function mutateAttribute($key, $value) /** * Get the value of an attribute using its mutator for array conversion. * - * @param string $key - * @param mixed $value - * + * @param string $key + * @param mixed $value * @return mixed */ protected function mutateAttributeForArray($key, $value) @@ -304,7 +289,6 @@ protected function mutateAttributeForArray($key, $value) /** * Merge new casts with existing casts on the model. * - * @param array $casts * * @return $this */ @@ -318,9 +302,8 @@ public function mergeCasts(array $casts) /** * Cast an attribute to a native PHP type. * - * @param string $key - * @param mixed $value - * + * @param string $key + * @param mixed $value * @return mixed */ protected function castAttribute($key, $value) @@ -357,8 +340,7 @@ protected function castAttribute($key, $value) /** * Get the type of cast for a model attribute. * - * @param string $key - * + * @param string $key * @return string */ protected function getCastType($key) @@ -369,9 +351,8 @@ protected function getCastType($key) /** * Set a given attribute on the model. * - * @param string $key - * @param mixed $value - * + * @param string $key + * @param mixed $value * @return mixed */ public function setAttribute($key, $value) @@ -383,7 +364,7 @@ public function setAttribute($key, $value) return $this->setMutatedAttributeValue($key, $value); } - if (!is_null($value) && $this->isJsonCastable($key)) { + if (! is_null($value) && $this->isJsonCastable($key)) { $value = $this->asJson($value); } @@ -395,8 +376,7 @@ public function setAttribute($key, $value) /** * Determine if a set mutator exists for an attribute. * - * @param string $key - * + * @param string $key * @return bool */ public function hasSetMutator($key) @@ -407,9 +387,8 @@ public function hasSetMutator($key) /** * Set the value of an attribute using its mutator. * - * @param string $key - * @param mixed $value - * + * @param string $key + * @param mixed $value * @return mixed */ protected function setMutatedAttributeValue($key, $value) @@ -420,8 +399,7 @@ protected function setMutatedAttributeValue($key, $value) /** * Encode the given value as JSON. * - * @param mixed $value - * + * @param mixed $value * @return string */ protected function asJson($value) @@ -432,21 +410,18 @@ protected function asJson($value) /** * Decode the given JSON back into an array or object. * - * @param string $value - * @param bool $asObject - * + * @param bool $asObject * @return mixed */ public function fromJson(string $value, $asObject = false) { - return json_decode($value, !$asObject, 512, JSON_THROW_ON_ERROR); + return json_decode($value, ! $asObject, 512, JSON_THROW_ON_ERROR); } /** * Decode the given float. * - * @param mixed $value - * + * @param mixed $value * @return mixed */ public function fromFloat($value) @@ -466,9 +441,8 @@ public function fromFloat($value) /** * Determine whether an attribute should be cast to a native type. * - * @param string $key - * @param array|string|null $types - * + * @param string $key + * @param array|string|null $types * @return bool */ public function hasCast($key, $types = null) @@ -493,8 +467,7 @@ public function getCasts() /** * Determine whether a value is JSON castable for inbound manipulation. * - * @param string $key - * + * @param string $key * @return bool */ protected function isJsonCastable($key) @@ -515,8 +488,7 @@ public function getAttributes() /** * Get a subset of the model's attributes. * - * @param array|mixed $attributes - * + * @param array|mixed $attributes * @return array */ public function only($attributes) @@ -533,9 +505,8 @@ public function only($attributes) /** * Transform a raw model value using mutators, casts, etc. * - * @param string $key - * @param mixed $value - * + * @param string $key + * @param mixed $value * @return mixed */ protected function transformModelValue($key, $value) @@ -560,7 +531,6 @@ protected function transformModelValue($key, $value) /** * Set the accessors to append to model arrays. * - * @param array $appends * * @return $this */ @@ -574,8 +544,7 @@ public function setAppends(array $appends) /** * Add the accessors to append to model arrays. * - * @param array $attributes - * + * @param array $attributes * @return $this */ public function mergeAppends($attributes) @@ -588,8 +557,7 @@ public function mergeAppends($attributes) /** * Return whether the accessor attribute has been appended. * - * @param string $attribute - * + * @param string $attribute * @return bool */ public function hasAppended($attribute) @@ -606,7 +574,7 @@ public function getMutatedAttributes() { $class = static::class; - if (!isset(static::$mutatorCache[$class])) { + if (! isset(static::$mutatorCache[$class])) { static::cacheMutatedAttributes($class); } @@ -616,8 +584,7 @@ public function getMutatedAttributes() /** * Extract and cache all the mutated attributes of a class. * - * @param string $class - * + * @param string $class * @return void */ public static function cacheMutatedAttributes($class) @@ -637,8 +604,7 @@ public static function cacheMutatedAttributes($class) /** * Get all of the attribute mutator methods. * - * @param mixed $class - * + * @param mixed $class * @return array */ protected static function getMutatorMethods($class) diff --git a/src/Concerns/HasId.php b/src/Concerns/HasId.php index 071ea85..c120ff5 100644 --- a/src/Concerns/HasId.php +++ b/src/Concerns/HasId.php @@ -11,17 +11,12 @@ trait HasId */ protected $id; - /** - * @return string|null - */ public function getId(): ?string { return $this->id; } /** - * @param string|null $id - * * @return static */ public function setId(?string $id) @@ -31,9 +26,6 @@ public function setId(?string $id) return $this; } - /** - * @return bool - */ public function hasId(): bool { return isset($this->id); diff --git a/src/Concerns/HasInitial.php b/src/Concerns/HasInitial.php index 3e51149..0c2fddd 100644 --- a/src/Concerns/HasInitial.php +++ b/src/Concerns/HasInitial.php @@ -12,13 +12,12 @@ trait HasInitial protected $initial = []; /** - * @param string|null $key - * + * @param string|null $key * @return array|mixed */ public function getInitial($key = null) { - if (null === $key) { + if ($key === null) { return $this->initial; } @@ -26,8 +25,6 @@ public function getInitial($key = null) } /** - * @param array $initial - * * @return static */ public function setInitial(array $initial) @@ -38,14 +35,12 @@ public function setInitial(array $initial) } /** - * @param string|null $key - * - * @return bool + * @param string|null $key */ public function hasInitial($key = null): bool { - if (null === $key) { - return !empty($this->initial); + if ($key === null) { + return ! empty($this->initial); } return isset($this->initial[$key]); diff --git a/src/Concerns/HasLinks.php b/src/Concerns/HasLinks.php index 2b24027..84025cb 100644 --- a/src/Concerns/HasLinks.php +++ b/src/Concerns/HasLinks.php @@ -13,17 +13,12 @@ trait HasLinks */ protected $links; - /** - * @return \Swis\JsonApi\Client\Links|null - */ public function getLinks(): ?Links { return $this->links; } /** - * @param \Swis\JsonApi\Client\Links|null $links - * * @return $this */ public function setLinks(?Links $links) diff --git a/src/Concerns/HasMeta.php b/src/Concerns/HasMeta.php index 2e758d9..3be662d 100644 --- a/src/Concerns/HasMeta.php +++ b/src/Concerns/HasMeta.php @@ -13,17 +13,12 @@ trait HasMeta */ protected $meta; - /** - * @return \Swis\JsonApi\Client\Meta|null - */ public function getMeta(): ?Meta { return $this->meta; } /** - * @param \Swis\JsonApi\Client\Meta|null $meta - * * @return $this */ public function setMeta(?Meta $meta) diff --git a/src/Concerns/HasRelations.php b/src/Concerns/HasRelations.php index 2482e9d..33cd137 100644 --- a/src/Concerns/HasRelations.php +++ b/src/Concerns/HasRelations.php @@ -26,17 +26,17 @@ trait HasRelations /** * Create a singular relation to another item. * - * @param string $itemClass - * @param string|null $name + * @template TItem of \Swis\JsonApi\Client\Interfaces\ItemInterface * - * @return \Swis\JsonApi\Client\Relations\HasOneRelation + * @param class-string $itemClass + * @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface */ public function hasOne(string $itemClass, ?string $name = null): OneRelationInterface { $name = $name ?: $this->guessRelationName(); - if (!array_key_exists($name, $this->relations)) { - $this->relations[$name] = $this->newHasOne((new $itemClass())->getType()); + if (! array_key_exists($name, $this->relations)) { + $this->relations[$name] = $this->newHasOne((new $itemClass)->getType()); } return $this->relations[$name]; @@ -50,17 +50,17 @@ protected function newHasOne(string $type): OneRelationInterface /** * Create a plural relation to another item. * - * @param string $itemClass - * @param string|null $name + * @template TItem of \Swis\JsonApi\Client\Interfaces\ItemInterface * - * @return \Swis\JsonApi\Client\Relations\HasManyRelation + * @param class-string $itemClass + * @return \Swis\JsonApi\Client\Interfaces\ManyRelationInterface */ public function hasMany(string $itemClass, ?string $name = null): ManyRelationInterface { $name = $name ?: $this->guessRelationName(); - if (!array_key_exists($name, $this->relations)) { - $this->relations[$name] = $this->newHasMany((new $itemClass())->getType()); + if (! array_key_exists($name, $this->relations)) { + $this->relations[$name] = $this->newHasMany((new $itemClass)->getType()); } return $this->relations[$name]; @@ -74,7 +74,6 @@ protected function newHasMany(string $type): ManyRelationInterface /** * Create a singular relation. * - * @param string|null $name * * @return \Swis\JsonApi\Client\Relations\MorphToRelation */ @@ -82,7 +81,7 @@ public function morphTo(?string $name = null): OneRelationInterface { $name = $name ?: $this->guessRelationName(); - if (!array_key_exists($name, $this->relations)) { + if (! array_key_exists($name, $this->relations)) { $this->relations[$name] = $this->newMorphTo(); } @@ -91,13 +90,12 @@ public function morphTo(?string $name = null): OneRelationInterface protected function newMorphTo(): OneRelationInterface { - return new MorphToRelation(); + return new MorphToRelation; } /** * Create a plural relation. * - * @param string|null $name * * @return \Swis\JsonApi\Client\Relations\MorphToManyRelation */ @@ -105,7 +103,7 @@ public function morphToMany(?string $name = null): ManyRelationInterface { $name = $name ?: $this->guessRelationName(); - if (!array_key_exists($name, $this->relations)) { + if (! array_key_exists($name, $this->relations)) { $this->relations[$name] = $this->newMorphToMany(); } @@ -114,13 +112,11 @@ public function morphToMany(?string $name = null): ManyRelationInterface protected function newMorphToMany(): ManyRelationInterface { - return new MorphToManyRelation(); + return new MorphToManyRelation; } /** * Guess the relationship name. - * - * @return string */ protected function guessRelationName(): string { @@ -138,8 +134,6 @@ public function getRelations(): array } /** - * @param string $name - * * @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface|null */ public function getRelation(string $name) @@ -149,10 +143,6 @@ public function getRelation(string $name) /** * Get the relationship data (included). - * - * @param string $name - * - * @return \Swis\JsonApi\Client\Interfaces\DataInterface|null */ public function getRelationValue(string $name): ?DataInterface { @@ -175,11 +165,7 @@ public function getRelationValue(string $name): ?DataInterface /** * Set the specific relationship on the model. * - * @param string $relation - * @param \Swis\JsonApi\Client\Interfaces\DataInterface|false|null $value - * @param \Swis\JsonApi\Client\Links|null $links - * @param \Swis\JsonApi\Client\Meta|null $meta - * + * @param \Swis\JsonApi\Client\Interfaces\DataInterface|false|null $value * @return static */ public function setRelation(string $relation, $value = false, ?Links $links = null, ?Meta $meta = null) @@ -206,19 +192,12 @@ public function setRelation(string $relation, $value = false, ?Links $links = nu return $this; } - /** - * @param string $name - * - * @return bool - */ public function hasRelation(string $name): bool { return array_key_exists($name, $this->relations); } /** - * @param string $name - * * @return static */ public function unsetRelation(string $name) diff --git a/src/Concerns/HasType.php b/src/Concerns/HasType.php index b634202..b9c1cdc 100644 --- a/src/Concerns/HasType.php +++ b/src/Concerns/HasType.php @@ -11,17 +11,12 @@ trait HasType */ protected $type; - /** - * @return string - */ public function getType(): string { return $this->type; } /** - * @param string $type - * * @return $this */ public function setType(string $type) @@ -31,9 +26,6 @@ public function setType(string $type) return $this; } - /** - * @return bool - */ public function hasType(): bool { return (bool) $this->type; diff --git a/src/Concerns/HidesAttributes.php b/src/Concerns/HidesAttributes.php index deaec54..cd5c4d2 100644 --- a/src/Concerns/HidesAttributes.php +++ b/src/Concerns/HidesAttributes.php @@ -33,7 +33,6 @@ public function getHidden() /** * Set the hidden attributes for the model. * - * @param array $hidden * * @return $this */ @@ -57,7 +56,6 @@ public function getVisible() /** * Set the visible attributes for the model. * - * @param array $visible * * @return $this */ @@ -71,8 +69,7 @@ public function setVisible(array $visible) /** * Make the given, typically hidden, attributes visible. * - * @param array|string|null $attributes - * + * @param array|string|null $attributes * @return $this */ public function makeVisible($attributes) @@ -81,7 +78,7 @@ public function makeVisible($attributes) $this->hidden = array_diff($this->hidden, $attributes); - if (!empty($this->visible)) { + if (! empty($this->visible)) { $this->visible = array_merge($this->visible, $attributes); } @@ -91,8 +88,7 @@ public function makeVisible($attributes) /** * Make the given, typically visible, attributes hidden. * - * @param array|string|null $attributes - * + * @param array|string|null $attributes * @return $this */ public function makeHidden($attributes) diff --git a/src/Document.php b/src/Document.php index 38a47f7..3e793e3 100644 --- a/src/Document.php +++ b/src/Document.php @@ -42,21 +42,16 @@ class Document implements DocumentInterface public function __construct() { - $this->errors = new ErrorCollection(); - $this->included = new Collection(); + $this->errors = new ErrorCollection; + $this->included = new Collection; } - /** - * @return \Psr\Http\Message\ResponseInterface|null - */ public function getResponse(): ?ResponseInterface { return $this->response; } /** - * @param \Psr\Http\Message\ResponseInterface|null $response - * * @return $this */ public function setResponse(?ResponseInterface $response) @@ -66,17 +61,12 @@ public function setResponse(?ResponseInterface $response) return $this; } - /** - * @return \Swis\JsonApi\Client\ErrorCollection - */ public function getErrors(): ErrorCollection { return $this->errors; } /** - * @param \Swis\JsonApi\Client\ErrorCollection $errors - * * @return $this */ public function setErrors(ErrorCollection $errors) @@ -86,33 +76,22 @@ public function setErrors(ErrorCollection $errors) return $this; } - /** - * @return bool - */ public function hasErrors(): bool { - return !$this->errors->isEmpty(); + return ! $this->errors->isEmpty(); } - /** - * @return bool - */ public function isSuccess(): bool { return $this->errors->isEmpty(); } - /** - * @return \Swis\JsonApi\Client\Collection - */ public function getIncluded(): Collection { return $this->included; } /** - * @param \Swis\JsonApi\Client\Collection $included - * * @return $this */ public function setIncluded(Collection $included) @@ -122,17 +101,12 @@ public function setIncluded(Collection $included) return $this; } - /** - * @return \Swis\JsonApi\Client\Jsonapi|null - */ public function getJsonapi(): ?Jsonapi { return $this->jsonapi; } /** - * @param \Swis\JsonApi\Client\Jsonapi|null $jsonapi - * * @return $this */ public function setJsonapi(?Jsonapi $jsonapi) @@ -151,8 +125,6 @@ public function getData() } /** - * @param \Swis\JsonApi\Client\Interfaces\DataInterface $data - * * @return $this */ public function setData(DataInterface $data) @@ -162,9 +134,6 @@ public function setData(DataInterface $data) return $this; } - /** - * @return array - */ public function toArray(): array { $document = []; diff --git a/src/DocumentClient.php b/src/DocumentClient.php index 824ef74..6d3fa9b 100644 --- a/src/DocumentClient.php +++ b/src/DocumentClient.php @@ -27,9 +27,6 @@ public function __construct(ClientInterface $client, ResponseParserInterface $pa } /** - * @param \Swis\JsonApi\Client\Interfaces\TypeMapperInterface|null $typeMapper - * @param \Psr\Http\Client\ClientInterface|null $client - * * @return static */ public static function create(?TypeMapperInterface $typeMapper = null, ?HttpClientInterface $client = null): self @@ -37,93 +34,46 @@ public static function create(?TypeMapperInterface $typeMapper = null, ?HttpClie return new static(new Client($client), ResponseParser::create($typeMapper)); } - /** - * @return string - */ public function getBaseUri(): string { return $this->client->getBaseUri(); } - /** - * @param string $baseUri - */ public function setBaseUri(string $baseUri): void { $this->client->setBaseUri($baseUri); } - /** - * @param string $endpoint - * @param array $headers - * - * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface - */ public function get(string $endpoint, array $headers = []): DocumentInterface { return $this->parseResponse($this->client->get($endpoint, $headers)); } - /** - * @param string $endpoint - * @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $body - * @param array $headers - * - * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface - */ public function post(string $endpoint, ItemDocumentInterface $body, array $headers = []): DocumentInterface { return $this->parseResponse($this->client->post($endpoint, $this->prepareBody($body), $headers)); } - /** - * @param string $endpoint - * @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $body - * @param array $headers - * - * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface - */ public function patch(string $endpoint, ItemDocumentInterface $body, array $headers = []): DocumentInterface { return $this->parseResponse($this->client->patch($endpoint, $this->prepareBody($body), $headers)); } - /** - * @param string $endpoint - * @param array $headers - * - * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface - */ public function delete(string $endpoint, array $headers = []): DocumentInterface { return $this->parseResponse($this->client->delete($endpoint, $headers)); } - /** - * @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $body - * - * @return string - */ protected function prepareBody(ItemDocumentInterface $body): string { return $this->sanitizeJson(json_encode($body, JSON_THROW_ON_ERROR)); } - /** - * @param string $json - * - * @return string - */ protected function sanitizeJson(string $json): string { return str_replace('\r\n', '\\n', $json); } - /** - * @param \Psr\Http\Message\ResponseInterface $response - * - * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface - */ protected function parseResponse(ResponseInterface $response): DocumentInterface { return $this->parser->parse($response); diff --git a/src/DocumentFactory.php b/src/DocumentFactory.php index e94434d..064d6fa 100644 --- a/src/DocumentFactory.php +++ b/src/DocumentFactory.php @@ -12,16 +12,14 @@ class DocumentFactory { /** - * @param \Swis\JsonApi\Client\Interfaces\DataInterface $data - * * @return \Swis\JsonApi\Client\ItemDocument|\Swis\JsonApi\Client\CollectionDocument */ public function make(DataInterface $data): DocumentInterface { if ($data instanceof ItemInterface) { - $document = new ItemDocument(); + $document = new ItemDocument; } elseif ($data instanceof Collection) { - $document = new CollectionDocument(); + $document = new CollectionDocument; } else { throw new UnsupportedDataException(sprintf('%s is not supported as input', get_class($data))); } @@ -29,11 +27,6 @@ public function make(DataInterface $data): DocumentInterface return $document->setData($data)->setIncluded($this->getIncluded($data)); } - /** - * @param \Swis\JsonApi\Client\Interfaces\DataInterface $data - * - * @return \Swis\JsonApi\Client\Collection - */ private function getIncluded(DataInterface $data): Collection { return Collection::wrap($data) @@ -42,18 +35,13 @@ private function getIncluded(DataInterface $data): Collection ->values(); } - /** - * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item - * - * @return \Swis\JsonApi\Client\Collection - */ private function getIncludedFromItem(ItemInterface $item): Collection { return Collection::make($item->getRelations()) ->reject( static function ($relationship) { /* @var \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface $relationship */ - return $relationship->shouldOmitIncluded() || !$relationship->hasIncluded(); + return $relationship->shouldOmitIncluded() || ! $relationship->hasIncluded(); } ) ->flatMap( @@ -66,11 +54,6 @@ static function ($relationship) { ->filter(fn (ItemInterface $item) => $this->itemCanBeIncluded($item)); } - /** - * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item - * - * @return bool - */ private function itemCanBeIncluded(ItemInterface $item): bool { return $item->hasType() diff --git a/src/Error.php b/src/Error.php index d6d2912..1cb54ad 100644 --- a/src/Error.php +++ b/src/Error.php @@ -42,16 +42,6 @@ class Error */ protected $source; - /** - * @param string|null $id - * @param \Swis\JsonApi\Client\Links|null $links - * @param string|null $status - * @param string|null $code - * @param string|null $title - * @param string|null $detail - * @param \Swis\JsonApi\Client\ErrorSource|null $source - * @param \Swis\JsonApi\Client\Meta|null $meta - */ public function __construct( ?string $id = null, ?Links $links = null, @@ -60,7 +50,7 @@ public function __construct( ?string $title = null, ?string $detail = null, ?ErrorSource $source = null, - ?Meta $meta = null + ?Meta $meta = null, ) { $this->id = $id; $this->links = $links; diff --git a/src/ErrorCollection.php b/src/ErrorCollection.php index 6f9529f..86c1bc5 100644 --- a/src/ErrorCollection.php +++ b/src/ErrorCollection.php @@ -4,6 +4,4 @@ namespace Swis\JsonApi\Client; -class ErrorCollection extends \Illuminate\Support\Collection -{ -} +class ErrorCollection extends \Illuminate\Support\Collection {} diff --git a/src/ErrorSource.php b/src/ErrorSource.php index bbcf3e6..7515c10 100644 --- a/src/ErrorSource.php +++ b/src/ErrorSource.php @@ -16,10 +16,6 @@ class ErrorSource */ protected $parameter; - /** - * @param string|null $pointer - * @param string|null $parameter - */ public function __construct(?string $pointer = null, ?string $parameter = null) { $this->pointer = $pointer; diff --git a/src/Exceptions/Exception.php b/src/Exceptions/Exception.php index c4f5a4a..54bc164 100644 --- a/src/Exceptions/Exception.php +++ b/src/Exceptions/Exception.php @@ -4,6 +4,4 @@ namespace Swis\JsonApi\Client\Exceptions; -interface Exception -{ -} +interface Exception {} diff --git a/src/Exceptions/HydrationException.php b/src/Exceptions/HydrationException.php index 525c5e5..b6acb05 100644 --- a/src/Exceptions/HydrationException.php +++ b/src/Exceptions/HydrationException.php @@ -4,6 +4,4 @@ namespace Swis\JsonApi\Client\Exceptions; -class HydrationException extends \InvalidArgumentException implements Exception -{ -} +class HydrationException extends \InvalidArgumentException implements Exception {} diff --git a/src/Exceptions/MassAssignmentException.php b/src/Exceptions/MassAssignmentException.php index 79dde77..751dfe4 100644 --- a/src/Exceptions/MassAssignmentException.php +++ b/src/Exceptions/MassAssignmentException.php @@ -4,6 +4,4 @@ namespace Swis\JsonApi\Client\Exceptions; -class MassAssignmentException extends \RuntimeException -{ -} +class MassAssignmentException extends \RuntimeException {} diff --git a/src/Exceptions/TypeMappingException.php b/src/Exceptions/TypeMappingException.php index c7591d7..c5034db 100644 --- a/src/Exceptions/TypeMappingException.php +++ b/src/Exceptions/TypeMappingException.php @@ -4,6 +4,4 @@ namespace Swis\JsonApi\Client\Exceptions; -class TypeMappingException extends \InvalidArgumentException implements Exception -{ -} +class TypeMappingException extends \InvalidArgumentException implements Exception {} diff --git a/src/Exceptions/UnsupportedDataException.php b/src/Exceptions/UnsupportedDataException.php index d02d2cc..3cc198d 100644 --- a/src/Exceptions/UnsupportedDataException.php +++ b/src/Exceptions/UnsupportedDataException.php @@ -4,6 +4,4 @@ namespace Swis\JsonApi\Client\Exceptions; -class UnsupportedDataException extends \InvalidArgumentException implements Exception -{ -} +class UnsupportedDataException extends \InvalidArgumentException implements Exception {} diff --git a/src/Exceptions/ValidationException.php b/src/Exceptions/ValidationException.php index 0d030dc..af7f578 100644 --- a/src/Exceptions/ValidationException.php +++ b/src/Exceptions/ValidationException.php @@ -4,6 +4,4 @@ namespace Swis\JsonApi\Client\Exceptions; -class ValidationException extends \InvalidArgumentException implements Exception -{ -} +class ValidationException extends \InvalidArgumentException implements Exception {} diff --git a/src/Interfaces/ClientInterface.php b/src/Interfaces/ClientInterface.php index e86727e..781f5b7 100644 --- a/src/Interfaces/ClientInterface.php +++ b/src/Interfaces/ClientInterface.php @@ -8,47 +8,21 @@ interface ClientInterface { - /** - * @param string $endpoint - * @param array $headers - * - * @return \Psr\Http\Message\ResponseInterface - */ public function get(string $endpoint, array $headers = []): ResponseInterface; /** - * @param string $endpoint - * @param string|resource|\Psr\Http\Message\StreamInterface|null $body - * @param array $headers - * - * @return \Psr\Http\Message\ResponseInterface + * @param string|resource|\Psr\Http\Message\StreamInterface|null $body */ public function patch(string $endpoint, $body, array $headers = []): ResponseInterface; /** - * @param string $endpoint - * @param string|resource|\Psr\Http\Message\StreamInterface|null $body - * @param array $headers - * - * @return \Psr\Http\Message\ResponseInterface + * @param string|resource|\Psr\Http\Message\StreamInterface|null $body */ public function post(string $endpoint, $body, array $headers = []): ResponseInterface; - /** - * @param string $endpoint - * @param array $headers - * - * @return \Psr\Http\Message\ResponseInterface - */ public function delete(string $endpoint, array $headers = []): ResponseInterface; - /** - * @return string - */ public function getBaseUri(): string; - /** - * @param string $baseUri - */ public function setBaseUri(string $baseUri); } diff --git a/src/Interfaces/CollectionDocumentInterface.php b/src/Interfaces/CollectionDocumentInterface.php index c8b21d8..a847a12 100644 --- a/src/Interfaces/CollectionDocumentInterface.php +++ b/src/Interfaces/CollectionDocumentInterface.php @@ -4,10 +4,13 @@ namespace Swis\JsonApi\Client\Interfaces; +/** + * @template TItem of \Swis\JsonApi\Client\Interfaces\ItemInterface + */ interface CollectionDocumentInterface extends DocumentInterface { /** - * @return \Swis\JsonApi\Client\Collection + * @return \Swis\JsonApi\Client\Collection */ public function getData(); } diff --git a/src/Interfaces/DataInterface.php b/src/Interfaces/DataInterface.php index bb65ca8..6921b52 100644 --- a/src/Interfaces/DataInterface.php +++ b/src/Interfaces/DataInterface.php @@ -8,8 +8,6 @@ interface DataInterface { /** * Get the data as a plain json api array. - * - * @return array */ public function toJsonApiArray(): array; } diff --git a/src/Interfaces/DocumentClientInterface.php b/src/Interfaces/DocumentClientInterface.php index 6599781..bf8c40e 100644 --- a/src/Interfaces/DocumentClientInterface.php +++ b/src/Interfaces/DocumentClientInterface.php @@ -6,47 +6,15 @@ interface DocumentClientInterface { - /** - * @param string $endpoint - * @param array $headers - * - * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface - */ public function get(string $endpoint, array $headers = []): DocumentInterface; - /** - * @param string $endpoint - * @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $document - * @param array $headers - * - * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface - */ public function patch(string $endpoint, ItemDocumentInterface $document, array $headers = []): DocumentInterface; - /** - * @param string $endpoint - * @param \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface $document - * @param array $headers - * - * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface - */ public function post(string $endpoint, ItemDocumentInterface $document, array $headers = []): DocumentInterface; - /** - * @param string $endpoint - * @param array $headers - * - * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface - */ public function delete(string $endpoint, array $headers = []): DocumentInterface; - /** - * @return string - */ public function getBaseUri(): string; - /** - * @param string $baseUri - */ public function setBaseUri(string $baseUri); } diff --git a/src/Interfaces/DocumentInterface.php b/src/Interfaces/DocumentInterface.php index 4b73cf0..9acdd6c 100644 --- a/src/Interfaces/DocumentInterface.php +++ b/src/Interfaces/DocumentInterface.php @@ -13,14 +13,9 @@ interface DocumentInterface extends \JsonSerializable { - /** - * @return \Psr\Http\Message\ResponseInterface|null - */ public function getResponse(): ?ResponseInterface; /** - * @param \Psr\Http\Message\ResponseInterface|null $response - * * @return $this */ public function setResponse(?ResponseInterface $response); @@ -31,49 +26,29 @@ public function setResponse(?ResponseInterface $response); public function getData(); /** - * @param \Swis\JsonApi\Client\Interfaces\DataInterface $data - * * @return $this */ public function setData(DataInterface $data); - /** - * @return \Swis\JsonApi\Client\ErrorCollection - */ public function getErrors(): ErrorCollection; /** - * @param \Swis\JsonApi\Client\ErrorCollection $errors - * * @return $this */ public function setErrors(ErrorCollection $errors); - /** - * @return bool - */ public function hasErrors(): bool; - /** - * @return \Swis\JsonApi\Client\Meta|null - */ public function getMeta(): ?Meta; /** - * @param \Swis\JsonApi\Client\Meta|null $meta - * * @return $this */ public function setMeta(?Meta $meta); - /** - * @return \Swis\JsonApi\Client\Links|null - */ public function getLinks(): ?Links; /** - * @param \Swis\JsonApi\Client\Links|null $links - * * @return $this */ public function setLinks(?Links $links); @@ -84,26 +59,16 @@ public function setLinks(?Links $links); public function getIncluded(): Collection; /** - * @param \Swis\JsonApi\Client\Collection $included - * * @return $this */ public function setIncluded(Collection $included); - /** - * @return \Swis\JsonApi\Client\Jsonapi|null - */ public function getJsonapi(): ?Jsonapi; /** - * @param \Swis\JsonApi\Client\Jsonapi|null $jsonapi - * * @return $this */ public function setJsonapi(?Jsonapi $jsonapi); - /** - * @return array - */ public function toArray(): array; } diff --git a/src/Interfaces/DocumentParserInterface.php b/src/Interfaces/DocumentParserInterface.php index 146c6ef..4b3620c 100644 --- a/src/Interfaces/DocumentParserInterface.php +++ b/src/Interfaces/DocumentParserInterface.php @@ -6,10 +6,5 @@ interface DocumentParserInterface { - /** - * @param string $json - * - * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface - */ public function parse(string $json): DocumentInterface; } diff --git a/src/Interfaces/ItemDocumentInterface.php b/src/Interfaces/ItemDocumentInterface.php index 54f266e..d34431a 100644 --- a/src/Interfaces/ItemDocumentInterface.php +++ b/src/Interfaces/ItemDocumentInterface.php @@ -4,10 +4,13 @@ namespace Swis\JsonApi\Client\Interfaces; +/** + * @template TItem of \Swis\JsonApi\Client\Interfaces\ItemInterface + */ interface ItemDocumentInterface extends DocumentInterface { /** - * @return \Swis\JsonApi\Client\Interfaces\ItemInterface + * @return TItem */ public function getData(); } diff --git a/src/Interfaces/ItemInterface.php b/src/Interfaces/ItemInterface.php index d6f86ea..238a9d2 100644 --- a/src/Interfaces/ItemInterface.php +++ b/src/Interfaces/ItemInterface.php @@ -9,79 +9,46 @@ interface ItemInterface extends DataInterface { - /** - * @return string|null - */ public function getId(): ?string; - /** - * @return bool - */ public function hasId(): bool; - /** - * @return bool - */ public function isNew(): bool; /** - * @param string|null $id - * * @return static */ public function setId(?string $id); - /** - * @return string - */ public function getType(): string; - /** - * @return bool - */ public function hasType(): bool; /** - * @param string $type - * * @return static */ public function setType(string $type); /** - * @param \Swis\JsonApi\Client\Links|null $links - * * @return $this */ public function setLinks(?Links $links); - /** - * @return \Swis\JsonApi\Client\Links|null - */ public function getLinks(): ?Links; /** - * @param \Swis\JsonApi\Client\Meta|null $meta - * * @return $this */ public function setMeta(?Meta $meta); - /** - * @return \Swis\JsonApi\Client\Meta|null - */ public function getMeta(): ?Meta; /** - * @param array $attributes - * * @return static */ public function fill(array $attributes); /** - * @param array $attributes - * * @return mixed */ public function forceFill(array $attributes); @@ -92,48 +59,24 @@ public function forceFill(array $attributes); public function getAttributes(); /** - * @param $key - * * @return mixed */ public function getAttribute($key); - /** - * @param $key - * @param $value - */ public function setAttribute($key, $value); - /** - * @param $key - * - * @return bool - */ public function hasAttribute($key): bool; - /** - * @return bool - */ public function hasAttributes(): bool; - /** - * @return bool - */ public function hasRelationships(): bool; - /** - * @return array - */ public function getAvailableRelations(): array; /** * Set the specific relationship in the model. * - * @param string $relation - * @param \Swis\JsonApi\Client\Interfaces\DataInterface|false|null $value - * @param \Swis\JsonApi\Client\Links|null $links - * @param \Swis\JsonApi\Client\Meta|null $meta - * + * @param \Swis\JsonApi\Client\Interfaces\DataInterface|false|null $value * @return static */ public function setRelation(string $relation, $value = false, ?Links $links = null, ?Meta $meta = null); diff --git a/src/Interfaces/ManyRelationInterface.php b/src/Interfaces/ManyRelationInterface.php index 92d2c98..ebc34f4 100644 --- a/src/Interfaces/ManyRelationInterface.php +++ b/src/Interfaces/ManyRelationInterface.php @@ -8,41 +8,32 @@ use Swis\JsonApi\Client\Links; use Swis\JsonApi\Client\Meta; +/** + * @template TItem of \Swis\JsonApi\Client\Interfaces\ItemInterface + */ interface ManyRelationInterface { /** - * @param \Swis\JsonApi\Client\Collection|null $data + * @param \Swis\JsonApi\Client\Collection|null $data + * @return static */ public function setData(?Collection $data); /** - * @return \Swis\JsonApi\Client\Collection|null + * @return \Swis\JsonApi\Client\Collection|null */ public function getData(): ?Collection; - /** - * @return bool - */ public function hasData(): bool; - /** - * @param \Swis\JsonApi\Client\Collection $included - */ public function setIncluded(Collection $included); - /** - * @return \Swis\JsonApi\Client\Collection - */ public function getIncluded(): Collection; - /** - * @return bool - */ public function hasIncluded(): bool; /** - * @param \Swis\JsonApi\Client\Collection $included - * + * @param \Swis\JsonApi\Client\Collection $included * @return static */ public function associate(Collection $included); @@ -52,45 +43,22 @@ public function associate(Collection $included); */ public function dissociate(); - /** - * @return \Swis\JsonApi\Client\Collection - */ public function getAssociated(): Collection; - /** - * @return bool - */ public function hasAssociated(): bool; /** - * @param bool $omitIncluded - * * @return static */ public function setOmitIncluded(bool $omitIncluded); - /** - * @return bool - */ public function shouldOmitIncluded(): bool; - /** - * @param \Swis\JsonApi\Client\Links|null $links - */ public function setLinks(?Links $links); - /** - * @return \Swis\JsonApi\Client\Links|null - */ public function getLinks(): ?Links; - /** - * @param \Swis\JsonApi\Client\Meta|null $meta - */ public function setMeta(?Meta $meta); - /** - * @return \Swis\JsonApi\Client\Meta|null - */ public function getMeta(): ?Meta; } diff --git a/src/Interfaces/OneRelationInterface.php b/src/Interfaces/OneRelationInterface.php index fc400a6..414c9b3 100644 --- a/src/Interfaces/OneRelationInterface.php +++ b/src/Interfaces/OneRelationInterface.php @@ -7,41 +7,38 @@ use Swis\JsonApi\Client\Links; use Swis\JsonApi\Client\Meta; +/** + * @template TItem of \Swis\JsonApi\Client\Interfaces\ItemInterface + */ interface OneRelationInterface { /** - * @param \Swis\JsonApi\Client\Interfaces\ItemInterface|null $data + * @param TItem|null $data + * @return static */ public function setData(?ItemInterface $data); /** - * @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null + * @return TItem|null */ public function getData(): ?ItemInterface; - /** - * @return bool - */ public function hasData(): bool; /** - * @param \Swis\JsonApi\Client\Interfaces\ItemInterface|null $included + * @param TItem|null $included */ public function setIncluded(?ItemInterface $included); /** - * @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null + * @return TItem|null */ public function getIncluded(): ?ItemInterface; - /** - * @return bool - */ public function hasIncluded(): bool; /** - * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $included - * + * @param TItem $included * @return static */ public function associate(ItemInterface $included); @@ -52,44 +49,24 @@ public function associate(ItemInterface $included); public function dissociate(); /** - * @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null + * @return TItem|null */ public function getAssociated(): ?ItemInterface; - /** - * @return bool - */ public function hasAssociated(): bool; /** - * @param bool $omitIncluded - * * @return static */ public function setOmitIncluded(bool $omitIncluded); - /** - * @return bool - */ public function shouldOmitIncluded(): bool; - /** - * @param \Swis\JsonApi\Client\Links|null $links - */ public function setLinks(?Links $links); - /** - * @return \Swis\JsonApi\Client\Links|null - */ public function getLinks(): ?Links; - /** - * @param \Swis\JsonApi\Client\Meta|null $meta - */ public function setMeta(?Meta $meta); - /** - * @return \Swis\JsonApi\Client\Meta|null - */ public function getMeta(): ?Meta; } diff --git a/src/Interfaces/RepositoryInterface.php b/src/Interfaces/RepositoryInterface.php index 249d77c..df2de87 100644 --- a/src/Interfaces/RepositoryInterface.php +++ b/src/Interfaces/RepositoryInterface.php @@ -4,30 +4,27 @@ namespace Swis\JsonApi\Client\Interfaces; +/** + * @template TItem of \Swis\JsonApi\Client\Interfaces\ItemInterface + */ interface RepositoryInterface { /** - * @return \Swis\JsonApi\Client\Interfaces\CollectionDocumentInterface + * @return \Swis\JsonApi\Client\Interfaces\CollectionDocumentInterface */ public function all(); /** - * @param string $id - * - * @return \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface + * @return \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface */ public function find(string $id); /** - * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item - * - * @return \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface + * @return \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface */ public function save(ItemInterface $item); /** - * @param string $id - * * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface */ public function delete(string $id); diff --git a/src/Interfaces/ResponseParserInterface.php b/src/Interfaces/ResponseParserInterface.php index 57ffe6e..45337a8 100644 --- a/src/Interfaces/ResponseParserInterface.php +++ b/src/Interfaces/ResponseParserInterface.php @@ -8,10 +8,5 @@ interface ResponseParserInterface { - /** - * @param \Psr\Http\Message\ResponseInterface $response - * - * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface - */ public function parse(ResponseInterface $response): DocumentInterface; } diff --git a/src/Interfaces/TypeMapperInterface.php b/src/Interfaces/TypeMapperInterface.php index d7f4401..d91e92d 100644 --- a/src/Interfaces/TypeMapperInterface.php +++ b/src/Interfaces/TypeMapperInterface.php @@ -10,31 +10,16 @@ interface TypeMapperInterface { /** - * @param string $type - * @param string $class - * * @throws \InvalidArgumentException */ public function setMapping(string $type, string $class): void; - /** - * @param string $type - * - * @return bool - */ public function hasMapping(string $type): bool; - /** - * @param string $type - */ public function removeMapping(string $type): void; /** - * @param string $type - * * @throws \InvalidArgumentException - * - * @return \Swis\JsonApi\Client\Interfaces\ItemInterface */ public function getMapping(string $type): ItemInterface; } diff --git a/src/Interfaces/TypedRelationInterface.php b/src/Interfaces/TypedRelationInterface.php index cfb15a7..8ccfb21 100644 --- a/src/Interfaces/TypedRelationInterface.php +++ b/src/Interfaces/TypedRelationInterface.php @@ -6,14 +6,9 @@ interface TypedRelationInterface { - /** - * @return string - */ public function getType(): string; /** - * @param string $type - * * @return static */ public function setType(string $type); diff --git a/src/InvalidResponseDocument.php b/src/InvalidResponseDocument.php index c6556e7..fe4ce42 100644 --- a/src/InvalidResponseDocument.php +++ b/src/InvalidResponseDocument.php @@ -4,6 +4,4 @@ namespace Swis\JsonApi\Client; -class InvalidResponseDocument extends Document -{ -} +class InvalidResponseDocument extends Document {} diff --git a/src/Item.php b/src/Item.php index b36b5e2..b4f2666 100644 --- a/src/Item.php +++ b/src/Item.php @@ -14,7 +14,7 @@ /** * @property string|null id */ -class Item implements \ArrayAccess, Arrayable, Jsonable, \JsonSerializable, ItemInterface +class Item implements \ArrayAccess, \JsonSerializable, Arrayable, ItemInterface, Jsonable { use Concerns\GuardsAttributes; use Concerns\HasAttributes; @@ -36,7 +36,6 @@ class Item implements \ArrayAccess, Arrayable, Jsonable, \JsonSerializable, Item /** * Create a new Item instance. * - * @param array $attributes * * @return void */ @@ -48,11 +47,10 @@ public function __construct(array $attributes = []) /** * Fill the model with an array of attributes. * - * @param array $attributes - * - * @throws \Swis\JsonApi\Client\Exceptions\MassAssignmentException * * @return $this + * + * @throws \Swis\JsonApi\Client\Exceptions\MassAssignmentException */ public function fill(array $attributes) { @@ -75,7 +73,6 @@ public function fill(array $attributes) /** * Fill the model with an array of attributes. Force mass assignment. * - * @param array $attributes * * @return $this */ @@ -87,7 +84,6 @@ public function forceFill(array $attributes) /** * Create a new instance of the given model. * - * @param array $attributes * * @return static */ @@ -105,14 +101,10 @@ public function newInstance(array $attributes = []) /** * Create a list of models from plain arrays. - * - * @param array $items - * - * @return array */ public static function hydrate(array $items): array { - $instance = new static(); + $instance = new static; return array_map( static fn ($item) => $instance->newInstance($item), @@ -120,9 +112,6 @@ public static function hydrate(array $items): array ); } - /** - * @return array - */ public function toJsonApiArray(): array { $data = [ @@ -134,12 +123,12 @@ public function toJsonApiArray(): array } $attributes = $this->toArray(); - if (!empty($attributes)) { + if (! empty($attributes)) { $data['attributes'] = $attributes; } $relationships = $this->getRelationships(); - if (!empty($relationships)) { + if (! empty($relationships)) { $data['relationships'] = $relationships; } @@ -159,8 +148,7 @@ public function toJsonApiArray(): array /** * Convert the model instance to JSON. * - * @param int $options - * + * @param int $options * @return string */ public function toJson($options = 0) @@ -192,7 +180,6 @@ public function toArray() /** * Clone the model into a new, non-existing instance. * - * @param array|null $except * * @return static */ @@ -203,49 +190,35 @@ public function replicate(?array $except = null) return new static($attributes); } - /** - * @return bool - */ public function isNew(): bool { - return !$this->hasId(); + return ! $this->hasId(); } - /** - * @return bool - */ public function hasAttributes(): bool { - return !empty($this->toArray()); + return ! empty($this->toArray()); } /** - * @param string $key - * - * @return bool + * @param string $key */ public function hasAttribute($key): bool { return array_key_exists($key, $this->attributes); } - /** - * @return array - */ public function getAvailableRelations(): array { return $this->availableRelations; } - /** - * @return array - */ public function getRelationships(): array { $relationships = []; foreach ($this->getRelations() as $name => $relation) { - if (!$relation->hasAssociated()) { + if (! $relation->hasAssociated()) { continue; } @@ -290,12 +263,9 @@ public function getRelationships(): array return $relationships; } - /** - * @return bool - */ public function hasRelationships(): bool { - return !empty($this->getRelationships()); + return ! empty($this->getRelationships()); } /** @@ -313,7 +283,6 @@ public function useInitial() /** * Dynamically retrieve attributes on the model. * - * @param string $key * * @return mixed */ @@ -325,9 +294,7 @@ public function __get(string $key) /** * Dynamically set attributes on the model. * - * @param string $key - * @param mixed $value - * + * @param mixed $value * @return void */ public function __set(string $key, $value) @@ -338,8 +305,7 @@ public function __set(string $key, $value) /** * Determine if the given attribute exists. * - * @param mixed $offset - * + * @param mixed $offset * @return bool */ #[\ReturnTypeWillChange] @@ -349,14 +315,13 @@ public function offsetExists($offset) return $this->hasId(); } - return !is_null($this->getAttribute($offset)); + return ! is_null($this->getAttribute($offset)); } /** * Get the value for a given offset. * - * @param mixed $offset - * + * @param mixed $offset * @return mixed */ #[\ReturnTypeWillChange] @@ -372,9 +337,8 @@ public function offsetGet($offset) /** * Set the value for a given offset. * - * @param mixed $offset - * @param mixed $value - * + * @param mixed $offset + * @param mixed $value * @return void */ #[\ReturnTypeWillChange] @@ -392,8 +356,7 @@ public function offsetSet($offset, $value) /** * Unset the value for a given offset. * - * @param mixed $offset - * + * @param mixed $offset * @return void */ #[\ReturnTypeWillChange] @@ -409,7 +372,6 @@ public function offsetUnset($offset) /** * Determine if an attribute or relation exists on the model. * - * @param string $key * * @return bool */ @@ -421,7 +383,6 @@ public function __isset(string $key) /** * Unset an attribute on the model. * - * @param string $key * * @return void */ diff --git a/src/ItemDocument.php b/src/ItemDocument.php index 45b9f64..705a595 100644 --- a/src/ItemDocument.php +++ b/src/ItemDocument.php @@ -6,6 +6,4 @@ use Swis\JsonApi\Client\Interfaces\ItemDocumentInterface; -class ItemDocument extends Document implements ItemDocumentInterface -{ -} +class ItemDocument extends Document implements ItemDocumentInterface {} diff --git a/src/ItemHydrator.php b/src/ItemHydrator.php index 41f644f..f576d17 100644 --- a/src/ItemHydrator.php +++ b/src/ItemHydrator.php @@ -21,22 +21,13 @@ class ItemHydrator */ protected $typeMapper; - /** - * @param \Swis\JsonApi\Client\Interfaces\TypeMapperInterface $typeMapper - */ public function __construct(TypeMapperInterface $typeMapper) { $this->typeMapper = $typeMapper; } /** - * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item - * @param array $attributes - * @param string|null $id - * * @throws \Swis\JsonApi\Client\Exceptions\HydrationException - * - * @return \Swis\JsonApi\Client\Interfaces\ItemInterface */ public function hydrate(ItemInterface $item, array $attributes, ?string $id = null): ItemInterface { @@ -50,10 +41,6 @@ public function hydrate(ItemInterface $item, array $attributes, ?string $id = nu return $item; } - /** - * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item - * @param array $attributes - */ protected function fill(ItemInterface $item, array $attributes): void { $item->fill(Util::arrayExcept($attributes, $item->getAvailableRelations())); @@ -62,8 +49,6 @@ protected function fill(ItemInterface $item, array $attributes): void /** * Get relationships from the attributes and add them to the item. * - * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item - * @param array $attributes * * @throws \Swis\JsonApi\Client\Exceptions\HydrationException */ @@ -71,7 +56,7 @@ protected function fillRelations(ItemInterface $item, array $attributes): void { // Fill Relations foreach ($item->getAvailableRelations() as $availableRelation) { - if (!array_key_exists($availableRelation, $attributes)) { + if (! array_key_exists($availableRelation, $attributes)) { // No data found, continue continue; } @@ -102,17 +87,14 @@ protected function fillRelations(ItemInterface $item, array $attributes): void } /** - * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item - * @param string $availableRelation + * @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface * * @throws \Swis\JsonApi\Client\Exceptions\HydrationException - * - * @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface|\Swis\JsonApi\Client\Interfaces\ManyRelationInterface */ protected function getRelationFromItem(ItemInterface $item, string $availableRelation) { $method = Util::stringCamel($availableRelation); - if (!method_exists($item, $method)) { + if (! method_exists($item, $method)) { throw new HydrationException(sprintf('Method %s not found on %s', $method, get_class($item))); } @@ -120,8 +102,7 @@ protected function getRelationFromItem(ItemInterface $item, string $availableRel } /** - * @param \Swis\JsonApi\Client\Relations\HasOneRelation $relation - * @param array|string $attributes + * @param array|string $attributes * * @throws \Swis\JsonApi\Client\Exceptions\HydrationException */ @@ -137,9 +118,6 @@ protected function hydrateHasOneRelation(HasOneRelation $relation, $attributes): } /** - * @param \Swis\JsonApi\Client\Relations\HasManyRelation $relation - * @param array $attributes - * * @throws \Swis\JsonApi\Client\Exceptions\HydrationException */ protected function hydrateHasManyRelation(HasManyRelation $relation, array $attributes): void @@ -156,14 +134,11 @@ protected function hydrateHasManyRelation(HasManyRelation $relation, array $attr } /** - * @param \Swis\JsonApi\Client\Relations\MorphToRelation $relation - * @param array $attributes - * * @throws \Swis\JsonApi\Client\Exceptions\HydrationException */ protected function hydrateMorphToRelation(MorphToRelation $relation, array $attributes): void { - if (!array_key_exists('type', $attributes)) { + if (! array_key_exists('type', $attributes)) { throw new HydrationException('Always provide a "type" attribute in a morphTo relationship'); } $relationItem = $this->buildItem($attributes['type'], Util::arrayExcept($attributes, 'type')); @@ -172,15 +147,12 @@ protected function hydrateMorphToRelation(MorphToRelation $relation, array $attr } /** - * @param \Swis\JsonApi\Client\Relations\MorphToManyRelation $relation - * @param array $attributes - * * @throws \Swis\JsonApi\Client\Exceptions\HydrationException */ protected function hydrateMorphToManyRelation(MorphToManyRelation $relation, array $attributes): void { foreach ($attributes as $relationData) { - if (!array_key_exists('type', $relationData)) { + if (! array_key_exists('type', $relationData)) { throw new HydrationException('Always provide a "type" attribute in a morphToMany relationship entry'); } $relationItem = $this->buildItem($relationData['type'], Util::arrayExcept($relationData, 'type')); @@ -190,16 +162,11 @@ protected function hydrateMorphToManyRelation(MorphToManyRelation $relation, arr } /** - * @param string $type - * @param array $attributes - * * @throws \Swis\JsonApi\Client\Exceptions\HydrationException - * - * @return \Swis\JsonApi\Client\Interfaces\ItemInterface */ protected function buildItem(string $type, array $attributes): ItemInterface { - $item = (new Item())->setType($type); + $item = (new Item)->setType($type); if ($this->typeMapper->hasMapping($type)) { $item = $this->typeMapper->getMapping($type); } diff --git a/src/Jsonapi.php b/src/Jsonapi.php index 0e6d552..a315182 100644 --- a/src/Jsonapi.php +++ b/src/Jsonapi.php @@ -8,7 +8,7 @@ use Illuminate\Contracts\Support\Jsonable; use Swis\JsonApi\Client\Concerns\HasMeta; -class Jsonapi implements Arrayable, Jsonable, \JsonSerializable +class Jsonapi implements \JsonSerializable, Arrayable, Jsonable { use HasMeta; @@ -17,10 +17,6 @@ class Jsonapi implements Arrayable, Jsonable, \JsonSerializable */ protected $version; - /** - * @param string|null $version - * @param \Swis\JsonApi\Client\Meta|null $meta - */ public function __construct(?string $version = null, ?Meta $meta = null) { $this->version = $version; @@ -37,8 +33,6 @@ public function getVersion() /** * {@inheritdoc} - * - * @return array */ public function toArray(): array { @@ -58,8 +52,7 @@ public function toArray(): array /** * {@inheritdoc} * - * @param int $options - * + * @param int $options * @return false|string */ public function toJson($options = 0) diff --git a/src/Link.php b/src/Link.php index 39125fa..a47fa69 100644 --- a/src/Link.php +++ b/src/Link.php @@ -15,19 +15,12 @@ class Link */ protected $href; - /** - * @param string $href - * @param \Swis\JsonApi\Client\Meta|null $meta - */ public function __construct(string $href, ?Meta $meta = null) { $this->href = $href; $this->meta = $meta; } - /** - * @return string - */ public function getHref(): string { return $this->href; diff --git a/src/Links.php b/src/Links.php index 7add31a..d2b3f74 100644 --- a/src/Links.php +++ b/src/Links.php @@ -7,7 +7,7 @@ use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; -class Links implements \ArrayAccess, Arrayable, Jsonable, \JsonSerializable +class Links implements \ArrayAccess, \JsonSerializable, Arrayable, Jsonable { /** * @var \Swis\JsonApi\Client\Link[] @@ -15,7 +15,7 @@ class Links implements \ArrayAccess, Arrayable, Jsonable, \JsonSerializable protected $links = []; /** - * @param \Swis\JsonApi\Client\Link[] $links + * @param \Swis\JsonApi\Client\Link[] $links */ public function __construct(array $links) { @@ -23,8 +23,6 @@ public function __construct(array $links) } /** - * @param $key - * * @return bool */ public function __isset($key) @@ -33,8 +31,6 @@ public function __isset($key) } /** - * @param $key - * * @return mixed */ public function __get($key) @@ -42,18 +38,11 @@ public function __get($key) return $this->offsetGet($key); } - /** - * @param $key - */ public function __unset($key) { $this->offsetUnset($key); } - /** - * @param $key - * @param $value - */ public function __set($key, $value) { $this->offsetSet($key, $value); @@ -62,8 +51,7 @@ public function __set($key, $value) /** * {@inheritdoc} * - * @param mixed $offset - * + * @param mixed $offset * @return bool */ #[\ReturnTypeWillChange] @@ -75,8 +63,7 @@ public function offsetExists($offset) /** * {@inheritdoc} * - * @param mixed $offset - * + * @param mixed $offset * @return mixed */ #[\ReturnTypeWillChange] @@ -88,8 +75,8 @@ public function offsetGet($offset) /** * {@inheritdoc} * - * @param mixed $offset - * @param mixed $value + * @param mixed $offset + * @param mixed $value */ #[\ReturnTypeWillChange] public function offsetSet($offset, $value) @@ -100,7 +87,7 @@ public function offsetSet($offset, $value) /** * {@inheritdoc} * - * @param mixed $offset + * @param mixed $offset */ #[\ReturnTypeWillChange] public function offsetUnset($offset) @@ -124,8 +111,7 @@ public function toArray() /** * {@inheritdoc} * - * @param int $options - * + * @param int $options * @return false|string */ public function toJson($options = 0) diff --git a/src/Meta.php b/src/Meta.php index d047f62..02665de 100644 --- a/src/Meta.php +++ b/src/Meta.php @@ -7,24 +7,19 @@ use Illuminate\Contracts\Support\Arrayable; use Illuminate\Contracts\Support\Jsonable; -class Meta implements \ArrayAccess, Arrayable, Jsonable, \JsonSerializable +class Meta implements \ArrayAccess, \JsonSerializable, Arrayable, Jsonable { /** * @var array */ protected $data = []; - /** - * @param array $data - */ public function __construct(array $data) { $this->data = $data; } /** - * @param $key - * * @return bool */ public function __isset($key) @@ -33,8 +28,6 @@ public function __isset($key) } /** - * @param $key - * * @return mixed */ public function __get($key) @@ -42,18 +35,11 @@ public function __get($key) return $this->offsetGet($key); } - /** - * @param $key - */ public function __unset($key) { $this->offsetUnset($key); } - /** - * @param $key - * @param $value - */ public function __set($key, $value) { $this->offsetSet($key, $value); @@ -62,8 +48,7 @@ public function __set($key, $value) /** * {@inheritdoc} * - * @param mixed $offset - * + * @param mixed $offset * @return bool */ #[\ReturnTypeWillChange] @@ -75,8 +60,7 @@ public function offsetExists($offset) /** * {@inheritdoc} * - * @param mixed $offset - * + * @param mixed $offset * @return mixed */ #[\ReturnTypeWillChange] @@ -88,8 +72,8 @@ public function offsetGet($offset) /** * {@inheritdoc} * - * @param mixed $offset - * @param mixed $value + * @param mixed $offset + * @param mixed $value */ #[\ReturnTypeWillChange] public function offsetSet($offset, $value) @@ -100,7 +84,7 @@ public function offsetSet($offset, $value) /** * {@inheritdoc} * - * @param mixed $offset + * @param mixed $offset */ #[\ReturnTypeWillChange] public function offsetUnset($offset) @@ -121,8 +105,7 @@ public function toArray() /** * {@inheritdoc} * - * @param int $options - * + * @param int $options * @return false|string */ public function toJson($options = 0) diff --git a/src/Parsers/CollectionParser.php b/src/Parsers/CollectionParser.php index 29f9b73..e3e11d6 100644 --- a/src/Parsers/CollectionParser.php +++ b/src/Parsers/CollectionParser.php @@ -20,13 +20,11 @@ public function __construct(ItemParser $itemParser) } /** - * @param mixed $data - * - * @return \Swis\JsonApi\Client\Collection + * @param mixed $data */ public function parse($data): Collection { - if (!is_array($data)) { + if (! is_array($data)) { throw new ValidationException(sprintf('ResourceCollection MUST be an array, "%s" given.', gettype($data))); } diff --git a/src/Parsers/DocumentParser.php b/src/Parsers/DocumentParser.php index 398868f..e60bbb8 100644 --- a/src/Parsers/DocumentParser.php +++ b/src/Parsers/DocumentParser.php @@ -37,7 +37,7 @@ public function __construct( ErrorCollectionParser $errorCollectionParser, LinksParser $linksParser, JsonapiParser $jsonapiParser, - MetaParser $metaParser + MetaParser $metaParser, ) { $this->itemParser = $itemParser; $this->collectionParser = $collectionParser; @@ -48,15 +48,13 @@ public function __construct( } /** - * @param \Swis\JsonApi\Client\Interfaces\TypeMapperInterface|null $typeMapper - * * @return static */ public static function create(?TypeMapperInterface $typeMapper = null): self { - $metaParser = new MetaParser(); + $metaParser = new MetaParser; $linksParser = new LinksParser($metaParser); - $itemParser = new ItemParser($typeMapper ?? new TypeMapper(), $linksParser, $metaParser); + $itemParser = new ItemParser($typeMapper ?? new TypeMapper, $linksParser, $metaParser); return new static( $itemParser, @@ -70,31 +68,26 @@ public static function create(?TypeMapperInterface $typeMapper = null): self ); } - /** - * @param string $json - * - * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface - */ public function parse(string $json): DocumentInterface { $data = $this->decodeJson($json); - if (!is_object($data)) { + if (! is_object($data)) { throw new ValidationException(sprintf('Document MUST be an object, "%s" given.', gettype($data))); } - if (!property_exists($data, 'data') && !property_exists($data, 'errors') && !property_exists($data, 'meta')) { + if (! property_exists($data, 'data') && ! property_exists($data, 'errors') && ! property_exists($data, 'meta')) { throw new ValidationException('Document MUST contain at least one of the following properties: `data`, `errors`, `meta`.'); } if (property_exists($data, 'data') && property_exists($data, 'errors')) { throw new ValidationException('The properties `data` and `errors` MUST NOT coexist in Document.'); } - if (!property_exists($data, 'data') && property_exists($data, 'included')) { + if (! property_exists($data, 'data') && property_exists($data, 'included')) { throw new ValidationException('If Document does not contain a `data` property, the `included` property MUST NOT be present either.'); } - if (property_exists($data, 'data') && !is_object($data->data) && !is_array($data->data) && $data->data !== null) { + if (property_exists($data, 'data') && ! is_object($data->data) && ! is_array($data->data) && $data->data !== null) { throw new ValidationException(sprintf('Document property "data" MUST be null, an array or an object, "%s" given.', gettype($data->data))); } - if (property_exists($data, 'included') && !is_array($data->included)) { + if (property_exists($data, 'included') && ! is_array($data->included)) { throw new ValidationException(sprintf('Document property "included" MUST be an array, "%s" given.', gettype($data->included))); } @@ -120,8 +113,6 @@ public function parse(string $json): DocumentInterface } /** - * @param string $json - * * @return mixed */ private function decodeJson(string $json) @@ -134,21 +125,19 @@ private function decodeJson(string $json) } /** - * @param mixed $data - * - * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface + * @param mixed $data */ private function getDocument($data): DocumentInterface { - if (!property_exists($data, 'data') || $data->data === null) { - return new Document(); + if (! property_exists($data, 'data') || $data->data === null) { + return new Document; } if (is_array($data->data)) { - $document = (new CollectionDocument()) + $document = (new CollectionDocument) ->setData($this->collectionParser->parse($data->data)); } else { - $document = (new ItemDocument()) + $document = (new ItemDocument) ->setData($this->itemParser->parse($data->data)); } @@ -170,9 +159,6 @@ private function getDocument($data): DocumentInterface return $document; } - /** - * @param \Swis\JsonApi\Client\Collection $items - */ private function linkRelationships(Collection $items): void { $keyedItems = $items->keyBy(fn (ItemInterface $item) => $this->getItemKey($item)); @@ -209,32 +195,16 @@ function (ItemInterface $item) use ($keyedItems) { ); } - /** - * @param \Swis\JsonApi\Client\Collection $included - * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item - * - * @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null - */ private function getItem(Collection $included, ItemInterface $item): ?ItemInterface { return $included->get($this->getItemKey($item)); } - /** - * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item - * - * @return string - */ private function getItemKey(ItemInterface $item): string { return sprintf('%s:%s', $item->getType(), $item->getId()); } - /** - * @param \Swis\JsonApi\Client\Collection $items - * - * @return \Swis\JsonApi\Client\Collection - */ private function getDuplicateItems(Collection $items): Collection { return $items->duplicates(fn (ItemInterface $item) => $this->getItemKey($item)); diff --git a/src/Parsers/ErrorCollectionParser.php b/src/Parsers/ErrorCollectionParser.php index 823a22a..7d8d28d 100644 --- a/src/Parsers/ErrorCollectionParser.php +++ b/src/Parsers/ErrorCollectionParser.php @@ -20,13 +20,11 @@ public function __construct(ErrorParser $errorParser) } /** - * @param mixed $data - * - * @return \Swis\JsonApi\Client\ErrorCollection + * @param mixed $data */ public function parse($data): ErrorCollection { - if (!is_array($data)) { + if (! is_array($data)) { throw new ValidationException(sprintf('ErrorCollection MUST be an array, "%s" given.', gettype($data))); } if (count($data) === 0) { diff --git a/src/Parsers/ErrorParser.php b/src/Parsers/ErrorParser.php index e8ec0d4..500ccc3 100644 --- a/src/Parsers/ErrorParser.php +++ b/src/Parsers/ErrorParser.php @@ -24,28 +24,26 @@ public function __construct(LinksParser $linksParser, MetaParser $metaParser) } /** - * @param mixed $data - * - * @return \Swis\JsonApi\Client\Error + * @param mixed $data */ public function parse($data): Error { - if (!is_object($data)) { + if (! is_object($data)) { throw new ValidationException(sprintf('Error MUST be an object, "%s" given.', gettype($data))); } - if (property_exists($data, 'id') && !is_string($data->id)) { + if (property_exists($data, 'id') && ! is_string($data->id)) { throw new ValidationException(sprintf('Error property "id" MUST be a string, "%s" given.', gettype($data->id))); } - if (property_exists($data, 'status') && !is_string($data->status)) { + if (property_exists($data, 'status') && ! is_string($data->status)) { throw new ValidationException(sprintf('Error property "status" MUST be a string, "%s" given.', gettype($data->status))); } - if (property_exists($data, 'code') && !is_string($data->code)) { + if (property_exists($data, 'code') && ! is_string($data->code)) { throw new ValidationException(sprintf('Error property "code" MUST be a string, "%s" given.', gettype($data->code))); } - if (property_exists($data, 'title') && !is_string($data->title)) { + if (property_exists($data, 'title') && ! is_string($data->title)) { throw new ValidationException(sprintf('Error property "title" MUST be a string, "%s" given.', gettype($data->title))); } - if (property_exists($data, 'detail') && !is_string($data->detail)) { + if (property_exists($data, 'detail') && ! is_string($data->detail)) { throw new ValidationException(sprintf('Error property "detail" MUST be a string, "%s" given.', gettype($data->detail))); } @@ -62,19 +60,17 @@ public function parse($data): Error } /** - * @param mixed $data - * - * @return \Swis\JsonApi\Client\ErrorSource + * @param mixed $data */ private function buildErrorSource($data): ErrorSource { - if (!is_object($data)) { + if (! is_object($data)) { throw new ValidationException(sprintf('ErrorSource MUST be an object, "%s" given.', gettype($data))); } - if (property_exists($data, 'pointer') && !is_string($data->pointer)) { + if (property_exists($data, 'pointer') && ! is_string($data->pointer)) { throw new ValidationException(sprintf('ErrorSource property "pointer" MUST be a string, "%s" given.', gettype($data->pointer))); } - if (property_exists($data, 'parameter') && !is_string($data->parameter)) { + if (property_exists($data, 'parameter') && ! is_string($data->parameter)) { throw new ValidationException(sprintf('ErrorSource property "parameter" MUST be a string, "%s" given.', gettype($data->parameter))); } diff --git a/src/Parsers/ItemParser.php b/src/Parsers/ItemParser.php index 90388fe..ddfbb47 100644 --- a/src/Parsers/ItemParser.php +++ b/src/Parsers/ItemParser.php @@ -30,29 +30,27 @@ public function __construct(TypeMapperInterface $typeMapper, LinksParser $linksP } /** - * @param mixed $data - * - * @return \Swis\JsonApi\Client\Interfaces\ItemInterface + * @param mixed $data */ public function parse($data): ItemInterface { - if (!is_object($data)) { + if (! is_object($data)) { throw new ValidationException(sprintf('Resource MUST be an object, "%s" given.', gettype($data))); } - if (!property_exists($data, 'type')) { + if (! property_exists($data, 'type')) { throw new ValidationException('Resource object MUST contain a type.'); } - if (!property_exists($data, 'id')) { + if (! property_exists($data, 'id')) { throw new ValidationException('Resource object MUST contain an id.'); } - if (!is_string($data->type)) { + if (! is_string($data->type)) { throw new ValidationException(sprintf('Resource property "type" MUST be a string, "%s" given.', gettype($data->type))); } - if (!is_string($data->id) && !is_numeric($data->id)) { + if (! is_string($data->id) && ! is_numeric($data->id)) { throw new ValidationException(sprintf('Resource property "id" MUST be a string, "%s" given.', gettype($data->id))); } if (property_exists($data, 'attributes')) { - if (!is_object($data->attributes)) { + if (! is_object($data->attributes)) { throw new ValidationException(sprintf('Resource property "attributes" MUST be an object, "%s" given.', gettype($data->attributes))); } if (property_exists($data->attributes, 'type') || property_exists($data->attributes, 'id') || property_exists($data->attributes, 'relationships') || property_exists($data->attributes, 'links')) { @@ -82,27 +80,21 @@ public function parse($data): ItemInterface return $item; } - /** - * @param string $type - * - * @return \Swis\JsonApi\Client\Interfaces\ItemInterface - */ private function getItemInstance(string $type): ItemInterface { if ($this->typeMapper->hasMapping($type)) { return $this->typeMapper->getMapping($type); } - return (new Item())->setType($type); + return (new Item)->setType($type); } /** - * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $item - * @param mixed $data + * @param mixed $data */ private function setRelations(ItemInterface $item, $data): void { - if (!is_object($data)) { + if (! is_object($data)) { throw new ValidationException(sprintf('Resource property "relationships" MUST be an object, "%s" given.', gettype($data))); } if (property_exists($data, 'type') || property_exists($data, 'id')) { @@ -113,10 +105,10 @@ private function setRelations(ItemInterface $item, $data): void if ($item->hasAttribute($name)) { throw new ValidationException(sprintf('Relationship "%s" cannot be set because it already exists in Resource object.', $name)); } - if (!is_object($relationship)) { + if (! is_object($relationship)) { throw new ValidationException(sprintf('Relationship MUST be an object, "%s" given.', gettype($relationship))); } - if (!property_exists($relationship, 'links') && !property_exists($relationship, 'data') && !property_exists($relationship, 'meta')) { + if (! property_exists($relationship, 'links') && ! property_exists($relationship, 'data') && ! property_exists($relationship, 'meta')) { throw new ValidationException('Relationship object MUST contain at least one of the following properties: `links`, `data`, `meta`.'); } @@ -143,11 +135,9 @@ private function setRelations(ItemInterface $item, $data): void } /** - * @param mixed $data + * @param mixed $data * * @throws \InvalidArgumentException - * - * @return \Swis\JsonApi\Client\Interfaces\DataInterface */ private function parseRelationshipData($data): DataInterface { @@ -156,19 +146,19 @@ private function parseRelationshipData($data): DataInterface ->map(fn ($identifier) => $this->parseRelationshipData($identifier)); } - if (!is_object($data)) { + if (! is_object($data)) { throw new ValidationException(sprintf('ResourceIdentifier MUST be an object, "%s" given.', gettype($data))); } - if (!property_exists($data, 'type')) { + if (! property_exists($data, 'type')) { throw new ValidationException('ResourceIdentifier object MUST contain a type.'); } - if (!property_exists($data, 'id')) { + if (! property_exists($data, 'id')) { throw new ValidationException('ResourceIdentifier object MUST contain an id.'); } - if (!is_string($data->type)) { + if (! is_string($data->type)) { throw new ValidationException(sprintf('ResourceIdentifier property "type" MUST be a string, "%s" given.', gettype($data->type))); } - if (!is_string($data->id) && !is_numeric($data->id)) { + if (! is_string($data->id) && ! is_numeric($data->id)) { throw new ValidationException(sprintf('ResourceIdentifier property "id" MUST be a string, "%s" given.', gettype($data->id))); } diff --git a/src/Parsers/JsonapiParser.php b/src/Parsers/JsonapiParser.php index d24c255..aad18f5 100644 --- a/src/Parsers/JsonapiParser.php +++ b/src/Parsers/JsonapiParser.php @@ -20,16 +20,14 @@ public function __construct(MetaParser $metaParser) } /** - * @param mixed $data - * - * @return \Swis\JsonApi\Client\Jsonapi + * @param mixed $data */ public function parse($data): Jsonapi { - if (!is_object($data)) { + if (! is_object($data)) { throw new ValidationException(sprintf('Jsonapi MUST be an object, "%s" given.', gettype($data))); } - if (property_exists($data, 'version') && !is_string($data->version)) { + if (property_exists($data, 'version') && ! is_string($data->version)) { throw new ValidationException(sprintf('Jsonapi property "version" MUST be a string, "%s" given.', gettype($data->version))); } diff --git a/src/Parsers/LinksParser.php b/src/Parsers/LinksParser.php index 078463a..f21ae64 100644 --- a/src/Parsers/LinksParser.php +++ b/src/Parsers/LinksParser.php @@ -35,17 +35,14 @@ public function __construct(MetaParser $metaParser) } /** - * @param mixed $data - * @param string $source - * - * @return \Swis\JsonApi\Client\Links + * @param mixed $data */ public function parse($data, string $source): Links { - if (!is_object($data)) { + if (! is_object($data)) { throw new ValidationException(sprintf('Links MUST be an object, "%s" given.', gettype($data))); } - if ($source === self::SOURCE_RELATIONSHIP && !property_exists($data, 'self') && !property_exists($data, 'related')) { + if ($source === self::SOURCE_RELATIONSHIP && ! property_exists($data, 'self') && ! property_exists($data, 'related')) { throw new ValidationException('Relationship links object MUST contain at least one of the following properties: `self`, `related`.'); } @@ -57,14 +54,11 @@ public function parse($data, string $source): Links } /** - * @param mixed $data - * @param string $name - * - * @return \Swis\JsonApi\Client\Link + * @param mixed $data */ private function buildLink($data, string $name): ?Link { - if (in_array($name, self::LINKS_THAT_MAY_NOT_BE_NULL_WHEN_PRESENT, true) && !is_string($data) && !is_object($data)) { + if (in_array($name, self::LINKS_THAT_MAY_NOT_BE_NULL_WHEN_PRESENT, true) && ! is_string($data) && ! is_object($data)) { throw new ValidationException(sprintf('Link "%s" MUST be an object or string, "%s" given.', $name, gettype($data))); } @@ -76,10 +70,10 @@ private function buildLink($data, string $name): ?Link return new Link($data); } - if (!is_object($data)) { + if (! is_object($data)) { throw new ValidationException(sprintf('Link "%s" MUST be an object, string or null, "%s" given.', $name, gettype($data))); } - if (!property_exists($data, 'href')) { + if (! property_exists($data, 'href')) { throw new ValidationException(sprintf('Link "%s" MUST have a "href" attribute.', $name)); } diff --git a/src/Parsers/MetaParser.php b/src/Parsers/MetaParser.php index 5743dea..e4fd991 100644 --- a/src/Parsers/MetaParser.php +++ b/src/Parsers/MetaParser.php @@ -13,13 +13,11 @@ class MetaParser { /** - * @param mixed $data - * - * @return \Swis\JsonApi\Client\Meta + * @param mixed $data */ public function parse($data): Meta { - if (!is_object($data)) { + if (! is_object($data)) { throw new ValidationException(sprintf('Meta MUST be an object, "%s" given.', gettype($data))); } diff --git a/src/Parsers/ResponseParser.php b/src/Parsers/ResponseParser.php index 029b4a6..0c2ac59 100644 --- a/src/Parsers/ResponseParser.php +++ b/src/Parsers/ResponseParser.php @@ -22,8 +22,6 @@ public function __construct(DocumentParserInterface $parser) } /** - * @param \Swis\JsonApi\Client\Interfaces\TypeMapperInterface|null $typeMapper - * * @return static */ public static function create(?TypeMapperInterface $typeMapper = null): self @@ -31,19 +29,14 @@ public static function create(?TypeMapperInterface $typeMapper = null): self return new static(DocumentParser::create($typeMapper)); } - /** - * @param \Psr\Http\Message\ResponseInterface $response - * - * @return \Swis\JsonApi\Client\Interfaces\DocumentInterface - */ public function parse(ResponseInterface $response): DocumentInterface { - $document = new InvalidResponseDocument(); + $document = new InvalidResponseDocument; if ($this->responseHasBody($response)) { $document = $this->parser->parse((string) $response->getBody()); } elseif ($this->responseHasSuccessfulStatusCode($response)) { - $document = new Document(); + $document = new Document; } $document->setResponse($response); @@ -51,21 +44,29 @@ public function parse(ResponseInterface $response): DocumentInterface return $document; } - /** - * @param \Psr\Http\Message\ResponseInterface $response - * - * @return bool - */ private function responseHasBody(ResponseInterface $response): bool { - return (bool) $response->getBody()->getSize(); + $body = $response->getBody(); + $size = $body->getSize(); + + if ($size === 0) { + return false; + } + if (is_int($size) && $size > 0) { + return true; + } + + if ($body->isSeekable()) { + $pos = $body->tell(); + $chunk = $body->read(1); + $body->seek($pos); + } else { + $chunk = $body->read(1); + } + + return $chunk !== ''; } - /** - * @param \Psr\Http\Message\ResponseInterface $response - * - * @return bool - */ private function responseHasSuccessfulStatusCode(ResponseInterface $response): bool { return $response->getStatusCode() >= 200 && $response->getStatusCode() < 300; diff --git a/src/Relations/AbstractManyRelation.php b/src/Relations/AbstractManyRelation.php index 533a634..5e3fc16 100644 --- a/src/Relations/AbstractManyRelation.php +++ b/src/Relations/AbstractManyRelation.php @@ -14,8 +14,6 @@ abstract class AbstractManyRelation extends AbstractRelation implements ManyRelationInterface { /** - * @param \Swis\JsonApi\Client\Collection|null $data - * * @return $this */ public function setData(?Collection $data) @@ -25,17 +23,12 @@ public function setData(?Collection $data) return $this; } - /** - * @return \Swis\JsonApi\Client\Collection|null - */ public function getData(): ?Collection { return $this->data ?: null; } /** - * @param \Swis\JsonApi\Client\Collection $included - * * @return $this */ public function setIncluded(Collection $included) @@ -45,17 +38,12 @@ public function setIncluded(Collection $included) return $this; } - /** - * @return \Swis\JsonApi\Client\Collection - */ public function getIncluded(): Collection { - return $this->included ?: new Collection(); + return $this->included ?: new Collection; } /** - * @param \Swis\JsonApi\Client\Collection $included - * * @return $this */ public function associate(Collection $included) @@ -64,9 +52,6 @@ public function associate(Collection $included) ->setIncluded($included); } - /** - * @return \Swis\JsonApi\Client\Collection - */ public function getAssociated(): Collection { if ($this->hasIncluded()) { @@ -77,17 +62,16 @@ public function getAssociated(): Collection return $this->getData(); } - return new Collection(); + return new Collection; } /** * Sort the included collection by the given key. * You can also pass your own callback to determine how to sort the collection values. * - * @param callable $callback - * @param int $options - * @param bool $descending - * + * @param callable $callback + * @param int $options + * @param bool $descending * @return $this */ public function sortBy($callback, $options = SORT_REGULAR, $descending = false) diff --git a/src/Relations/AbstractOneRelation.php b/src/Relations/AbstractOneRelation.php index 002b167..33c7c6e 100644 --- a/src/Relations/AbstractOneRelation.php +++ b/src/Relations/AbstractOneRelation.php @@ -14,8 +14,6 @@ abstract class AbstractOneRelation extends AbstractRelation implements OneRelationInterface { /** - * @param \Swis\JsonApi\Client\Interfaces\ItemInterface|null $data - * * @return $this */ public function setData(?ItemInterface $data) @@ -25,17 +23,12 @@ public function setData(?ItemInterface $data) return $this; } - /** - * @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null - */ public function getData(): ?ItemInterface { return $this->data ?: null; } /** - * @param \Swis\JsonApi\Client\Interfaces\ItemInterface|null $included - * * @return $this */ public function setIncluded(?ItemInterface $included) @@ -45,17 +38,12 @@ public function setIncluded(?ItemInterface $included) return $this; } - /** - * @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null - */ public function getIncluded(): ?ItemInterface { return $this->included ?: null; } /** - * @param \Swis\JsonApi\Client\Interfaces\ItemInterface $included - * * @return $this */ public function associate(ItemInterface $included) @@ -64,9 +52,6 @@ public function associate(ItemInterface $included) ->setIncluded($included); } - /** - * @return \Swis\JsonApi\Client\Interfaces\ItemInterface|null - */ public function getAssociated(): ?ItemInterface { if ($this->hasIncluded()) { diff --git a/src/Relations/AbstractRelation.php b/src/Relations/AbstractRelation.php index 4e4fe04..0ba6681 100644 --- a/src/Relations/AbstractRelation.php +++ b/src/Relations/AbstractRelation.php @@ -27,17 +27,11 @@ abstract class AbstractRelation */ protected $omitIncluded = false; - /** - * @return bool - */ public function hasData(): bool { return $this->data !== false; } - /** - * @return bool - */ public function hasIncluded(): bool { return $this->included !== false; @@ -54,17 +48,12 @@ public function dissociate() return $this; } - /** - * @return bool - */ public function hasAssociated(): bool { return $this->hasData() || $this->hasIncluded(); } /** - * @param bool $omitIncluded - * * @return $this */ public function setOmitIncluded(bool $omitIncluded) @@ -74,9 +63,6 @@ public function setOmitIncluded(bool $omitIncluded) return $this; } - /** - * @return bool - */ public function shouldOmitIncluded(): bool { return $this->omitIncluded; diff --git a/src/Relations/HasManyRelation.php b/src/Relations/HasManyRelation.php index 0271672..42fa187 100644 --- a/src/Relations/HasManyRelation.php +++ b/src/Relations/HasManyRelation.php @@ -11,9 +11,6 @@ class HasManyRelation extends AbstractManyRelation implements TypedRelationInter { use HasType; - /** - * @param string $type - */ public function __construct(string $type) { $this->setType($type); diff --git a/src/Relations/HasOneRelation.php b/src/Relations/HasOneRelation.php index df3e2cd..a820526 100644 --- a/src/Relations/HasOneRelation.php +++ b/src/Relations/HasOneRelation.php @@ -11,9 +11,6 @@ class HasOneRelation extends AbstractOneRelation implements TypedRelationInterfa { use HasType; - /** - * @param string $type - */ public function __construct(string $type) { $this->setType($type); diff --git a/src/Relations/MorphToManyRelation.php b/src/Relations/MorphToManyRelation.php index a89c3e3..4ee153d 100644 --- a/src/Relations/MorphToManyRelation.php +++ b/src/Relations/MorphToManyRelation.php @@ -4,6 +4,4 @@ namespace Swis\JsonApi\Client\Relations; -class MorphToManyRelation extends AbstractManyRelation -{ -} +class MorphToManyRelation extends AbstractManyRelation {} diff --git a/src/Relations/MorphToRelation.php b/src/Relations/MorphToRelation.php index 6ba52c4..cc10b7c 100644 --- a/src/Relations/MorphToRelation.php +++ b/src/Relations/MorphToRelation.php @@ -4,6 +4,4 @@ namespace Swis\JsonApi\Client\Relations; -class MorphToRelation extends AbstractOneRelation -{ -} +class MorphToRelation extends AbstractOneRelation {} diff --git a/src/Repository.php b/src/Repository.php index a0c0962..542a291 100644 --- a/src/Repository.php +++ b/src/Repository.php @@ -11,11 +11,24 @@ use Swis\JsonApi\Client\Actions\TakeOne; use Swis\JsonApi\Client\Interfaces\RepositoryInterface; +/** + * @template TItem of \Swis\JsonApi\Client\Interfaces\ItemInterface + * + * @implements RepositoryInterface + */ class Repository extends BaseRepository implements RepositoryInterface { + use Delete; + + /** @use FetchMany */ use FetchMany; + + /** @use FetchOne */ use FetchOne; - use TakeOne; + + /** @use Save */ use Save; - use Delete; + + /** @use TakeOne */ + use TakeOne; } diff --git a/src/TypeMapper.php b/src/TypeMapper.php index 742a693..2714a7d 100644 --- a/src/TypeMapper.php +++ b/src/TypeMapper.php @@ -16,55 +16,40 @@ class TypeMapper implements TypeMapperInterface protected $typeMappings = []; /** - * @param string $type - * @param string $class - * * @throws \Swis\JsonApi\Client\Exceptions\TypeMappingException */ public function setMapping(string $type, string $class): void { - if (!class_exists($class)) { + if (! class_exists($class)) { throw new TypeMappingException(sprintf('Class %s not found.', $class)); } - if (!is_subclass_of($class, ItemInterface::class)) { + if (! is_subclass_of($class, ItemInterface::class)) { throw new TypeMappingException(sprintf('Class %s must implement %s.', $class, ItemInterface::class)); } $this->typeMappings[$type] = $class; } - /** - * @param string $type - * - * @return bool - */ public function hasMapping(string $type): bool { return array_key_exists($type, $this->typeMappings); } - /** - * @param string $type - */ public function removeMapping(string $type): void { unset($this->typeMappings[$type]); } /** - * @param string $type - * * @throws \Swis\JsonApi\Client\Exceptions\TypeMappingException - * - * @return \Swis\JsonApi\Client\Interfaces\ItemInterface */ public function getMapping(string $type): ItemInterface { - if (!array_key_exists($type, $this->typeMappings)) { + if (! array_key_exists($type, $this->typeMappings)) { throw new TypeMappingException(sprintf('No mapping for type %s', $type)); } - return new $this->typeMappings[$type](); + return new $this->typeMappings[$type]; } } diff --git a/src/Util.php b/src/Util.php index d672f1d..6fb0325 100644 --- a/src/Util.php +++ b/src/Util.php @@ -18,7 +18,6 @@ class Util /** * Convert the given string to lower-case. * - * @param string $value * * @return string */ @@ -30,8 +29,6 @@ public static function stringLower(string $value) /** * Convert a string to snake case. * - * @param string $value - * @param string $delimiter * * @return string */ @@ -43,7 +40,7 @@ public static function stringSnake(string $value, string $delimiter = '_') return self::$snakeCache[$key][$delimiter]; } - if (!ctype_lower($value)) { + if (! ctype_lower($value)) { $value = preg_replace('/\s+/u', '', ucwords($value)); $value = static::stringLower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value)); @@ -55,7 +52,6 @@ public static function stringSnake(string $value, string $delimiter = '_') /** * Convert a value to studly caps case. * - * @param string $value * * @return string */ @@ -75,7 +71,6 @@ public static function stringStudly(string $value) /** * Convert a value to camel case. * - * @param string $value * * @return string */ @@ -91,9 +86,7 @@ public static function stringCamel(string $value) /** * Remove array element from array. * - * @param array $array - * @param mixed $keys - * + * @param mixed $keys * @return array */ public static function arrayExcept(array $array, $keys) diff --git a/tests/ClientTest.php b/tests/ClientTest.php index 1e112b3..91297cb 100644 --- a/tests/ClientTest.php +++ b/tests/ClientTest.php @@ -15,9 +15,9 @@ class ClientTest extends TestCase /** * @test */ - public function itCanGetAndSetTheBaseUrl() + public function it_can_get_and_set_the_base_url() { - $client = new Client(); + $client = new Client; $this->assertEquals('', $client->getBaseUri()); $client->setBaseUri('http://www.test-changed.com'); @@ -27,9 +27,9 @@ public function itCanGetAndSetTheBaseUrl() /** * @test */ - public function itCanGetAndSetTheDefaultHeaders() + public function it_can_get_and_set_the_default_headers() { - $client = new Client(); + $client = new Client; $this->assertEquals( [ @@ -58,9 +58,9 @@ public function itCanGetAndSetTheDefaultHeaders() /** * @test */ - public function itBuildsAGetRequest() + public function it_builds_a_get_request() { - $httpClient = new HttpMockClient(); + $httpClient = new HttpMockClient; $client = new Client($httpClient); $endpoint = '/test/1'; @@ -82,9 +82,9 @@ public function itBuildsAGetRequest() /** * @test */ - public function itBuildsADeleteRequest() + public function it_builds_a_delete_request() { - $httpClient = new HttpMockClient(); + $httpClient = new HttpMockClient; $client = new Client($httpClient); $endpoint = '/test/1'; @@ -106,9 +106,9 @@ public function itBuildsADeleteRequest() /** * @test */ - public function itBuildsAPatchRequest() + public function it_builds_a_patch_request() { - $httpClient = new HttpMockClient(); + $httpClient = new HttpMockClient; $client = new Client($httpClient); $endpoint = '/test/1'; @@ -131,9 +131,9 @@ public function itBuildsAPatchRequest() /** * @test */ - public function itBuildsAPostRequest() + public function it_builds_a_post_request() { - $httpClient = new HttpMockClient(); + $httpClient = new HttpMockClient; $client = new Client($httpClient); $endpoint = '/test/1'; @@ -156,9 +156,9 @@ public function itBuildsAPostRequest() /** * @test */ - public function itBuildsOtherRequests() + public function it_builds_other_requests() { - $httpClient = new HttpMockClient(); + $httpClient = new HttpMockClient; $client = new Client($httpClient); $endpoint = '/test/1'; @@ -180,9 +180,9 @@ public function itBuildsOtherRequests() /** * @test */ - public function itBuildsRequestsWithAStringAsBody() + public function it_builds_requests_with_a_string_as_body() { - $httpClient = new HttpMockClient(); + $httpClient = new HttpMockClient; $client = new Client($httpClient); $body = 'testvar=testvalue'; @@ -195,9 +195,9 @@ public function itBuildsRequestsWithAStringAsBody() /** * @test */ - public function itBuildsRequestsWithAResourceAsBody() + public function it_builds_requests_with_a_resource_as_body() { - $httpClient = new HttpMockClient(); + $httpClient = new HttpMockClient; $client = new Client($httpClient); $body = fopen('php://temp', 'r+'); @@ -211,9 +211,9 @@ public function itBuildsRequestsWithAResourceAsBody() /** * @test */ - public function itBuildsRequestsWithAStreamAsBody() + public function it_builds_requests_with_a_stream_as_body() { - $httpClient = new HttpMockClient(); + $httpClient = new HttpMockClient; $client = new Client($httpClient); $body = Utils::streamFor('testvar=testvalue'); @@ -226,9 +226,9 @@ public function itBuildsRequestsWithAStreamAsBody() /** * @test */ - public function itBuildsRequestsWithoutABody() + public function it_builds_requests_without_a_body() { - $httpClient = new HttpMockClient(); + $httpClient = new HttpMockClient; $client = new Client($httpClient); $body = null; @@ -241,12 +241,12 @@ public function itBuildsRequestsWithoutABody() /** * @test */ - public function itPrependsTheBaseUriIfTheEndpointIsRelative() + public function it_prepends_the_base_uri_if_the_endpoint_is_relative() { $baseUri = 'http://example.com/api'; $endpoint = '/test/1'; - $httpClient = new HttpMockClient(); + $httpClient = new HttpMockClient; $client = new Client($httpClient); $client->setBaseUri($baseUri); @@ -258,12 +258,12 @@ public function itPrependsTheBaseUriIfTheEndpointIsRelative() /** * @test */ - public function itDoesNotPrependTheBaseUriIfTheEndpointIsAlreadyAbsolute() + public function it_does_not_prepend_the_base_uri_if_the_endpoint_is_already_absolute() { $baseUri = 'http://example.com/api'; $endpoint = 'http://foo.bar/test/1'; - $httpClient = new HttpMockClient(); + $httpClient = new HttpMockClient; $client = new Client($httpClient); $client->setBaseUri($baseUri); diff --git a/tests/Concerns/HasIdTest.php b/tests/Concerns/HasIdTest.php index 4cd9f19..0bb7d1c 100644 --- a/tests/Concerns/HasIdTest.php +++ b/tests/Concerns/HasIdTest.php @@ -12,7 +12,7 @@ class HasIdTest extends TestCase /** * @test */ - public function itCanGetAndSetAnId() + public function it_can_get_and_set_an_id() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasId $mock */ $mock = $this->getMockForTrait(HasId::class); @@ -26,7 +26,7 @@ public function itCanGetAndSetAnId() /** * @test */ - public function itReturnsABooleanIndicatingIfItHasAnId() + public function it_returns_a_boolean_indicating_if_it_has_an_id() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasId $mock */ $mock = $this->getMockForTrait(HasId::class); diff --git a/tests/Concerns/HasInitialTest.php b/tests/Concerns/HasInitialTest.php index 5fb3f9d..3e4a774 100644 --- a/tests/Concerns/HasInitialTest.php +++ b/tests/Concerns/HasInitialTest.php @@ -12,7 +12,7 @@ class HasInitialTest extends TestCase /** * @test */ - public function itCanGetAndSetInitialAttributes() + public function it_can_get_and_set_initial_attributes() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasInitial $mock */ $mock = $this->getMockForTrait(HasInitial::class); @@ -27,7 +27,7 @@ public function itCanGetAndSetInitialAttributes() /** * @test */ - public function itReturnsABooleanIndicatingIfItHasInitialAttributes() + public function it_returns_a_boolean_indicating_if_it_has_initial_attributes() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasInitial $mock */ $mock = $this->getMockForTrait(HasInitial::class); diff --git a/tests/Concerns/HasLinksTest.php b/tests/Concerns/HasLinksTest.php index 418b731..629c6e4 100644 --- a/tests/Concerns/HasLinksTest.php +++ b/tests/Concerns/HasLinksTest.php @@ -13,7 +13,7 @@ class HasLinksTest extends TestCase /** * @test */ - public function itCanGetAndSetLinks() + public function it_can_get_and_set_links() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasLinks $mock */ $mock = $this->getMockForTrait(HasLinks::class); diff --git a/tests/Concerns/HasMetaTest.php b/tests/Concerns/HasMetaTest.php index 5b13e94..0783c44 100644 --- a/tests/Concerns/HasMetaTest.php +++ b/tests/Concerns/HasMetaTest.php @@ -13,7 +13,7 @@ class HasMetaTest extends TestCase /** * @test */ - public function itCanGetAndSetMeta() + public function it_can_get_and_set_meta() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasMeta $mock */ $mock = $this->getMockForTrait(HasMeta::class); diff --git a/tests/Concerns/HasRelationsTest.php b/tests/Concerns/HasRelationsTest.php index ac8aa8f..fa7ae42 100644 --- a/tests/Concerns/HasRelationsTest.php +++ b/tests/Concerns/HasRelationsTest.php @@ -22,11 +22,11 @@ class HasRelationsTest extends TestCase /** * @test */ - public function itCanGetAndSetAnItemAsRelation() + public function it_can_get_and_set_an_item_as_relation() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ $mock = $this->getMockForTrait(HasRelations::class); - $data = new Item(); + $data = new Item; $mock->setRelation('foo', $data); @@ -39,11 +39,11 @@ public function itCanGetAndSetAnItemAsRelation() /** * @test */ - public function itCanGetAndSetACollectionAsRelation() + public function it_can_get_and_set_a_collection_as_relation() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ $mock = $this->getMockForTrait(HasRelations::class); - $data = new Collection(); + $data = new Collection; $mock->setRelation('foo', $data); @@ -56,7 +56,7 @@ public function itCanGetAndSetACollectionAsRelation() /** * @test */ - public function itCanGetAndSetNullAsRelation() + public function it_can_get_and_set_null_as_relation() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ $mock = $this->getMockForTrait(HasRelations::class); @@ -72,7 +72,7 @@ public function itCanGetAndSetNullAsRelation() /** * @test */ - public function itDoesNotSetFalseAsRelation() + public function it_does_not_set_false_as_relation() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ $mock = $this->getMockForTrait(HasRelations::class); @@ -87,11 +87,11 @@ public function itDoesNotSetFalseAsRelation() /** * @test */ - public function itSetsTheLinksOnTheRelation() + public function it_sets_the_links_on_the_relation() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ $mock = $this->getMockForTrait(HasRelations::class); - $data = new Item(); + $data = new Item; $links = new Links([]); $mock->setRelation('foo', $data, $links); @@ -103,11 +103,11 @@ public function itSetsTheLinksOnTheRelation() /** * @test */ - public function itSetsTheMetaOnTheRelation() + public function it_sets_the_meta_on_the_relation() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ $mock = $this->getMockForTrait(HasRelations::class); - $data = new Item(); + $data = new Item; $meta = new Meta([]); $mock->setRelation('foo', $data, null, $meta); @@ -119,11 +119,11 @@ public function itSetsTheMetaOnTheRelation() /** * @test */ - public function itCanGetAllRelations() + public function it_can_get_all_relations() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ $mock = $this->getMockForTrait(HasRelations::class); - $data = new Item(); + $data = new Item; $mock->setRelation('foo', $data); $relation = $mock->getRelation('foo'); @@ -134,11 +134,11 @@ public function itCanGetAllRelations() /** * @test */ - public function itCanGetARelationValue() + public function it_can_get_a_relation_value() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ $mock = $this->getMockForTrait(HasRelations::class); - $data = new Item(); + $data = new Item; $mock->setRelation('foo', $data); @@ -148,7 +148,7 @@ public function itCanGetARelationValue() /** * @test */ - public function itReturnsNullWhenGettingAnUnexistingRelationValue() + public function it_returns_null_when_getting_an_unexisting_relation_value() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ $mock = $this->getMockForTrait(HasRelations::class); @@ -159,11 +159,11 @@ public function itReturnsNullWhenGettingAnUnexistingRelationValue() /** * @test */ - public function itReturnsABooleanIndicatingIfItHasARelation() + public function it_returns_a_boolean_indicating_if_it_has_a_relation() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ $mock = $this->getMockForTrait(HasRelations::class); - $data = new Item(); + $data = new Item; $this->assertFalse($mock->hasRelation('foo')); @@ -175,11 +175,11 @@ public function itReturnsABooleanIndicatingIfItHasARelation() /** * @test */ - public function itCanUnsetARelation() + public function it_can_unset_a_relation() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ $mock = $this->getMockForTrait(HasRelations::class); - $data = new Item(); + $data = new Item; $mock->setRelation('foo', $data); $this->assertNotNull($mock->getRelation('foo')); @@ -192,7 +192,7 @@ public function itCanUnsetARelation() /** * @test */ - public function itCanDefineAHasOneRelation() + public function it_can_define_a_has_one_relation() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ $mock = $this->getMockForTrait(HasRelations::class); @@ -206,7 +206,7 @@ public function itCanDefineAHasOneRelation() /** * @test */ - public function itCanDefineAHasOneRelationWithTheCallingMethodAsFallbackName() + public function it_can_define_a_has_one_relation_with_the_calling_method_as_fallback_name() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ $mock = $this->getMockForTrait(HasRelations::class); @@ -220,7 +220,7 @@ public function itCanDefineAHasOneRelationWithTheCallingMethodAsFallbackName() /** * @test */ - public function itCanDefineAHasManyRelation() + public function it_can_define_a_has_many_relation() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ $mock = $this->getMockForTrait(HasRelations::class); @@ -234,7 +234,7 @@ public function itCanDefineAHasManyRelation() /** * @test */ - public function itCanDefineAHasManyRelationWithTheCallingMethodAsFallbackName() + public function it_can_define_a_has_many_relation_with_the_calling_method_as_fallback_name() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ $mock = $this->getMockForTrait(HasRelations::class); @@ -248,7 +248,7 @@ public function itCanDefineAHasManyRelationWithTheCallingMethodAsFallbackName() /** * @test */ - public function itCanDefineAMorphToRelation() + public function it_can_define_a_morph_to_relation() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ $mock = $this->getMockForTrait(HasRelations::class); @@ -262,7 +262,7 @@ public function itCanDefineAMorphToRelation() /** * @test */ - public function itCanDefineAMorphToRelationWithTheCallingMethodAsFallbackName() + public function it_can_define_a_morph_to_relation_with_the_calling_method_as_fallback_name() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ $mock = $this->getMockForTrait(HasRelations::class); @@ -276,7 +276,7 @@ public function itCanDefineAMorphToRelationWithTheCallingMethodAsFallbackName() /** * @test */ - public function itCanDefineAMorphToManyRelation() + public function it_can_define_a_morph_to_many_relation() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ $mock = $this->getMockForTrait(HasRelations::class); @@ -290,7 +290,7 @@ public function itCanDefineAMorphToManyRelation() /** * @test */ - public function itCanDefineAMorphToManyRelationWithTheCallingMethodAsFallbackName() + public function it_can_define_a_morph_to_many_relation_with_the_calling_method_as_fallback_name() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasRelations $mock */ $mock = $this->getMockForTrait(HasRelations::class); diff --git a/tests/Concerns/HasTypeTest.php b/tests/Concerns/HasTypeTest.php index 1d42ab8..988ab65 100644 --- a/tests/Concerns/HasTypeTest.php +++ b/tests/Concerns/HasTypeTest.php @@ -12,7 +12,7 @@ class HasTypeTest extends TestCase /** * @test */ - public function itCanGetAndSetAType() + public function it_can_get_and_set_a_type() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasType $mock */ $mock = $this->getMockForTrait(HasType::class); @@ -26,7 +26,7 @@ public function itCanGetAndSetAType() /** * @test */ - public function itReturnsABooleanIndicatingIfItHasAType() + public function it_returns_a_boolean_indicating_if_it_has_a_type() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Concerns\HasType $mock */ $mock = $this->getMockForTrait(HasType::class); diff --git a/tests/DocumentClientTest.php b/tests/DocumentClientTest.php index c8025e0..cd65556 100644 --- a/tests/DocumentClientTest.php +++ b/tests/DocumentClientTest.php @@ -18,7 +18,7 @@ class DocumentClientTest extends TestCase /** * @test */ - public function itCanCreateAnInstanceUsingAFactoryMethod() + public function it_can_create_an_instance_using_a_factory_method() { $this->assertInstanceOf(DocumentClient::class, DocumentClient::create()); } @@ -26,7 +26,7 @@ public function itCanCreateAnInstanceUsingAFactoryMethod() /** * @test */ - public function theBaseUrlCanBeChangedAfterInstantiation() + public function the_base_url_can_be_changed_after_instantiation() { /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\ClientInterface $client */ $client = $this->createMock(ClientInterface::class); @@ -51,10 +51,10 @@ public function theBaseUrlCanBeChangedAfterInstantiation() /** * @test */ - public function itBuildsAGetRequest() + public function it_builds_a_get_request() { $response = $this->createMock(ResponseInterface::class); - $document = new Document(); + $document = new Document; /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\ClientInterface $client */ $client = $this->createMock(ClientInterface::class); @@ -82,10 +82,10 @@ public function itBuildsAGetRequest() /** * @test */ - public function itBuildsADeleteRequest() + public function it_builds_a_delete_request() { $response = $this->createMock(ResponseInterface::class); - $document = new Document(); + $document = new Document; /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\ClientInterface $client */ $client = $this->createMock(ClientInterface::class); @@ -113,12 +113,12 @@ public function itBuildsADeleteRequest() /** * @test */ - public function itBuildsAPatchRequest() + public function it_builds_a_patch_request() { $response = $this->createMock(ResponseInterface::class); - $document = new Document(); - $itemDocument = new ItemDocument(); - $itemDocument->setData((new Item())->setType('test')->setId('1')); + $document = new Document; + $itemDocument = new ItemDocument; + $itemDocument->setData((new Item)->setType('test')->setId('1')); /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\ClientInterface $client */ $client = $this->createMock(ClientInterface::class); @@ -146,12 +146,12 @@ public function itBuildsAPatchRequest() /** * @test */ - public function itBuildsAPostRequest() + public function it_builds_a_post_request() { $response = $this->createMock(ResponseInterface::class); - $document = new Document(); - $itemDocument = new ItemDocument(); - $itemDocument->setData((new Item())->setType('test')); + $document = new Document; + $itemDocument = new ItemDocument; + $itemDocument->setData((new Item)->setType('test')); /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\ClientInterface $client */ $client = $this->createMock(ClientInterface::class); diff --git a/tests/DocumentFactoryTest.php b/tests/DocumentFactoryTest.php index cb7286e..8e69b22 100644 --- a/tests/DocumentFactoryTest.php +++ b/tests/DocumentFactoryTest.php @@ -16,11 +16,11 @@ class DocumentFactoryTest extends TestCase /** * @test */ - public function itMakesAnItemdocumentForAnItem() + public function it_makes_an_itemdocument_for_an_item() { $item = (new Item(['foo' => 'bar']))->setId('123'); - $documentFactory = new DocumentFactory(); + $documentFactory = new DocumentFactory; $document = $documentFactory->make($item); @@ -31,12 +31,12 @@ public function itMakesAnItemdocumentForAnItem() /** * @test */ - public function itMakesACollectiondocumentForACollection() + public function it_makes_a_collectiondocument_for_a_collection() { $item = (new Item(['foo' => 'bar']))->setId('123'); $collection = new Collection([$item]); - $documentFactory = new DocumentFactory(); + $documentFactory = new DocumentFactory; $document = $documentFactory->make($collection); @@ -47,13 +47,13 @@ public function itMakesACollectiondocumentForACollection() /** * @test */ - public function itAddsIncludedToTheDocumentForAnItem() + public function it_adds_included_to_the_document_for_an_item() { $item = (new Item(['foo' => 'bar']))->setType('foo-bar')->setId('123'); $childItem = (new Item(['foo' => 'bar']))->setType('foo-bar')->setId('456'); $item->setRelation('child', $childItem); - $documentFactory = new DocumentFactory(); + $documentFactory = new DocumentFactory; $document = $documentFactory->make($item); @@ -63,14 +63,14 @@ public function itAddsIncludedToTheDocumentForAnItem() /** * @test */ - public function itAddsIncludedToTheDocumentForACollection() + public function it_adds_included_to_the_document_for_a_collection() { $item = (new Item(['foo' => 'bar']))->setType('foo-bar')->setId('123'); $childItem = (new Item(['foo' => 'bar']))->setType('foo-bar')->setId('456'); $item->setRelation('child', $childItem); $collection = new Collection([$item]); - $documentFactory = new DocumentFactory(); + $documentFactory = new DocumentFactory; $document = $documentFactory->make($collection); @@ -80,13 +80,13 @@ public function itAddsIncludedToTheDocumentForACollection() /** * @test */ - public function itAddsIncludedToTheDocumentForSingularRelations() + public function it_adds_included_to_the_document_for_singular_relations() { $item = (new Item(['foo' => 'bar']))->setType('foo-bar')->setId('123'); $childItem = (new Item(['foo' => 'bar']))->setType('foo-bar')->setId('456'); $item->setRelation('child', $childItem); - $documentFactory = new DocumentFactory(); + $documentFactory = new DocumentFactory; $document = $documentFactory->make($item); @@ -96,14 +96,14 @@ public function itAddsIncludedToTheDocumentForSingularRelations() /** * @test */ - public function itAddsIncludedToTheDocumentForPluralRelations() + public function it_adds_included_to_the_document_for_plural_relations() { $item = (new Item(['foo' => 'bar']))->setType('foo-bar')->setId('123'); $childItem = (new Item(['foo' => 'bar']))->setType('foo-bar')->setId('456'); $childTwoItem = (new Item(['foo' => 'bar']))->setType('foo-bar')->setId('789'); $item->setRelation('child', new Collection([$childItem, $childTwoItem])); - $documentFactory = new DocumentFactory(); + $documentFactory = new DocumentFactory; $document = $documentFactory->make($item); @@ -114,13 +114,13 @@ public function itAddsIncludedToTheDocumentForPluralRelations() /** * @test */ - public function itDoesNotAddIncludedToTheDocumentIfItHasNoType() + public function it_does_not_add_included_to_the_document_if_it_has_no_type() { $item = (new Item(['foo' => 'bar']))->setType('foo-bar')->setId('123'); $childItem = (new Item(['foo' => 'bar']))->setId('456'); $item->setRelation('child', $childItem); - $documentFactory = new DocumentFactory(); + $documentFactory = new DocumentFactory; $document = $documentFactory->make($item); @@ -130,13 +130,13 @@ public function itDoesNotAddIncludedToTheDocumentIfItHasNoType() /** * @test */ - public function itDoesNotAddIncludedToTheDocumentIfItHasNoId() + public function it_does_not_add_included_to_the_document_if_it_has_no_id() { $item = (new Item(['foo' => 'bar']))->setType('foo-bar')->setId('123'); $childItem = (new Item(['foo' => 'bar']))->setType('foo-bar'); $item->setRelation('child', $childItem); - $documentFactory = new DocumentFactory(); + $documentFactory = new DocumentFactory; $document = $documentFactory->make($item); @@ -146,13 +146,13 @@ public function itDoesNotAddIncludedToTheDocumentIfItHasNoId() /** * @test */ - public function itDoesNotAddIncludedToTheDocumentIfItHasNoAttributesOrRelationships() + public function it_does_not_add_included_to_the_document_if_it_has_no_attributes_or_relationships() { $item = (new Item(['foo' => 'bar']))->setType('foo-bar')->setId('123'); - $childItem = (new Item())->setType('foo-bar')->setId('456'); + $childItem = (new Item)->setType('foo-bar')->setId('456'); $item->setRelation('child', $childItem); - $documentFactory = new DocumentFactory(); + $documentFactory = new DocumentFactory; $document = $documentFactory->make($item); @@ -162,13 +162,13 @@ public function itDoesNotAddIncludedToTheDocumentIfItHasNoAttributesOrRelationsh /** * @test */ - public function itAddsIncludedToTheDocumentIfItHasATypeIdAndAttributesButNoRelationships() + public function it_adds_included_to_the_document_if_it_has_a_type_id_and_attributes_but_no_relationships() { $item = (new Item(['foo' => 'bar']))->setType('foo-bar')->setId('123'); $childItem = (new Item(['foo' => 'bar']))->setType('foo-bar')->setId('456'); $item->setRelation('child', $childItem); - $documentFactory = new DocumentFactory(); + $documentFactory = new DocumentFactory; $document = $documentFactory->make($item); @@ -178,15 +178,15 @@ public function itAddsIncludedToTheDocumentIfItHasATypeIdAndAttributesButNoRelat /** * @test */ - public function itAddsIncludedToTheDocumentIfItHasATypeIdAndRelationshipsButNoAttributes() + public function it_adds_included_to_the_document_if_it_has_a_type_id_and_relationships_but_no_attributes() { $item = (new Item(['foo' => 'bar']))->setType('foo-bar')->setId('123'); - $childItem = (new Item())->setType('foo-bar')->setId('456'); + $childItem = (new Item)->setType('foo-bar')->setId('456'); $item->setRelation('child', $childItem); $anotherChildItem = (new Item(['foo' => 'bar']))->setType('foo-bar')->setId('789'); $childItem->setRelation('child', $anotherChildItem); - $documentFactory = new DocumentFactory(); + $documentFactory = new DocumentFactory; $document = $documentFactory->make($item); @@ -196,14 +196,14 @@ public function itAddsIncludedToTheDocumentIfItHasATypeIdAndRelationshipsButNoAt /** * @test */ - public function itDoesNotAddIncludedToTheDocumentIfTheRelationshipShouldBeOmitted() + public function it_does_not_add_included_to_the_document_if_the_relationship_should_be_omitted() { $item = (new Item(['foo' => 'bar']))->setType('foo-bar')->setId('123'); $childItem = (new Item(['foo' => 'bar']))->setType('foo-bar')->setId('456'); $item->setRelation('child', $childItem); $item->getRelation('child')->setOmitIncluded(true); - $documentFactory = new DocumentFactory(); + $documentFactory = new DocumentFactory; $document = $documentFactory->make($item); diff --git a/tests/DocumentTest.php b/tests/DocumentTest.php index 6988c51..a81ce94 100644 --- a/tests/DocumentTest.php +++ b/tests/DocumentTest.php @@ -20,10 +20,10 @@ class DocumentTest extends TestCase /** * @test */ - public function itCanGetAndSetAResponse() + public function it_can_get_and_set_a_response() { - $document = new Document(); - $response = new Response(); + $document = new Document; + $response = new Response; $document->setResponse($response); @@ -33,10 +33,10 @@ public function itCanGetAndSetAResponse() /** * @test */ - public function itCanGetAndSetErrors() + public function it_can_get_and_set_errors() { - $document = new Document(); - $errors = new ErrorCollection(); + $document = new Document; + $errors = new ErrorCollection; $document->setErrors($errors); @@ -46,9 +46,9 @@ public function itCanGetAndSetErrors() /** * @test */ - public function itReturnsABooleanIndicatingIfItHasErrors() + public function it_returns_a_boolean_indicating_if_it_has_errors() { - $document = new Document(); + $document = new Document; $this->assertEquals($document->isSuccess(), true); $this->assertEquals($document->hasErrors(), false); @@ -66,10 +66,10 @@ public function itReturnsABooleanIndicatingIfItHasErrors() /** * @test */ - public function itCanGetAndSetIncluded() + public function it_can_get_and_set_included() { - $document = new Document(); - $included = new Collection(); + $document = new Document; + $included = new Collection; $document->setIncluded($included); @@ -79,10 +79,10 @@ public function itCanGetAndSetIncluded() /** * @test */ - public function itCanGetAndSetJsonapi() + public function it_can_get_and_set_jsonapi() { - $document = new Document(); - $jsonApi = new Jsonapi(); + $document = new Document; + $jsonApi = new Jsonapi; $document->setJsonapi($jsonApi); @@ -92,10 +92,10 @@ public function itCanGetAndSetJsonapi() /** * @test */ - public function itCanGetAndSetAnItemAsData() + public function it_can_get_and_set_an_item_as_data() { - $document = new Document(); - $data = new Item(); + $document = new Document; + $data = new Item; $document->setData($data); @@ -105,10 +105,10 @@ public function itCanGetAndSetAnItemAsData() /** * @test */ - public function itCanGetAndSetACollectionAsData() + public function it_can_get_and_set_a_collection_as_data() { - $document = new Document(); - $data = new Collection([new Item()]); + $document = new Document; + $data = new Collection([new Item]); $document->setData($data); @@ -118,9 +118,9 @@ public function itCanGetAndSetACollectionAsData() /** * @test */ - public function itReturnsOnlyFilledPropertiesInToArray() + public function it_returns_only_filled_properties_in_to_array() { - $document = new Document(); + $document = new Document; $this->assertEquals([], $document->toArray()); @@ -235,16 +235,16 @@ public function itReturnsOnlyFilledPropertiesInToArray() /** * @test */ - public function itSerializesEmptyMembersAsEmptyObjects() + public function it_serializes_empty_members_as_empty_objects() { - $document = new Document(); + $document = new Document; $this->assertEquals('{}', json_encode($document)); - $document->setData(new Collection()); - $document->setErrors(new ErrorCollection()); - $document->setIncluded(new Collection()); - $document->setJsonapi(new Jsonapi()); + $document->setData(new Collection); + $document->setErrors(new ErrorCollection); + $document->setIncluded(new Collection); + $document->setJsonapi(new Jsonapi); $document->setLinks(new Links([])); $document->setMeta(new Meta([])); diff --git a/tests/ItemHydratorTest.php b/tests/ItemHydratorTest.php index a390507..55cef14 100644 --- a/tests/ItemHydratorTest.php +++ b/tests/ItemHydratorTest.php @@ -26,7 +26,7 @@ class ItemHydratorTest extends TestCase */ private function getItemHydrator() { - $typeMapper = new TypeMapper(); + $typeMapper = new TypeMapper; $typeMapper->setMapping('hydratedItem', Item::class); $typeMapper->setMapping('related-item', RelatedItem::class); @@ -38,7 +38,7 @@ private function getItemHydrator() /** * @test */ - public function itHydratesAttributes() + public function it_hydrates_attributes() { $data = [ 'testattribute1' => 'test', @@ -49,7 +49,7 @@ public function itHydratesAttributes() 'testarray' => ['1', '2', '3'], ]; - $item = new Item(); + $item = new Item; $item = $this->getItemHydrator()->hydrate($item, $data); $this->assertEquals($data, $item->getAttributes()); @@ -58,13 +58,13 @@ public function itHydratesAttributes() /** * @test */ - public function itHydratesHasoneRelationshipsById() + public function it_hydrates_hasone_relationships_by_id() { $data = [ 'hasone_relation' => '1', ]; - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item = $this->getItemHydrator()->hydrate($item, $data); /** @var \Swis\JsonApi\Client\Relations\HasOneRelation $hasOne */ @@ -80,7 +80,7 @@ public function itHydratesHasoneRelationshipsById() /** * @test */ - public function itHydratesHasoneRelationshipsWithAttributes() + public function it_hydrates_hasone_relationships_with_attributes() { $data = [ 'hasone_relation' => [ @@ -90,7 +90,7 @@ public function itHydratesHasoneRelationshipsWithAttributes() ], ]; - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item = $this->getItemHydrator()->hydrate($item, $data); /** @var \Swis\JsonApi\Client\Relations\HasOneRelation $hasOne */ @@ -108,13 +108,13 @@ public function itHydratesHasoneRelationshipsWithAttributes() /** * @test */ - public function itDissociatesHasoneRelationshipsWhenNull() + public function it_dissociates_hasone_relationships_when_null() { $data = [ 'hasone_relation' => null, ]; - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item = $this->getItemHydrator()->hydrate($item, $data); /** @var \Swis\JsonApi\Client\Relations\HasOneRelation $hasOne */ @@ -130,7 +130,7 @@ public function itDissociatesHasoneRelationshipsWhenNull() /** * @test */ - public function itHydratesHasmanyRelationshipsById() + public function it_hydrates_hasmany_relationships_by_id() { $data = [ 'hasmany_relation' => [ @@ -139,7 +139,7 @@ public function itHydratesHasmanyRelationshipsById() ], ]; - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item = $this->getItemHydrator()->hydrate($item, $data); /** @var \Swis\JsonApi\Client\Relations\HasManyRelation $hasMany */ @@ -160,7 +160,7 @@ public function itHydratesHasmanyRelationshipsById() /** * @test */ - public function itHydratesHasmanyRelationshipsWithAttributes() + public function it_hydrates_hasmany_relationships_with_attributes() { $data = [ 'hasmany_relation' => [ @@ -177,7 +177,7 @@ public function itHydratesHasmanyRelationshipsWithAttributes() ], ]; - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item = $this->getItemHydrator()->hydrate($item, $data); /** @var \Swis\JsonApi\Client\Relations\HasManyRelation $hasMany */ @@ -202,13 +202,13 @@ public function itHydratesHasmanyRelationshipsWithAttributes() /** * @test */ - public function itDissociatesHasmanyRelationshipsWhenEmptyArray() + public function it_dissociates_hasmany_relationships_when_empty_array() { $data = [ 'hasmany_relation' => [], ]; - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item = $this->getItemHydrator()->hydrate($item, $data); /** @var \Swis\JsonApi\Client\Relations\HasManyRelation $hasMany */ @@ -224,7 +224,7 @@ public function itDissociatesHasmanyRelationshipsWhenEmptyArray() /** * @test */ - public function itHydratesMorphtoRelationshipsById() + public function it_hydrates_morphto_relationships_by_id() { $data = [ 'morphto_relation' => [ @@ -233,7 +233,7 @@ public function itHydratesMorphtoRelationshipsById() ], ]; - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item = $this->getItemHydrator()->hydrate($item, $data); /** @var \Swis\JsonApi\Client\Relations\MorphToRelation $morphTo */ @@ -250,7 +250,7 @@ public function itHydratesMorphtoRelationshipsById() /** * @test */ - public function itHydratesMorphtoRelationshipsWithAttributes() + public function it_hydrates_morphto_relationships_with_attributes() { $data = [ 'morphto_relation' => [ @@ -260,7 +260,7 @@ public function itHydratesMorphtoRelationshipsWithAttributes() ], ]; - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item = $this->getItemHydrator()->hydrate($item, $data); /** @var \Swis\JsonApi\Client\Relations\MorphToRelation $morphTo */ @@ -278,13 +278,13 @@ public function itHydratesMorphtoRelationshipsWithAttributes() /** * @test */ - public function itDissociatesMorphtoRelationshipsWhenNull() + public function it_dissociates_morphto_relationships_when_null() { $data = [ 'morphto_relation' => null, ]; - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item = $this->getItemHydrator()->hydrate($item, $data); /** @var \Swis\JsonApi\Client\Relations\MorphToRelation $morphTo */ @@ -300,7 +300,7 @@ public function itDissociatesMorphtoRelationshipsWhenNull() /** * @test */ - public function itHydratesMorphtoRelationshipsWithUnmappedItems() + public function it_hydrates_morphto_relationships_with_unmapped_items() { $data = [ 'morphto_relation' => [ @@ -309,7 +309,7 @@ public function itHydratesMorphtoRelationshipsWithUnmappedItems() ], ]; - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item = $this->getItemHydrator()->hydrate($item, $data); /** @var \Swis\JsonApi\Client\Relations\MorphToRelation $morphTo */ @@ -324,7 +324,7 @@ public function itHydratesMorphtoRelationshipsWithUnmappedItems() /** * @test */ - public function itThrowsForMorphtoRelationshipsWithoutTypeAttribute() + public function it_throws_for_morphto_relationships_without_type_attribute() { $data = [ 'morphto_relation' => [ @@ -333,7 +333,7 @@ public function itThrowsForMorphtoRelationshipsWithoutTypeAttribute() ], ]; - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $this->expectException(HydrationException::class); $this->getItemHydrator()->hydrate($item, $data); @@ -342,7 +342,7 @@ public function itThrowsForMorphtoRelationshipsWithoutTypeAttribute() /** * @test */ - public function itHydratesMorphtomanyRelationshipsById() + public function it_hydrates_morphtomany_relationships_by_id() { $data = [ 'morphtomany_relation' => [ @@ -357,7 +357,7 @@ public function itHydratesMorphtomanyRelationshipsById() ], ]; - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item = $this->getItemHydrator()->hydrate($item, $data); /** @var \Swis\JsonApi\Client\Relations\MorphToManyRelation $morphToMany */ @@ -380,7 +380,7 @@ public function itHydratesMorphtomanyRelationshipsById() /** * @test */ - public function itHydratesMorphtomanyRelationshipsWithAttributes() + public function it_hydrates_morphtomany_relationships_with_attributes() { $data = [ 'morphtomany_relation' => [ @@ -397,7 +397,7 @@ public function itHydratesMorphtomanyRelationshipsWithAttributes() ], ]; - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item = $this->getItemHydrator()->hydrate($item, $data); /** @var \Swis\JsonApi\Client\Relations\MorphToManyRelation $morphToMany */ @@ -422,13 +422,13 @@ public function itHydratesMorphtomanyRelationshipsWithAttributes() /** * @test */ - public function itDissociatesMorphtomanyRelationshipsWhenEmptyArray() + public function it_dissociates_morphtomany_relationships_when_empty_array() { $data = [ 'morphtomany_relation' => [], ]; - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item = $this->getItemHydrator()->hydrate($item, $data); /** @var \Swis\JsonApi\Client\Relations\MorphToManyRelation $morphToMany */ @@ -444,7 +444,7 @@ public function itDissociatesMorphtomanyRelationshipsWhenEmptyArray() /** * @test */ - public function itHydratesMorphtomanyRelationshipsWithUnmappedItems() + public function it_hydrates_morphtomany_relationships_with_unmapped_items() { $data = [ 'morphtomany_relation' => [ @@ -459,7 +459,7 @@ public function itHydratesMorphtomanyRelationshipsWithUnmappedItems() ], ]; - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item = $this->getItemHydrator()->hydrate($item, $data); /** @var \Swis\JsonApi\Client\Relations\MorphToManyRelation $morphToMany */ @@ -479,7 +479,7 @@ public function itHydratesMorphtomanyRelationshipsWithUnmappedItems() /** * @test */ - public function itThrowsForMorphtomanyRelationshipsWithoutTypeAttribute() + public function it_throws_for_morphtomany_relationships_without_type_attribute() { $data = [ 'morphtomany_relation' => [ @@ -490,7 +490,7 @@ public function itThrowsForMorphtomanyRelationshipsWithoutTypeAttribute() ], ]; - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $this->expectException(HydrationException::class); $this->getItemHydrator()->hydrate($item, $data); @@ -499,7 +499,7 @@ public function itThrowsForMorphtomanyRelationshipsWithoutTypeAttribute() /** * @test */ - public function itHydratesNestedRelationshipItems() + public function it_hydrates_nested_relationship_items() { $data = [ 'hasone_relation' => [ @@ -508,7 +508,7 @@ public function itHydratesNestedRelationshipItems() ], ]; - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item = $this->getItemHydrator()->hydrate($item, $data); /** @var \Swis\JsonApi\Client\Relations\HasOneRelation $hasOne */ @@ -529,7 +529,7 @@ public function itHydratesNestedRelationshipItems() /** * @test */ - public function itDissociatesAndHydratesRelationshipsInOneCall() + public function it_dissociates_and_hydrates_relationships_in_one_call() { $data = [ 'hasone_relation' => null, @@ -539,7 +539,7 @@ public function itDissociatesAndHydratesRelationshipsInOneCall() ], ]; - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item = $this->getItemHydrator()->hydrate($item, $data); /** @var \Swis\JsonApi\Client\Relations\MorphToRelation $morphTo */ @@ -557,13 +557,10 @@ public function itDissociatesAndHydratesRelationshipsInOneCall() * @test * * @dataProvider provideIdArguments - * - * @param $givenId - * @param $expectedId */ - public function itHydratesTheIdWhenNotNullOrEmptyString($givenId, $expectedId) + public function it_hydrates_the_id_when_not_null_or_empty_string($givenId, $expectedId) { - $item = new Item(); + $item = new Item; $item = $this->getItemHydrator()->hydrate($item, [], $givenId); static::assertSame($expectedId, $item->getId()); @@ -583,13 +580,13 @@ public function provideIdArguments(): array /** * @test */ - public function itThrowsWhenRelationshipIsPresentInAvailableRelationshipsButTheMethodDoesNotExist() + public function it_throws_when_relationship_is_present_in_available_relationships_but_the_method_does_not_exist() { $data = [ 'does_not_exist' => '1', ]; - $item = new ParentItem(); + $item = new ParentItem; $this->expectException(HydrationException::class); $this->expectExceptionMessage(sprintf('Method doesNotExist not found on %s', ParentItem::class)); diff --git a/tests/ItemTest.php b/tests/ItemTest.php index 7c9cbde..5d13ab5 100644 --- a/tests/ItemTest.php +++ b/tests/ItemTest.php @@ -23,7 +23,7 @@ class ItemTest extends TestCase /** * @test */ - public function itCanInstantiateAnItem() + public function it_can_instantiate_an_item() { $item = new Item(['name' => 'john']); $this->assertInstanceOf(Item::class, $item); @@ -33,9 +33,9 @@ public function itCanInstantiateAnItem() /** * @test */ - public function itCanCreateANewInstanceWithAttributes() + public function it_can_create_a_new_instance_with_attributes() { - $item = new ItemStub(); + $item = new ItemStub; $item->setType('foo-bar'); $instance = $item->newInstance(['name' => 'john']); @@ -47,7 +47,7 @@ public function itCanCreateANewInstanceWithAttributes() /** * @test */ - public function isShowsTypeAndIdAndAttributesInToJsonApiArray() + public function is_shows_type_and_id_and_attributes_in_to_json_api_array() { $attributes = [ 'testKey' => 'testValue', @@ -74,7 +74,7 @@ public function isShowsTypeAndIdAndAttributesInToJsonApiArray() /** * @test */ - public function isDoesNotShowAttributesInToJsonApiArrayWhenItHasNoAttributes() + public function is_does_not_show_attributes_in_to_json_api_array_when_it_has_no_attributes() { $item = new WithHiddenItem(['testKey' => 'testValue']); $item->setType('testType'); @@ -92,11 +92,11 @@ public function isDoesNotShowAttributesInToJsonApiArrayWhenItHasNoAttributes() /** * @test */ - public function isAddsHasoneRelationInToJsonApiArray() + public function is_adds_hasone_relation_in_to_json_api_array() { - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item->setId('1234'); - $item->hasoneRelation()->associate((new RelatedItem())->setId('5678')); + $item->hasoneRelation()->associate((new RelatedItem)->setId('5678')); $item->hasoneRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')])); $item->hasoneRelation()->setMeta(new Meta(['foo' => 'bar'])); @@ -128,9 +128,9 @@ public function isAddsHasoneRelationInToJsonApiArray() /** * @test */ - public function isAddsEmptyHasoneRelationInToJsonApiArray() + public function is_adds_empty_hasone_relation_in_to_json_api_array() { - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item->setId('1234'); $item->hasoneRelation()->dissociate(); @@ -151,9 +151,9 @@ public function isAddsEmptyHasoneRelationInToJsonApiArray() /** * @test */ - public function isDoesNotAddHasoneRelationWithoutDataInToJsonApiArray() + public function is_does_not_add_hasone_relation_without_data_in_to_json_api_array() { - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item->setId('1234'); $item->hasoneRelation(); $item->hasoneRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')])); @@ -171,11 +171,11 @@ public function isDoesNotAddHasoneRelationWithoutDataInToJsonApiArray() /** * @test */ - public function isAddsHasmanyRelationInToJsonApiArray() + public function is_adds_hasmany_relation_in_to_json_api_array() { - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item->setId('1234'); - $item->hasmanyRelation()->associate(new Collection([(new RelatedItem())->setId('5678')])); + $item->hasmanyRelation()->associate(new Collection([(new RelatedItem)->setId('5678')])); $item->hasmanyRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')])); $item->hasmanyRelation()->setMeta(new Meta(['foo' => 'bar'])); @@ -209,9 +209,9 @@ public function isAddsHasmanyRelationInToJsonApiArray() /** * @test */ - public function isAddsEmptyHasmanyRelationInToJsonApiArray() + public function is_adds_empty_hasmany_relation_in_to_json_api_array() { - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item->setId('1234'); $item->hasmanyRelation()->dissociate(); @@ -232,9 +232,9 @@ public function isAddsEmptyHasmanyRelationInToJsonApiArray() /** * @test */ - public function isDoesNotAddHasmanyRelationWithoutDataInToJsonApiArray() + public function is_does_not_add_hasmany_relation_without_data_in_to_json_api_array() { - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item->setId('1234'); $item->hasmanyRelation(); $item->hasmanyRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')])); @@ -252,11 +252,11 @@ public function isDoesNotAddHasmanyRelationWithoutDataInToJsonApiArray() /** * @test */ - public function isAddsMorphtoRelationInToJsonApiArray() + public function is_adds_morphto_relation_in_to_json_api_array() { - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item->setId('1234'); - $item->morphtoRelation()->associate((new RelatedItem())->setId('5678')); + $item->morphtoRelation()->associate((new RelatedItem)->setId('5678')); $item->morphtoRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')])); $item->morphtoRelation()->setMeta(new Meta(['foo' => 'bar'])); @@ -288,9 +288,9 @@ public function isAddsMorphtoRelationInToJsonApiArray() /** * @test */ - public function isAddsEmptyMorphtoRelationInToJsonApiArray() + public function is_adds_empty_morphto_relation_in_to_json_api_array() { - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item->setId('1234'); $item->morphtoRelation()->dissociate(); @@ -311,9 +311,9 @@ public function isAddsEmptyMorphtoRelationInToJsonApiArray() /** * @test */ - public function isDoesNotAddMorphtoRelationWithoutDataInToJsonApiArray() + public function is_does_not_add_morphto_relation_without_data_in_to_json_api_array() { - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item->setId('1234'); $item->morphtoRelation(); $item->morphtoRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')])); @@ -331,11 +331,11 @@ public function isDoesNotAddMorphtoRelationWithoutDataInToJsonApiArray() /** * @test */ - public function isAddsMorphtomanyRelationInToJsonApiArray() + public function is_adds_morphtomany_relation_in_to_json_api_array() { - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item->setId('1234'); - $item->morphtomanyRelation()->associate(new Collection([(new RelatedItem())->setId('5678')])); + $item->morphtomanyRelation()->associate(new Collection([(new RelatedItem)->setId('5678')])); $item->morphtomanyRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')])); $item->morphtomanyRelation()->setMeta(new Meta(['foo' => 'bar'])); @@ -369,9 +369,9 @@ public function isAddsMorphtomanyRelationInToJsonApiArray() /** * @test */ - public function isAddsEmptyMorphtomanyRelationInToJsonApiArray() + public function is_adds_empty_morphtomany_relation_in_to_json_api_array() { - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item->setId('1234'); $item->morphtomanyRelation()->dissociate(); @@ -392,9 +392,9 @@ public function isAddsEmptyMorphtomanyRelationInToJsonApiArray() /** * @test */ - public function isDoesNotAddMorphtomanyRelationWithoutDataInToJsonApiArray() + public function is_does_not_add_morphtomany_relation_without_data_in_to_json_api_array() { - $item = new WithRelationshipItem(); + $item = new WithRelationshipItem; $item->setId('1234'); $item->morphtomanyRelation(); $item->morphtomanyRelation()->setLinks(new Links(['self' => new Link('http://example.com/articles')])); @@ -412,9 +412,9 @@ public function isDoesNotAddMorphtomanyRelationWithoutDataInToJsonApiArray() /** * @test */ - public function isAddsLinksInToJsonApiArray() + public function is_adds_links_in_to_json_api_array() { - $item = new Item(); + $item = new Item; $item->setType('testType'); $item->setId('1'); $item->setLinks( @@ -452,9 +452,9 @@ public function isAddsLinksInToJsonApiArray() /** * @test */ - public function isAddsMetaInToJsonApiArray() + public function is_adds_meta_in_to_json_api_array() { - $item = new Item(); + $item = new Item; $item->setType('testType'); $item->setId('1'); $item->setMeta(new Meta(['foo' => 'bar'])); @@ -474,9 +474,9 @@ public function isAddsMetaInToJsonApiArray() /** * @test */ - public function itIsNewWhenNoIdIsset() + public function it_is_new_when_no_id_isset() { - $item = new Item(); + $item = new Item; $item->setType('testType'); $this->assertTrue($item->isNew()); @@ -488,10 +488,10 @@ public function itIsNewWhenNoIdIsset() /** * @test */ - public function itCanGetARelationValueUsingGetAttributeMethod() + public function it_can_get_a_relation_value_using_get_attribute_method() { - $parentItem = new ParentItem(); - $childItem = new ChildItem(); + $parentItem = new ParentItem; + $childItem = new ChildItem; $parentItem->child()->associate($childItem); $this->assertSame($childItem, $parentItem->getAttribute('child')); @@ -500,7 +500,7 @@ public function itCanGetARelationValueUsingGetAttributeMethod() /** * @test */ - public function itReturnsAttributes() + public function it_returns_attributes() { $attributes = [ 'foo' => 'bar', @@ -512,9 +512,9 @@ public function itReturnsAttributes() /** * @test */ - public function itReturnsABooleanIndicatingIfItHasAttributes() + public function it_returns_a_boolean_indicating_if_it_has_attributes() { - $item = new Item(); + $item = new Item; $this->assertFalse($item->hasAttributes()); $item->fill(['foo' => 'bar']); @@ -525,10 +525,10 @@ public function itReturnsABooleanIndicatingIfItHasAttributes() /** * @test */ - public function itCanGetAllRelationships() + public function it_can_get_all_relationships() { - $parentItem = new ParentItem(); - $childItem = new ChildItem(); + $parentItem = new ParentItem; + $childItem = new ChildItem; $childItem->setId('1'); $childItem->setMeta(new Meta(['foo' => 'bar'])); $parentItem->child()->associate($childItem); @@ -563,12 +563,12 @@ public function itCanGetAllRelationships() /** * @test */ - public function itReturnsABooleanIndicatingIfItHasRelationships() + public function it_returns_a_boolean_indicating_if_it_has_relationships() { - $parentItem = new ParentItem(); + $parentItem = new ParentItem; $this->assertFalse($parentItem->hasRelationships()); - $childItem = (new ChildItem())->setId('1'); + $childItem = (new ChildItem)->setId('1'); $parentItem->child()->associate($childItem); $this->assertTrue($parentItem->hasRelationships()); @@ -577,9 +577,9 @@ public function itReturnsABooleanIndicatingIfItHasRelationships() /** * @test */ - public function itUsesInitialValues() + public function it_uses_initial_values() { - $itemBuilder = new Item(); + $itemBuilder = new Item; $itemBuilder->fill(['testKey' => 1, 'anotherTestKey' => 'someValue']); $itemBuilder->setInitial(['testKey' => 9999]); $itemBuilder->useInitial(); @@ -590,9 +590,9 @@ public function itUsesInitialValues() /** * @test */ - public function itCanSetTheIdUsingTheMagicMethod() + public function it_can_set_the_id_using_the_magic_method() { - $item = new Item(); + $item = new Item; $item->id = '1234'; $this->assertEquals('1234', $item->getId()); @@ -601,9 +601,9 @@ public function itCanSetTheIdUsingTheMagicMethod() /** * @test */ - public function itCanGetTheIdUsingTheMagicMethod() + public function it_can_get_the_id_using_the_magic_method() { - $item = new Item(); + $item = new Item; $item->setId('1234'); $this->assertEquals('1234', $item->id); @@ -612,9 +612,9 @@ public function itCanGetTheIdUsingTheMagicMethod() /** * @test */ - public function itCanCheckIfTheIdIsSetUsingTheMagicMethod() + public function it_can_check_if_the_id_is_set_using_the_magic_method() { - $item = new Item(); + $item = new Item; $this->assertFalse(isset($item->id)); $item->setId('1234'); @@ -624,9 +624,9 @@ public function itCanCheckIfTheIdIsSetUsingTheMagicMethod() /** * @test */ - public function itCanUnsetTheIdUsingTheMagicMethod() + public function it_can_unset_the_id_using_the_magic_method() { - $item = new Item(); + $item = new Item; $item->id = '1234'; unset($item->id); @@ -636,9 +636,9 @@ public function itCanUnsetTheIdUsingTheMagicMethod() /** * @test */ - public function itCanSetAnAttributeUsingTheMagicMethod() + public function it_can_set_an_attribute_using_the_magic_method() { - $item = new Item(); + $item = new Item; $item->foo = 'bar'; $this->assertEquals('bar', $item->getAttribute('foo')); @@ -647,9 +647,9 @@ public function itCanSetAnAttributeUsingTheMagicMethod() /** * @test */ - public function itCanGetAnAttributeUsingTheMagicMethod() + public function it_can_get_an_attribute_using_the_magic_method() { - $item = new Item(); + $item = new Item; $item->setAttribute('foo', 'bar'); $this->assertEquals('bar', $item->foo); @@ -658,9 +658,9 @@ public function itCanGetAnAttributeUsingTheMagicMethod() /** * @test */ - public function itCanCheckIfAnAttributeIsSetUsingTheMagicMethod() + public function it_can_check_if_an_attribute_is_set_using_the_magic_method() { - $item = new Item(); + $item = new Item; $this->assertFalse(isset($item->foo)); $item->setAttribute('foo', 'bar'); @@ -670,9 +670,9 @@ public function itCanCheckIfAnAttributeIsSetUsingTheMagicMethod() /** * @test */ - public function itCanUnsetAnAttributeUsingTheMagicMethod() + public function it_can_unset_an_attribute_using_the_magic_method() { - $item = new Item(); + $item = new Item; $item->setAttribute('foo', 'bar'); $this->assertNotNull($item->getAttribute('foo')); @@ -684,9 +684,9 @@ public function itCanUnsetAnAttributeUsingTheMagicMethod() /** * @test */ - public function itReturnsNullWhenProvidedAnEmptyKey() + public function it_returns_null_when_provided_an_empty_key() { - $item = new Item(); + $item = new Item; $this->assertNull($item->getAttribute('')); } @@ -694,9 +694,9 @@ public function itReturnsNullWhenProvidedAnEmptyKey() /** * @test */ - public function itReturnsNullWhenProvidedAKeyThatIsAMethodOnTheItem() + public function it_returns_null_when_provided_a_key_that_is_a_method_on_the_item() { - $item = new Item(); + $item = new Item; $this->assertNull($item->getAttribute('getAttribute')); } @@ -704,9 +704,9 @@ public function itReturnsNullWhenProvidedAKeyThatIsAMethodOnTheItem() /** * @test */ - public function itCanManipulateAttributes() + public function it_can_manipulate_attributes() { - $item = new ItemStub(); + $item = new ItemStub; $item->name = 'foo'; $this->assertEquals('foo', $item->name); @@ -724,9 +724,9 @@ public function itCanManipulateAttributes() /** * @test */ - public function itDoesNotShowHiddenAttributes() + public function it_does_not_show_hidden_attributes() { - $item = new ItemStub(); + $item = new ItemStub; $item->password = 'secret'; $attributes = $item->attributesToArray(); @@ -737,9 +737,9 @@ public function itDoesNotShowHiddenAttributes() /** * @test */ - public function itDoesShowVisibleAttributes() + public function it_does_show_visible_attributes() { - $item = new ItemStub(); + $item = new ItemStub; $item->setVisible(['name']); $item->name = 'John Doe'; $item->city = 'Paris'; @@ -751,9 +751,9 @@ public function itDoesShowVisibleAttributes() /** * @test */ - public function itCanReturnTheItemInArrayForm() + public function it_can_return_the_item_in_array_form() { - $item = new ItemStub(); + $item = new ItemStub; $item->name = 'foo'; $item->bar = null; $item->password = 'password1'; @@ -777,9 +777,9 @@ public function itCanReturnTheItemInArrayForm() /** * @test */ - public function itCanReturnASubsetOfTheItemInArrayForm() + public function it_can_return_a_subset_of_the_item_in_array_form() { - $item = new ItemStub(); + $item = new ItemStub; $item->name = 'foo'; $item->bar = null; $array = $item->only('name'); @@ -792,13 +792,13 @@ public function itCanReturnASubsetOfTheItemInArrayForm() /** * @test */ - public function itCanReturnTheItemInJsonForm() + public function it_can_return_the_item_in_json_form() { - $item = new ItemStub(); + $item = new ItemStub; $item->name = 'john'; $item->foo = 10; - $object = new \stdClass(); + $object = new \stdClass; $object->name = 'john'; $object->foo = 10; @@ -809,9 +809,9 @@ public function itCanReturnTheItemInJsonForm() /** * @test */ - public function itCanMutateAttributes() + public function it_can_mutate_attributes() { - $item = new ItemStub(); + $item = new ItemStub; $item->list_items = ['name' => 'john']; $this->assertEquals(['name' => 'john'], $item->list_items); $attributes = $item->getAttributes(); @@ -819,7 +819,7 @@ public function itCanMutateAttributes() $birthday = strtotime('245 months ago'); - $item = new ItemStub(); + $item = new ItemStub; $item->birthday = '245 months ago'; $this->assertEquals(date('Y-m-d', $birthday), $item->birthday); @@ -829,9 +829,9 @@ public function itCanMutateAttributes() /** * @test */ - public function itUsesMutatorsInToArray() + public function it_uses_mutators_in_to_array() { - $item = new ItemStub(); + $item = new ItemStub; $item->list_items = [1, 2, 3]; $array = $item->toArray(); @@ -841,9 +841,9 @@ public function itUsesMutatorsInToArray() /** * @test */ - public function itCanReplicate() + public function it_can_replicate() { - $item = new ItemStub(); + $item = new ItemStub; $item->name = 'John Doe'; $item->city = 'Paris'; @@ -855,13 +855,13 @@ public function itCanReplicate() /** * @test */ - public function itAppendsMutators() + public function it_appends_mutators() { - $item = new ItemStub(); + $item = new ItemStub; $array = $item->toArray(); $this->assertFalse(isset($array['test'])); - $item = new ItemStub(); + $item = new ItemStub; $item->setAppends(['test']); $array = $item->toArray(); $this->assertTrue(isset($array['test'])); @@ -871,9 +871,9 @@ public function itAppendsMutators() /** * @test */ - public function itCanMergeAppends() + public function it_can_merge_appends() { - $item = new ItemStub(); + $item = new ItemStub; $item->mergeAppends([__FUNCTION__]); $this->assertTrue($item->hasAppended(__FUNCTION__)); } @@ -881,9 +881,9 @@ public function itCanMergeAppends() /** * @test */ - public function itCanUseArrayAccess() + public function it_can_use_array_access() { - $item = new ItemStub(); + $item = new ItemStub; $item->name = 'John Doen'; $item['city'] = 'Paris'; @@ -894,9 +894,9 @@ public function itCanUseArrayAccess() /** * @test */ - public function itCanBeSerializedAndUnserialized() + public function it_can_be_serialized_and_unserialized() { - $item = new ItemStub(); + $item = new ItemStub; $item->name = 'john'; $item->foo = 10; @@ -907,9 +907,9 @@ public function itCanBeSerializedAndUnserialized() /** * @test */ - public function itCanCastAttributes() + public function it_can_cast_attributes() { - $item = new ItemStub(); + $item = new ItemStub; $item->score = '0.34'; $item->score_inf = 'Infinity'; $item->score_inf_neg = '-Infinity'; @@ -963,9 +963,9 @@ public function itCanCastAttributes() /** * @test */ - public function itCanMergeCasts() + public function it_can_merge_casts() { - $item = new ItemStub(); + $item = new ItemStub; $item->mergeCasts([__FUNCTION__ => 'int']); $this->assertArrayHasKey(__FUNCTION__, $item->getCasts()); } @@ -973,7 +973,7 @@ public function itCanMergeCasts() /** * @test */ - public function itCanGuardAttributes() + public function it_can_guard_attributes() { $item = new ItemStub(['secret' => 'foo']); $this->assertTrue($item->isGuarded('secret')); @@ -995,9 +995,9 @@ public function itCanGuardAttributes() /** * @test */ - public function itCanMergeGuarded() + public function it_can_merge_guarded() { - $item = new Item(); + $item = new Item; $item->guard([]); $this->assertFalse($item->isGuarded('foo')); @@ -1012,7 +1012,7 @@ public function itCanMergeGuarded() /** * @test */ - public function itCanUseTheGuardedCallback() + public function it_can_use_the_guarded_callback() { $mock = $this->getMockBuilder(\stdClass::class) ->addMethods(['callback']) @@ -1031,11 +1031,11 @@ public function itCanUseTheGuardedCallback() /** * @test */ - public function itCanBeTotallyGuarded() + public function it_can_be_totally_guarded() { $this->expectException(MassAssignmentException::class); - $item = new ItemStub(); + $item = new ItemStub; $item->guard(['*']); $item->fillable([]); $item->fill(['name' => 'John Doe']); @@ -1044,7 +1044,7 @@ public function itCanBeTotallyGuarded() /** * @test */ - public function itCanBeFillable() + public function it_can_be_fillable() { $item = new ItemStub(['foo' => 'bar']); $this->assertFalse($item->isFillable('foo')); @@ -1054,7 +1054,7 @@ public function itCanBeFillable() $item->foo = 'bar'; $this->assertEquals('bar', $item->foo); - $item = new ItemStub(); + $item = new ItemStub; $item->forceFill(['foo' => 'bar']); $this->assertEquals('bar', $item->foo); } @@ -1062,9 +1062,9 @@ public function itCanBeFillable() /** * @test */ - public function itCanMergeFillable() + public function it_can_merge_fillable() { - $item = new ItemStub(); + $item = new ItemStub; $item->fillable(['foo']); $item->mergeFillable(['bar']); $this->assertEquals(['foo', 'bar'], $item->getFillable()); @@ -1073,7 +1073,7 @@ public function itCanMergeFillable() /** * @test */ - public function itCanHydrateAnArrayOfAttributes() + public function it_can_hydrate_an_array_of_attributes() { $items = ItemStub::hydrate([['name' => 'John Doe']]); $this->assertInstanceOf(ItemStub::class, $items[0]); diff --git a/tests/LinksTest.php b/tests/LinksTest.php index c5eb60f..694dee3 100644 --- a/tests/LinksTest.php +++ b/tests/LinksTest.php @@ -10,7 +10,7 @@ class LinksTest extends TestCase { - public function testGet() + public function test_get() { $link = new Link('http://example.com/self'); $links = new Links(['self' => $link]); @@ -19,7 +19,7 @@ public function testGet() $this->assertNull($links->related); } - public function testOffsetGet() + public function test_offset_get() { $link = new Link('http://example.com/self'); $links = new Links(['self' => $link]); @@ -28,7 +28,7 @@ public function testOffsetGet() $this->assertNull($links['related']); } - public function testIsset() + public function test_isset() { $link = new Link('http://example.com/self'); $links = new Links(['self' => $link]); @@ -37,7 +37,7 @@ public function testIsset() $this->assertFalse(isset($links->related)); } - public function testOffsetExists() + public function test_offset_exists() { $link = new Link('http://example.com/self'); $links = new Links(['self' => $link]); @@ -46,7 +46,7 @@ public function testOffsetExists() $this->assertFalse(isset($links['related'])); } - public function testSet() + public function test_set() { $link = new Link('http://example.com/self'); $links = new Links([]); @@ -56,7 +56,7 @@ public function testSet() $this->assertEquals($link, $links->self); } - public function testOffsetSet() + public function test_offset_set() { $link = new Link('http://example.com/self'); $links = new Links([]); @@ -66,7 +66,7 @@ public function testOffsetSet() $this->assertEquals($link, $links['self']); } - public function testUnset() + public function test_unset() { $link = new Link('http://example.com/self'); $links = new Links(['self' => $link]); @@ -76,7 +76,7 @@ public function testUnset() $this->assertNull($links->self); } - public function testOffsetUnset() + public function test_offset_unset() { $link = new Link('http://example.com/self'); $links = new Links(['self' => $link]); @@ -86,7 +86,7 @@ public function testOffsetUnset() $this->assertNull($links['self']); } - public function testToArray() + public function test_to_array() { $link = new Link('http://example.com/self'); $links = new Links(['self' => $link]); @@ -101,7 +101,7 @@ public function testToArray() ); } - public function testToJson() + public function test_to_json() { $link = new Link('http://example.com/self'); $links = new Links(['self' => $link]); @@ -112,7 +112,7 @@ public function testToJson() ); } - public function testJsonSerialize() + public function test_json_serialize() { $link = new Link('http://example.com/self'); $links = new Links(['self' => $link]); diff --git a/tests/MetaTest.php b/tests/MetaTest.php index a33a664..c63e7a1 100644 --- a/tests/MetaTest.php +++ b/tests/MetaTest.php @@ -9,7 +9,7 @@ class MetaTest extends TestCase { - public function testGet() + public function test_get() { $meta = new Meta(['foo' => 'bar']); @@ -17,7 +17,7 @@ public function testGet() $this->assertNull($meta->other); } - public function testOffsetGet() + public function test_offset_get() { $meta = new Meta(['foo' => 'bar']); @@ -25,7 +25,7 @@ public function testOffsetGet() $this->assertNull($meta['other']); } - public function testIsset() + public function test_isset() { $meta = new Meta(['foo' => 'bar']); @@ -33,7 +33,7 @@ public function testIsset() $this->assertFalse(isset($meta->other)); } - public function testOffsetExists() + public function test_offset_exists() { $meta = new Meta(['foo' => 'bar']); @@ -41,7 +41,7 @@ public function testOffsetExists() $this->assertFalse(isset($meta['other'])); } - public function testSet() + public function test_set() { $meta = new Meta([]); @@ -50,7 +50,7 @@ public function testSet() $this->assertEquals('bar', $meta->foo); } - public function testOffsetSet() + public function test_offset_set() { $meta = new Meta([]); @@ -59,7 +59,7 @@ public function testOffsetSet() $this->assertEquals('bar', $meta['foo']); } - public function testUnset() + public function test_unset() { $meta = new Meta(['foo' => 'bar']); @@ -68,7 +68,7 @@ public function testUnset() $this->assertNull($meta->foo); } - public function testOffsetUnset() + public function test_offset_unset() { $meta = new Meta(['foo' => 'bar']); @@ -77,7 +77,7 @@ public function testOffsetUnset() $this->assertNull($meta['foo']); } - public function testToArray() + public function test_to_array() { $meta = new Meta(['foo' => 'bar']); @@ -87,7 +87,7 @@ public function testToArray() ); } - public function testToJson() + public function test_to_json() { $meta = new Meta(['foo' => 'bar']); @@ -97,7 +97,7 @@ public function testToJson() ); } - public function testJsonSerialize() + public function test_json_serialize() { $meta = new Meta(['foo' => 'bar']); diff --git a/tests/Parsers/CollectionParserTest.php b/tests/Parsers/CollectionParserTest.php index 4ff11fa..55cde0b 100644 --- a/tests/Parsers/CollectionParserTest.php +++ b/tests/Parsers/CollectionParserTest.php @@ -16,12 +16,12 @@ class CollectionParserTest extends TestCase /** * @test */ - public function itConvertsDataToCollection() + public function it_converts_data_to_collection() { $itemParser = $this->createMock(ItemParser::class); $itemParser->expects($this->exactly(2)) ->method('parse') - ->willReturn(new PlainItem()); + ->willReturn(new PlainItem); $parser = new CollectionParser($itemParser); $collection = $parser->parse($this->getResourceCollection()); @@ -38,9 +38,9 @@ public function itConvertsDataToCollection() * * @dataProvider provideInvalidData * - * @param mixed $invalidData + * @param mixed $invalidData */ - public function itThrowsWhenDataIsNotAnArray($invalidData) + public function it_throws_when_data_is_not_an_array($invalidData) { $parser = new CollectionParser($this->createMock(ItemParser::class)); @@ -52,7 +52,7 @@ public function itThrowsWhenDataIsNotAnArray($invalidData) public function provideInvalidData(): array { - $object = new \stdClass(); + $object = new \stdClass; $object->foo = 'bar'; return [ diff --git a/tests/Parsers/DocumentParserTest.php b/tests/Parsers/DocumentParserTest.php index 5160e5e..7c0aca4 100644 --- a/tests/Parsers/DocumentParserTest.php +++ b/tests/Parsers/DocumentParserTest.php @@ -27,7 +27,7 @@ class DocumentParserTest extends TestCase /** * @test */ - public function itCanCreateAnInstanceUsingAFactoryMethod() + public function it_can_create_an_instance_using_a_factory_method() { $this->assertInstanceOf(DocumentParser::class, DocumentParser::create()); } @@ -35,7 +35,7 @@ public function itCanCreateAnInstanceUsingAFactoryMethod() /** * @test */ - public function itConvertsJsondocumentToDocument() + public function it_converts_jsondocument_to_document() { $parser = DocumentParser::create(); $document = $parser->parse( @@ -52,7 +52,7 @@ public function itConvertsJsondocumentToDocument() /** * @test */ - public function itThrowsWhenJsonIsNotValid() + public function it_throws_when_json_is_not_valid() { $parser = DocumentParser::create(); @@ -66,10 +66,8 @@ public function itThrowsWhenJsonIsNotValid() * @test * * @dataProvider provideInvalidJson - * - * @param string $invalidJson */ - public function itThrowsWhenJsonIsNotAJsonapiDocument(string $invalidJson) + public function it_throws_when_json_is_not_a_jsonapi_document(string $invalidJson) { $parser = DocumentParser::create(); @@ -94,20 +92,20 @@ public function provideInvalidJson(): array /** * @test */ - public function itThrowsWhenDataErrorsAndMetaAreMissing() + public function it_throws_when_data_errors_and_meta_are_missing() { $parser = DocumentParser::create(); $this->expectException(ValidationException::class); $this->expectExceptionMessage('Document MUST contain at least one of the following properties: `data`, `errors`, `meta`.'); - $parser->parse(json_encode(new \stdClass())); + $parser->parse(json_encode(new \stdClass)); } /** * @test */ - public function itThrowsWhenBothDataAndErrorsArePresent() + public function it_throws_when_both_data_and_errors_are_present() { $parser = DocumentParser::create(); @@ -120,7 +118,7 @@ public function itThrowsWhenBothDataAndErrorsArePresent() /** * @test */ - public function itThrowsWhenIncludedIsPresentButDataIsNot() + public function it_throws_when_included_is_present_but_data_is_not() { $parser = DocumentParser::create(); @@ -135,9 +133,9 @@ public function itThrowsWhenIncludedIsPresentButDataIsNot() * * @dataProvider provideInvalidData * - * @param mixed $invalidData + * @param mixed $invalidData */ - public function itThrowsWhenDataIsNotAnArrayObjectOrNull($invalidData) + public function it_throws_when_data_is_not_an_array_object_or_null($invalidData) { $parser = DocumentParser::create(); @@ -162,9 +160,9 @@ public function provideInvalidData(): array * * @dataProvider provideInvalidIncluded * - * @param mixed $invalidIncluded + * @param mixed $invalidIncluded */ - public function itThrowsWhenIncludedIsNotAnArray($invalidIncluded) + public function it_throws_when_included_is_not_an_array($invalidIncluded) { $parser = DocumentParser::create(); @@ -189,7 +187,7 @@ public function provideInvalidIncluded(): array /** * @test */ - public function itThrowsWhenItFindsDuplicateResources() + public function it_throws_when_it_finds_duplicate_resources() { $parser = DocumentParser::create(); @@ -225,7 +223,7 @@ public function itThrowsWhenItFindsDuplicateResources() /** * @test */ - public function itParsesAResourceDocument() + public function it_parses_a_resource_document() { $parser = DocumentParser::create(); $document = $parser->parse( @@ -251,7 +249,7 @@ public function itParsesAResourceDocument() /** * @test */ - public function itParsesAResourceCollectionDocument() + public function it_parses_a_resource_collection_document() { $parser = DocumentParser::create(); $document = $parser->parse( @@ -280,7 +278,7 @@ public function itParsesAResourceCollectionDocument() /** * @test */ - public function itParsesADocumentWithoutData() + public function it_parses_a_document_without_data() { $parser = DocumentParser::create(); $document = $parser->parse( @@ -299,7 +297,7 @@ public function itParsesADocumentWithoutData() /** * @test */ - public function itParsesIncluded() + public function it_parses_included() { $parser = DocumentParser::create(); $document = $parser->parse( @@ -329,9 +327,9 @@ public function itParsesIncluded() /** * @test */ - public function itLinksSingularRelationsToItemsFromIncluded() + public function it_links_singular_relations_to_items_from_included() { - $typeMapper = new TypeMapper(); + $typeMapper = new TypeMapper; $typeMapper->setMapping('parent', ParentItem::class); $typeMapper->setMapping('child', ChildItem::class); $parser = DocumentParser::create($typeMapper); @@ -375,9 +373,9 @@ public function itLinksSingularRelationsToItemsFromIncluded() /** * @test */ - public function itDoesNotLinkEmptySingularRelations() + public function it_does_not_link_empty_singular_relations() { - $typeMapper = new TypeMapper(); + $typeMapper = new TypeMapper; $typeMapper->setMapping('parent', ParentItem::class); $typeMapper->setMapping('child', ChildItem::class); $parser = DocumentParser::create($typeMapper); @@ -418,9 +416,9 @@ public function itDoesNotLinkEmptySingularRelations() /** * @test */ - public function itLinksPluralRelationsToItemsFromIncluded() + public function it_links_plural_relations_to_items_from_included() { - $typeMapper = new TypeMapper(); + $typeMapper = new TypeMapper; $typeMapper->setMapping('parent', ParentItem::class); $typeMapper->setMapping('child', ChildItem::class); $parser = DocumentParser::create($typeMapper); @@ -479,9 +477,9 @@ public function itLinksPluralRelationsToItemsFromIncluded() /** * @test */ - public function itDoesNotLinkEmptyPluralRelations() + public function it_does_not_link_empty_plural_relations() { - $typeMapper = new TypeMapper(); + $typeMapper = new TypeMapper; $typeMapper->setMapping('parent', ParentItem::class); $typeMapper->setMapping('child', ChildItem::class); $parser = DocumentParser::create($typeMapper); @@ -531,7 +529,7 @@ public function itDoesNotLinkEmptyPluralRelations() /** * @test */ - public function itParsesLinks() + public function it_parses_links() { $parser = DocumentParser::create(); @@ -554,7 +552,7 @@ public function itParsesLinks() /** * @test */ - public function itParsesErrors() + public function it_parses_errors() { $parser = DocumentParser::create(); @@ -579,7 +577,7 @@ public function itParsesErrors() /** * @test */ - public function itParsesMeta() + public function it_parses_meta() { $parser = DocumentParser::create(); @@ -602,9 +600,9 @@ public function itParsesMeta() /** * @test */ - public function itParsesMetaInRelationshipDataAndIncluded() + public function it_parses_meta_in_relationship_data_and_included() { - $typeMapper = new TypeMapper(); + $typeMapper = new TypeMapper; $typeMapper->setMapping('parent', ParentItem::class); $typeMapper->setMapping('child', ChildItem::class); $parser = DocumentParser::create($typeMapper); @@ -656,7 +654,7 @@ public function itParsesMetaInRelationshipDataAndIncluded() /** * @test */ - public function itParsesJsonapi() + public function it_parses_jsonapi() { $parser = DocumentParser::create(); diff --git a/tests/Parsers/ErrorCollectionParserTest.php b/tests/Parsers/ErrorCollectionParserTest.php index 8c38ce1..34b1961 100644 --- a/tests/Parsers/ErrorCollectionParserTest.php +++ b/tests/Parsers/ErrorCollectionParserTest.php @@ -16,12 +16,12 @@ class ErrorCollectionParserTest extends TestCase /** * @test */ - public function itConvertsDataToErrorCollection() + public function it_converts_data_to_error_collection() { $errorParser = $this->createMock(ErrorParser::class); $errorParser->expects($this->exactly(2)) ->method('parse') - ->willReturn(new Error()); + ->willReturn(new Error); $parser = new ErrorCollectionParser($errorParser); $errorCollection = $parser->parse($this->getErrorCollection()); @@ -38,9 +38,9 @@ public function itConvertsDataToErrorCollection() * * @dataProvider provideInvalidData * - * @param mixed $invalidData + * @param mixed $invalidData */ - public function itThrowsWhenDataIsNotAnArray($invalidData) + public function it_throws_when_data_is_not_an_array($invalidData) { $parser = new ErrorCollectionParser($this->createMock(ErrorParser::class)); @@ -52,7 +52,7 @@ public function itThrowsWhenDataIsNotAnArray($invalidData) public function provideInvalidData(): array { - $object = new \stdClass(); + $object = new \stdClass; $object->foo = 'bar'; return [ @@ -68,7 +68,7 @@ public function provideInvalidData(): array /** * @test */ - public function itThrowsWhenDataIsEmpty() + public function it_throws_when_data_is_empty() { $parser = new ErrorCollectionParser($this->createMock(ErrorParser::class)); diff --git a/tests/Parsers/ErrorParserTest.php b/tests/Parsers/ErrorParserTest.php index e729cc9..e67bc1c 100644 --- a/tests/Parsers/ErrorParserTest.php +++ b/tests/Parsers/ErrorParserTest.php @@ -19,9 +19,9 @@ class ErrorParserTest extends TestCase /** * @test */ - public function itConvertsDataToError() + public function it_converts_data_to_error() { - $parser = new ErrorParser(new LinksParser(new MetaParser()), new MetaParser()); + $parser = new ErrorParser(new LinksParser(new MetaParser), new MetaParser); $error = $parser->parse($this->getError()); $this->assertInstanceOf(Error::class, $error); @@ -45,9 +45,9 @@ public function itConvertsDataToError() * * @dataProvider provideInvalidData * - * @param mixed $invalidData + * @param mixed $invalidData */ - public function itThrowsWhenDataIsNotAnObject($invalidData) + public function it_throws_when_data_is_not_an_object($invalidData) { $parser = new ErrorParser($this->createMock(LinksParser::class), $this->createMock(MetaParser::class)); @@ -74,9 +74,9 @@ public function provideInvalidData(): array * * @dataProvider provideInvalidIdError * - * @param mixed $invalidError + * @param mixed $invalidError */ - public function itThrowsWhenIdIsNotAString($invalidError) + public function it_throws_when_id_is_not_a_string($invalidError) { $parser = new ErrorParser($this->createMock(LinksParser::class), $this->createMock(MetaParser::class)); @@ -103,9 +103,9 @@ public function provideInvalidIdError(): array * * @dataProvider provideInvalidStatusError * - * @param mixed $invalidError + * @param mixed $invalidError */ - public function itThrowsWhenStatusIsNotAString($invalidError) + public function it_throws_when_status_is_not_a_string($invalidError) { $parser = new ErrorParser($this->createMock(LinksParser::class), $this->createMock(MetaParser::class)); @@ -132,9 +132,9 @@ public function provideInvalidStatusError(): array * * @dataProvider provideInvalidCodeError * - * @param mixed $invalidError + * @param mixed $invalidError */ - public function itThrowsWhenCodeIsNotAString($invalidError) + public function it_throws_when_code_is_not_a_string($invalidError) { $parser = new ErrorParser($this->createMock(LinksParser::class), $this->createMock(MetaParser::class)); @@ -161,9 +161,9 @@ public function provideInvalidCodeError(): array * * @dataProvider provideInvalidTitleError * - * @param mixed $invalidError + * @param mixed $invalidError */ - public function itThrowsWhenTitleIsNotAString($invalidError) + public function it_throws_when_title_is_not_a_string($invalidError) { $parser = new ErrorParser($this->createMock(LinksParser::class), $this->createMock(MetaParser::class)); @@ -190,9 +190,9 @@ public function provideInvalidTitleError(): array * * @dataProvider provideInvalidDetailError * - * @param mixed $invalidError + * @param mixed $invalidError */ - public function itThrowsWhenDetailIsNotAString($invalidError) + public function it_throws_when_detail_is_not_a_string($invalidError) { $parser = new ErrorParser($this->createMock(LinksParser::class), $this->createMock(MetaParser::class)); @@ -219,9 +219,9 @@ public function provideInvalidDetailError(): array * * @dataProvider provideInvalidErrorSourceError * - * @param mixed $invalidError + * @param mixed $invalidError */ - public function itThrowsWhenErrorsourceIsNotAnObject($invalidError) + public function it_throws_when_errorsource_is_not_an_object($invalidError) { $parser = new ErrorParser($this->createMock(LinksParser::class), $this->createMock(MetaParser::class)); @@ -248,9 +248,9 @@ public function provideInvalidErrorSourceError(): array * * @dataProvider provideInvalidErrorSourcePointerError * - * @param mixed $invalidError + * @param mixed $invalidError */ - public function itThrowsWhenErrorsourcePointerIsNotAnString($invalidError) + public function it_throws_when_errorsource_pointer_is_not_an_string($invalidError) { $parser = new ErrorParser($this->createMock(LinksParser::class), $this->createMock(MetaParser::class)); @@ -277,9 +277,9 @@ public function provideInvalidErrorSourcePointerError(): array * * @dataProvider provideInvalidErrorSourceParameterError * - * @param mixed $invalidError + * @param mixed $invalidError */ - public function itThrowsWhenErrorsourceParameterIsNotAnString($invalidError) + public function it_throws_when_errorsource_parameter_is_not_an_string($invalidError) { $parser = new ErrorParser($this->createMock(LinksParser::class), $this->createMock(MetaParser::class)); diff --git a/tests/Parsers/ItemParserTest.php b/tests/Parsers/ItemParserTest.php index 2d5b394..6a4fdab 100644 --- a/tests/Parsers/ItemParserTest.php +++ b/tests/Parsers/ItemParserTest.php @@ -30,7 +30,7 @@ class ItemParserTest extends TestCase /** * @test */ - public function itConvertsDataToItem() + public function it_converts_data_to_item() { $parser = $this->getItemParser(); $item = $parser->parse($this->getJsonApiItemMock('parent', '1')); @@ -39,7 +39,7 @@ public function itConvertsDataToItem() static::assertEquals('parent', $item->getType()); static::assertEquals('1', $item->getId()); - $object = new \stdClass(); + $object = new \stdClass; $object->foo = 'bar'; static::assertEquals( [ @@ -59,9 +59,9 @@ public function itConvertsDataToItem() * * @dataProvider provideInvalidData * - * @param mixed $invalidData + * @param mixed $invalidData */ - public function itThrowsWhenDataIsNotAnObject($invalidData) + public function it_throws_when_data_is_not_an_object($invalidData) { $parser = $this->getItemParser(); @@ -86,7 +86,7 @@ public function provideInvalidData(): array /** * @test */ - public function itThrowsWhenItemDoesNotHaveTypeProperty() + public function it_throws_when_item_does_not_have_type_property() { $parser = $this->getItemParser(); @@ -99,7 +99,7 @@ public function itThrowsWhenItemDoesNotHaveTypeProperty() /** * @test */ - public function itThrowsWhenItemDoesNotHaveIdProperty() + public function it_throws_when_item_does_not_have_id_property() { $parser = $this->getItemParser(); @@ -114,9 +114,9 @@ public function itThrowsWhenItemDoesNotHaveIdProperty() * * @dataProvider provideInvalidIdItem * - * @param mixed $invalidItem + * @param mixed $invalidItem */ - public function itThrowsWhenIdIsNotAString($invalidItem) + public function it_throws_when_id_is_not_a_string($invalidItem) { $parser = $this->getItemParser(); @@ -141,9 +141,9 @@ public function provideInvalidIdItem(): array * * @dataProvider provideInvalidTypeItem * - * @param mixed $invalidItem + * @param mixed $invalidItem */ - public function itThrowsWhenTypeIsNotAString($invalidItem) + public function it_throws_when_type_is_not_a_string($invalidItem) { $parser = $this->getItemParser(); @@ -170,9 +170,9 @@ public function provideInvalidTypeItem(): array * * @dataProvider provideInvalidAttributesItem * - * @param mixed $invalidItem + * @param mixed $invalidItem */ - public function itThrowsWhenAttributesIsNotAnObject($invalidItem) + public function it_throws_when_attributes_is_not_an_object($invalidItem) { $parser = $this->getItemParser(); @@ -197,7 +197,7 @@ public function provideInvalidAttributesItem(): array /** * @test */ - public function itThrowsWhenTypeIsPresentInAttributes() + public function it_throws_when_type_is_present_in_attributes() { $parser = $this->getItemParser(); @@ -210,7 +210,7 @@ public function itThrowsWhenTypeIsPresentInAttributes() /** * @test */ - public function itThrowsWhenIdIsPresentInAttributes() + public function it_throws_when_id_is_present_in_attributes() { $parser = $this->getItemParser(); @@ -223,7 +223,7 @@ public function itThrowsWhenIdIsPresentInAttributes() /** * @test */ - public function itThrowsWhenRelationshipsIsPresentInAttributes() + public function it_throws_when_relationships_is_present_in_attributes() { $parser = $this->getItemParser(); @@ -236,7 +236,7 @@ public function itThrowsWhenRelationshipsIsPresentInAttributes() /** * @test */ - public function itThrowsWhenLinksIsPresentInAttributes() + public function it_throws_when_links_is_present_in_attributes() { $parser = $this->getItemParser(); @@ -251,9 +251,9 @@ public function itThrowsWhenLinksIsPresentInAttributes() * * @dataProvider provideInvalidRelationshipsItem * - * @param mixed $invalidItem + * @param mixed $invalidItem */ - public function itThrowsWhenRelationshipsIsNotAnObject($invalidItem) + public function it_throws_when_relationships_is_not_an_object($invalidItem) { $parser = $this->getItemParser(); @@ -278,7 +278,7 @@ public function provideInvalidRelationshipsItem(): array /** * @test */ - public function itThrowsWhenTypeIsPresentInRelationships() + public function it_throws_when_type_is_present_in_relationships() { $parser = $this->getItemParser(); @@ -291,7 +291,7 @@ public function itThrowsWhenTypeIsPresentInRelationships() /** * @test */ - public function itThrowsWhenIdIsPresentInRelationships() + public function it_throws_when_id_is_present_in_relationships() { $parser = $this->getItemParser(); @@ -304,7 +304,7 @@ public function itThrowsWhenIdIsPresentInRelationships() /** * @test */ - public function itThrowsWhenPropertyIsPresentInBothAttributesAndRelationships() + public function it_throws_when_property_is_present_in_both_attributes_and_relationships() { $parser = $this->getItemParser(); @@ -319,9 +319,9 @@ public function itThrowsWhenPropertyIsPresentInBothAttributesAndRelationships() * * @dataProvider provideInvalidRelationshipsItemItem * - * @param mixed $invalidItem + * @param mixed $invalidItem */ - public function itThrowsWhenRelationshipsItemIsNotAnObject($invalidItem) + public function it_throws_when_relationships_item_is_not_an_object($invalidItem) { $parser = $this->getItemParser(); @@ -346,7 +346,7 @@ public function provideInvalidRelationshipsItemItem(): array /** * @test */ - public function itThrowsWhenRelationshipsItemMissesLinksDataAndMeta() + public function it_throws_when_relationships_item_misses_links_data_and_meta() { $parser = $this->getItemParser(); @@ -361,9 +361,9 @@ public function itThrowsWhenRelationshipsItemMissesLinksDataAndMeta() * * @dataProvider provideInvalidRelationshipsItemIdentifierItem * - * @param mixed $invalidItem + * @param mixed $invalidItem */ - public function itThrowsWhenRelationshipsItemIdentifierIsNotAnObjectArrayOrNull($invalidItem) + public function it_throws_when_relationships_item_identifier_is_not_an_object_array_or_null($invalidItem) { $parser = $this->getItemParser(); @@ -386,7 +386,7 @@ public function provideInvalidRelationshipsItemIdentifierItem(): array /** * @test */ - public function itThrowsWhenRelationshipsItemIdentifierDoesNotHaveTypeProperty() + public function it_throws_when_relationships_item_identifier_does_not_have_type_property() { $parser = $this->getItemParser(); @@ -399,7 +399,7 @@ public function itThrowsWhenRelationshipsItemIdentifierDoesNotHaveTypeProperty() /** * @test */ - public function itThrowsWhenRelationshipsItemIdentifierDoesNotHaveIdProperty() + public function it_throws_when_relationships_item_identifier_does_not_have_id_property() { $parser = $this->getItemParser(); @@ -414,9 +414,9 @@ public function itThrowsWhenRelationshipsItemIdentifierDoesNotHaveIdProperty() * * @dataProvider provideInvalidRelationshipsItemIdentifierIdItem * - * @param mixed $invalidItem + * @param mixed $invalidItem */ - public function itThrowsWhenRelationshipsItemIdentifierIdIsNotAString($invalidItem) + public function it_throws_when_relationships_item_identifier_id_is_not_a_string($invalidItem) { $parser = $this->getItemParser(); @@ -441,9 +441,9 @@ public function provideInvalidRelationshipsItemIdentifierIdItem(): array * * @dataProvider provideInvalidRelationshipsItemIdentifierTypeItem * - * @param mixed $invalidItem + * @param mixed $invalidItem */ - public function itThrowsWhenRelationshipsItemIdentifierTypeIsNotAString($invalidItem) + public function it_throws_when_relationships_item_identifier_type_is_not_a_string($invalidItem) { $parser = $this->getItemParser(); @@ -468,9 +468,9 @@ public function provideInvalidRelationshipsItemIdentifierTypeItem(): array /** * @test */ - public function itParsesAHasOneRelationship() + public function it_parses_a_has_one_relationship() { - $typeMapper = new TypeMapper(); + $typeMapper = new TypeMapper; $typeMapper->setMapping('child', ChildItem::class); $typeMapper->setMapping('parent', ParentItem::class); $parser = $this->getItemParser($typeMapper); @@ -489,9 +489,9 @@ public function itParsesAHasOneRelationship() /** * @test */ - public function itParsesAnEmptyHasOneRelationship() + public function it_parses_an_empty_has_one_relationship() { - $typeMapper = new TypeMapper(); + $typeMapper = new TypeMapper; $typeMapper->setMapping('child', ChildItem::class); $typeMapper->setMapping('parent', ParentItem::class); $parser = $this->getItemParser($typeMapper); @@ -508,9 +508,9 @@ public function itParsesAnEmptyHasOneRelationship() /** * @test */ - public function itParsesAHasManyRelationship() + public function it_parses_a_has_many_relationship() { - $typeMapper = new TypeMapper(); + $typeMapper = new TypeMapper; $typeMapper->setMapping('child', ChildItem::class); $typeMapper->setMapping('parent', ParentItem::class); $parser = $this->getItemParser($typeMapper); @@ -532,9 +532,9 @@ public function itParsesAHasManyRelationship() /** * @test */ - public function itParsesAMorphToRelation() + public function it_parses_a_morph_to_relation() { - $typeMapper = new TypeMapper(); + $typeMapper = new TypeMapper; $typeMapper->setMapping('child', ChildItem::class); $typeMapper->setMapping('parent', ParentItem::class); $parser = $this->getItemParser($typeMapper); @@ -553,9 +553,9 @@ public function itParsesAMorphToRelation() /** * @test */ - public function itParsesAMorphToManyRelation() + public function it_parses_a_morph_to_many_relation() { - $typeMapper = new TypeMapper(); + $typeMapper = new TypeMapper; $typeMapper->setMapping('child', ChildItem::class); $typeMapper->setMapping('parent', ParentItem::class); $parser = $this->getItemParser($typeMapper); @@ -579,9 +579,9 @@ public function itParsesAMorphToManyRelation() /** * @test */ - public function itParsesAnUnknownSingularRelationAsMorphTo() + public function it_parses_an_unknown_singular_relation_as_morph_to() { - $typeMapper = new TypeMapper(); + $typeMapper = new TypeMapper; $typeMapper->setMapping('item-without-relationships', WithoutRelationshipsItem::class); $parser = $this->getItemParser($typeMapper); @@ -597,9 +597,9 @@ public function itParsesAnUnknownSingularRelationAsMorphTo() /** * @test */ - public function itParsesAnUnknownPluralRelationAsMorphToMany() + public function it_parses_an_unknown_plural_relation_as_morph_to_many() { - $typeMapper = new TypeMapper(); + $typeMapper = new TypeMapper; $typeMapper->setMapping('item-without-relationships', WithoutRelationshipsItem::class); $parser = $this->getItemParser($typeMapper); @@ -616,7 +616,7 @@ public function itParsesAnUnknownPluralRelationAsMorphToMany() /** * @test */ - public function itDoesNotSetDataWhenThereIsNoDataPresent() + public function it_does_not_set_data_when_there_is_no_data_present() { $parser = $this->getItemParser(); @@ -631,7 +631,7 @@ public function itDoesNotSetDataWhenThereIsNoDataPresent() /** * @test */ - public function itParsesLinks() + public function it_parses_links() { $parser = $this->getItemParser(); @@ -645,7 +645,7 @@ public function itParsesLinks() /** * @test */ - public function itParsesMeta() + public function it_parses_meta() { $parser = $this->getItemParser(); @@ -659,9 +659,9 @@ public function itParsesMeta() /** * @test */ - public function itParsesMetaInRelationship() + public function it_parses_meta_in_relationship() { - $typeMapper = new TypeMapper(); + $typeMapper = new TypeMapper; $typeMapper->setMapping('child', ChildItem::class); $typeMapper->setMapping('parent', ParentItem::class); $parser = $this->getItemParser($typeMapper); @@ -677,9 +677,9 @@ public function itParsesMetaInRelationship() /** * @test */ - public function itParsesMetaInRelationshipData() + public function it_parses_meta_in_relationship_data() { - $typeMapper = new TypeMapper(); + $typeMapper = new TypeMapper; $typeMapper->setMapping('child', ChildItem::class); $typeMapper->setMapping('parent', ParentItem::class); $parser = $this->getItemParser($typeMapper); @@ -693,17 +693,12 @@ public function itParsesMetaInRelationshipData() $this->assertEquals('https://example.com/image/header/about-us.jpeg', $image); } - /** - * @param \Swis\JsonApi\Client\Interfaces\TypeMapperInterface|null $typeMapper - * - * @return \Swis\JsonApi\Client\Parsers\ItemParser - */ private function getItemParser(?TypeMapperInterface $typeMapper = null): ItemParser { return new ItemParser( $typeMapper ?? $this->getTypeMapperMock(), - new LinksParser(new MetaParser()), - new MetaParser() + new LinksParser(new MetaParser), + new MetaParser ); } @@ -719,7 +714,7 @@ private function getTypeMapperMock() $typeMapper->method('getMapping') ->willReturnCallback( static function (string $type) { - return (new PlainItem())->setType($type); + return (new PlainItem)->setType($type); } ); @@ -727,9 +722,6 @@ static function (string $type) { } /** - * @param $type - * @param $id - * * @return mixed */ private function getJsonApiItemMock($type, $id) diff --git a/tests/Parsers/JsonapiParserTest.php b/tests/Parsers/JsonapiParserTest.php index f21de89..53afc45 100644 --- a/tests/Parsers/JsonapiParserTest.php +++ b/tests/Parsers/JsonapiParserTest.php @@ -16,9 +16,9 @@ class JsonapiParserTest extends TestCase /** * @test */ - public function itConvertsDataToJsonapi() + public function it_converts_data_to_jsonapi() { - $parser = new JsonapiParser(new MetaParser()); + $parser = new JsonapiParser(new MetaParser); $jsonapi = $parser->parse($this->getJsonapi()); $this->assertInstanceOf(Jsonapi::class, $jsonapi); @@ -33,9 +33,9 @@ public function itConvertsDataToJsonapi() * * @dataProvider provideInvalidData * - * @param mixed $invalidData + * @param mixed $invalidData */ - public function itThrowsWhenDataIsNotAnObject($invalidData) + public function it_throws_when_data_is_not_an_object($invalidData) { $parser = new JsonapiParser($this->createMock(MetaParser::class)); @@ -62,9 +62,9 @@ public function provideInvalidData(): array * * @dataProvider provideInvalidVersionJsonapi * - * @param mixed $invalidJsonapi + * @param mixed $invalidJsonapi */ - public function itThrowsWhenVersionIsNotAString($invalidJsonapi) + public function it_throws_when_version_is_not_a_string($invalidJsonapi) { $parser = new JsonapiParser($this->createMock(MetaParser::class)); diff --git a/tests/Parsers/LinksParserTest.php b/tests/Parsers/LinksParserTest.php index 0127b62..d8955ff 100644 --- a/tests/Parsers/LinksParserTest.php +++ b/tests/Parsers/LinksParserTest.php @@ -17,9 +17,9 @@ class LinksParserTest extends TestCase /** * @test */ - public function itConvertsDataToLinks() + public function it_converts_data_to_links() { - $parser = new LinksParser(new MetaParser()); + $parser = new LinksParser(new MetaParser); $links = $parser->parse($this->getLinks(), LinksParser::SOURCE_DOCUMENT); $this->assertInstanceOf(Links::class, $links); @@ -54,9 +54,9 @@ public function itConvertsDataToLinks() * * @dataProvider provideInvalidData * - * @param mixed $invalidData + * @param mixed $invalidData */ - public function itThrowsWhenLinksIsNotAnObject($invalidData) + public function it_throws_when_links_is_not_an_object($invalidData) { $parser = new LinksParser($this->createMock(MetaParser::class)); @@ -83,9 +83,9 @@ public function provideInvalidData(): array * * @dataProvider provideInvalidLinkData * - * @param mixed $invalidData + * @param mixed $invalidData */ - public function itThrowsWhenLinkIsNotAStringObjectOrNull($invalidData) + public function it_throws_when_link_is_not_a_string_object_or_null($invalidData) { $parser = new LinksParser($this->createMock(MetaParser::class)); @@ -108,7 +108,7 @@ public function provideInvalidLinkData(): array /** * @test */ - public function itThrowsWhenSelfLinkIsNull() + public function it_throws_when_self_link_is_null() { $parser = new LinksParser($this->createMock(MetaParser::class)); @@ -121,7 +121,7 @@ public function itThrowsWhenSelfLinkIsNull() /** * @test */ - public function itThrowsWhenRelatedLinkIsNull() + public function it_throws_when_related_link_is_null() { $parser = new LinksParser($this->createMock(MetaParser::class)); @@ -134,7 +134,7 @@ public function itThrowsWhenRelatedLinkIsNull() /** * @test */ - public function itThrowsWhenRelationshipLinksMissesSelfAndRelatedLinks() + public function it_throws_when_relationship_links_misses_self_and_related_links() { $parser = new LinksParser($this->createMock(MetaParser::class)); @@ -147,7 +147,7 @@ public function itThrowsWhenRelationshipLinksMissesSelfAndRelatedLinks() /** * @test */ - public function itThrowsWhenLinkDoesNotHaveHrefProperty() + public function it_throws_when_link_does_not_have_href_property() { $parser = new LinksParser($this->createMock(MetaParser::class)); diff --git a/tests/Parsers/MetaParserTest.php b/tests/Parsers/MetaParserTest.php index bc8492f..c274cf7 100644 --- a/tests/Parsers/MetaParserTest.php +++ b/tests/Parsers/MetaParserTest.php @@ -14,9 +14,9 @@ class MetaParserTest extends TestCase /** * @test */ - public function itConvertsDataToMeta() + public function it_converts_data_to_meta() { - $parser = new MetaParser(); + $parser = new MetaParser; $meta = $parser->parse($this->getMeta()); $this->assertInstanceOf(Meta::class, $meta); @@ -29,11 +29,11 @@ public function itConvertsDataToMeta() * * @dataProvider provideInvalidData * - * @param mixed $invalidData + * @param mixed $invalidData */ - public function itThrowsWhenDataIsNotAnObject($invalidData) + public function it_throws_when_data_is_not_an_object($invalidData) { - $parser = new MetaParser(); + $parser = new MetaParser; $this->expectException(ValidationException::class); $this->expectExceptionMessage(sprintf('Meta MUST be an object, "%s" given.', gettype($invalidData))); diff --git a/tests/Parsers/ResponseParserTest.php b/tests/Parsers/ResponseParserTest.php index 26ac2c4..dc8022f 100644 --- a/tests/Parsers/ResponseParserTest.php +++ b/tests/Parsers/ResponseParserTest.php @@ -4,8 +4,10 @@ namespace Swis\JsonApi\Client\Tests\Parsers; +use GuzzleHttp\Psr7\PumpStream; use GuzzleHttp\Psr7\Response; use PHPUnit\Framework\TestCase; +use Psr\Http\Message\StreamInterface; use Swis\JsonApi\Client\CollectionDocument; use Swis\JsonApi\Client\Document; use Swis\JsonApi\Client\InvalidResponseDocument; @@ -17,7 +19,7 @@ class ResponseParserTest extends TestCase /** * @test */ - public function itCanCreateAnInstanceUsingAFactoryMethod() + public function it_can_create_an_instance_using_a_factory_method() { $this->assertInstanceOf(ResponseParser::class, ResponseParser::create()); } @@ -25,12 +27,12 @@ public function itCanCreateAnInstanceUsingAFactoryMethod() /** * @test */ - public function itConvertsPsrReponseToDocument() + public function it_converts_psr_reponse_to_document() { $documentParser = $this->createMock(DocumentParser::class); $documentParser->expects($this->once()) ->method('parse') - ->willReturn(new CollectionDocument()); + ->willReturn(new CollectionDocument); $parser = new ResponseParser($documentParser); @@ -44,7 +46,7 @@ public function itConvertsPsrReponseToDocument() /** * @test */ - public function itParsesAResponseWithAnEmptyBody() + public function it_parses_a_response_with_an_empty_body() { $documentParser = $this->createMock(DocumentParser::class); $documentParser->expects($this->never()) @@ -62,7 +64,96 @@ public function itParsesAResponseWithAnEmptyBody() /** * @test */ - public function itParsesAnErrorResponse() + public function it_parses_a_response_with_an_empty_body_and_unknown_size() + { + $documentParser = $this->createMock(DocumentParser::class); + $documentParser->expects($this->never()) + ->method('parse'); + + $parser = new ResponseParser($documentParser); + + $stream = new PumpStream(function () { + return false; + }); + + $response = new Response(204, [], $stream); + + $document = $parser->parse($response); + + $this->assertInstanceOf(Document::class, $document); + $this->assertSame($response, $document->getResponse()); + } + + /** + * @test + */ + public function it_parses_a_response_with_a_body_and_unknown_size() + { + $json = json_encode(['meta' => ['ok' => true]]); + + $stream = new PumpStream(function () use ($json) { + static $done = false; + if ($done) { + return false; + } + $done = true; + + return $json; + }); + + $parsedDocument = new Document; + + $documentParser = $this->createMock(DocumentParser::class); + $documentParser + ->expects($this->once()) + ->method('parse') + ->with($this->isType('string')) + ->willReturn($parsedDocument); + + $parser = new ResponseParser($documentParser); + + $response = new Response(200, [], $stream); + + $document = $parser->parse($response); + + $this->assertSame($parsedDocument, $document); + $this->assertSame($response, $document->getResponse()); + } + + /** + * @test + */ + public function it_parses_a_response_with_a_seekable_stream_and_unknown_size() + { + $stream = $this->createMock(StreamInterface::class); + $stream->method('getSize')->willReturn(-1); + $stream->method('isSeekable')->willReturn(true); + $stream->method('tell')->willReturn(0); + $stream->expects($this->once())->method('read')->with(1)->willReturn('x'); + $stream->expects($this->once())->method('seek')->with(0); + + $parsedDocument = new Document; + + $documentParser = $this->createMock(DocumentParser::class); + $documentParser + ->expects($this->once()) + ->method('parse') + ->with($this->isType('string')) + ->willReturn($parsedDocument); + + $parser = new ResponseParser($documentParser); + $response = new Response(200, [], $stream); + + $document = $parser->parse($response); + + $this->assertSame($parsedDocument, $document); + $this->assertSame($response, $document->getResponse()); + } + + /** + * @test + */ + public function it_parses_an_error_response() { $documentParser = $this->createMock(DocumentParser::class); $documentParser->expects($this->never()) diff --git a/tests/Relations/AbstractManyRelationTest.php b/tests/Relations/AbstractManyRelationTest.php index cee834e..a39b01c 100644 --- a/tests/Relations/AbstractManyRelationTest.php +++ b/tests/Relations/AbstractManyRelationTest.php @@ -14,11 +14,11 @@ class AbstractManyRelationTest extends TestCase /** * @test */ - public function itCanAssociateACollectionAndGetTheIncluded() + public function it_can_associate_a_collection_and_get_the_included() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Relations\AbstractManyRelation $mock */ $mock = $this->getMockForAbstractClass(AbstractManyRelation::class); - $collection = new Collection([new Item()]); + $collection = new Collection([new Item]); $mock->associate($collection); @@ -28,28 +28,28 @@ public function itCanAssociateACollectionAndGetTheIncluded() /** * @test */ - public function itCanDissociateACollection() + public function it_can_dissociate_a_collection() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Relations\AbstractManyRelation $mock */ $mock = $this->getMockForAbstractClass(AbstractManyRelation::class); - $collection = new Collection([new Item()]); + $collection = new Collection([new Item]); $mock->associate($collection); $this->assertNotNull($mock->getIncluded()); $mock->dissociate(); - $this->assertEquals($mock->getIncluded(), new Collection()); + $this->assertEquals($mock->getIncluded(), new Collection); } /** * @test */ - public function itReturnsABooleanIndicatingIfItHasIncluded() + public function it_returns_a_boolean_indicating_if_it_has_included() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Relations\AbstractManyRelation $mock */ $mock = $this->getMockForAbstractClass(AbstractManyRelation::class); - $collection = new Collection([new Item()]); + $collection = new Collection([new Item]); $this->assertFalse($mock->hasIncluded()); $mock->associate($collection); @@ -60,7 +60,7 @@ public function itReturnsABooleanIndicatingIfItHasIncluded() /** * @test */ - public function itCanSetAndGetOmitIncluded() + public function it_can_set_and_get_omit_included() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Relations\AbstractManyRelation $mock */ $mock = $this->getMockForAbstractClass(AbstractManyRelation::class); @@ -74,7 +74,7 @@ public function itCanSetAndGetOmitIncluded() /** * @test */ - public function itCanSortTheIncluded() + public function it_can_sort_the_included() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Relations\AbstractManyRelation $mock */ $mock = $this->getMockForAbstractClass(AbstractManyRelation::class); diff --git a/tests/Relations/AbstractOneRelationTest.php b/tests/Relations/AbstractOneRelationTest.php index 9cf2a06..439e3b0 100644 --- a/tests/Relations/AbstractOneRelationTest.php +++ b/tests/Relations/AbstractOneRelationTest.php @@ -13,11 +13,11 @@ class AbstractOneRelationTest extends TestCase /** * @test */ - public function itCanAssociateAnItemAndGetTheIncluded() + public function it_can_associate_an_item_and_get_the_included() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Relations\AbstractOneRelation $mock */ $mock = $this->getMockForAbstractClass(AbstractOneRelation::class); - $item = new Item(); + $item = new Item; $mock->associate($item); @@ -27,11 +27,11 @@ public function itCanAssociateAnItemAndGetTheIncluded() /** * @test */ - public function itCanDissociateAnItem() + public function it_can_dissociate_an_item() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Relations\AbstractOneRelation $mock */ $mock = $this->getMockForAbstractClass(AbstractOneRelation::class); - $item = new Item(); + $item = new Item; $mock->associate($item); $this->assertNotNull($mock->getIncluded()); @@ -44,11 +44,11 @@ public function itCanDissociateAnItem() /** * @test */ - public function itReturnsABooleanIndicatingIfItHasIncluded() + public function it_returns_a_boolean_indicating_if_it_has_included() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Relations\AbstractOneRelation $mock */ $mock = $this->getMockForAbstractClass(AbstractOneRelation::class); - $item = new Item(); + $item = new Item; $this->assertFalse($mock->hasIncluded()); $mock->associate($item); @@ -59,7 +59,7 @@ public function itReturnsABooleanIndicatingIfItHasIncluded() /** * @test */ - public function itCanSetAndGetOmitIncluded() + public function it_can_set_and_get_omit_included() { /** @var \PHPUnit\Framework\MockObject\MockObject&\Swis\JsonApi\Client\Relations\AbstractOneRelation $mock */ $mock = $this->getMockForAbstractClass(AbstractOneRelation::class); diff --git a/tests/RepositoryTest.php b/tests/RepositoryTest.php index a62cc2c..266ce4e 100644 --- a/tests/RepositoryTest.php +++ b/tests/RepositoryTest.php @@ -17,11 +17,11 @@ class RepositoryTest extends TestCase /** * @test */ - public function itCanGetTheClient() + public function it_can_get_the_client() { /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client */ $client = $this->createMock(DocumentClientInterface::class); - $repository = new MockRepository($client, new DocumentFactory()); + $repository = new MockRepository($client, new DocumentFactory); $this->assertSame($client, $repository->getClient()); } @@ -29,11 +29,11 @@ public function itCanGetTheClient() /** * @test */ - public function itCanGetTheEndpoint() + public function it_can_get_the_endpoint() { /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client */ $client = $this->createMock(DocumentClientInterface::class); - $repository = new MockRepository($client, new DocumentFactory()); + $repository = new MockRepository($client, new DocumentFactory); $this->assertSame('mocks', $repository->getEndpoint()); } @@ -41,122 +41,122 @@ public function itCanGetTheEndpoint() /** * @test */ - public function itCanGetAll() + public function it_can_get_all() { - $document = new Document(); + $document = new Document; /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client */ $client = $this->createMock(DocumentClientInterface::class); $client->expects($this->once()) ->method('get') - ->with('mocks?foo=bar') + ->with('mocks?foo=bar', ['Test-Header' => 'Foo-Bar']) ->willReturn($document); - $repository = new MockRepository($client, new DocumentFactory()); + $repository = new MockRepository($client, new DocumentFactory); - $this->assertSame($document, $repository->all(['foo' => 'bar'])); + $this->assertSame($document, $repository->all(['foo' => 'bar'], ['Test-Header' => 'Foo-Bar'])); } /** * @test */ - public function itCanTakeOne() + public function it_can_take_one() { - $document = new Document(); + $document = new Document; /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client */ $client = $this->createMock(DocumentClientInterface::class); $client->expects($this->once()) ->method('get') - ->with('mocks?foo=bar') + ->with('mocks?foo=bar', ['Test-Header' => 'Foo-Bar']) ->willReturn($document); - $repository = new MockRepository($client, new DocumentFactory()); + $repository = new MockRepository($client, new DocumentFactory); - $this->assertSame($document, $repository->take(['foo' => 'bar'])); + $this->assertSame($document, $repository->take(['foo' => 'bar'], ['Test-Header' => 'Foo-Bar'])); } /** * @test */ - public function itCanFindOne() + public function it_can_find_one() { - $document = new Document(); + $document = new Document; /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client */ $client = $this->createMock(DocumentClientInterface::class); $client->expects($this->once()) ->method('get') - ->with('mocks/1?foo=bar') + ->with('mocks/1?foo=bar', ['Test-Header' => 'Foo-Bar']) ->willReturn($document); - $repository = new MockRepository($client, new DocumentFactory()); + $repository = new MockRepository($client, new DocumentFactory); - $this->assertSame($document, $repository->find('1', ['foo' => 'bar'])); + $this->assertSame($document, $repository->find('1', ['foo' => 'bar'], ['Test-Header' => 'Foo-Bar'])); } /** * @test */ - public function itCanSaveNew() + public function it_can_save_new() { - $document = new ItemDocument(); - $document->setData(new Item()); + $document = new ItemDocument; + $document->setData(new Item); /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client */ $client = $this->createMock(DocumentClientInterface::class); $client->expects($this->once()) ->method('post') - ->with('mocks?foo=bar') + ->with('mocks?foo=bar', $document, ['Test-Header' => 'Foo-Bar']) ->willReturn($document); - $repository = new MockRepository($client, new DocumentFactory()); + $repository = new MockRepository($client, new DocumentFactory); - $this->assertSame($document, $repository->save(new Item(), ['foo' => 'bar'])); + $this->assertSame($document, $repository->save(new Item, ['foo' => 'bar'], ['Test-Header' => 'Foo-Bar'])); } /** * @test */ - public function itCanSaveExisting() + public function it_can_save_existing() { - $document = new ItemDocument(); - $document->setData((new Item())->setId('1')); + $document = new ItemDocument; + $document->setData((new Item)->setId('1')); /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client */ $client = $this->createMock(DocumentClientInterface::class); $client->expects($this->once()) ->method('patch') - ->with('mocks/1?foo=bar') + ->with('mocks/1?foo=bar', $document, ['Test-Header' => 'Foo-Bar']) ->willReturn($document); - $repository = new MockRepository($client, new DocumentFactory()); + $repository = new MockRepository($client, new DocumentFactory); - $this->assertSame($document, $repository->save((new Item())->setId('1'), ['foo' => 'bar'])); + $this->assertSame($document, $repository->save((new Item)->setId('1'), ['foo' => 'bar'], ['Test-Header' => 'Foo-Bar'])); } /** * @test */ - public function itCanDelete() + public function it_can_delete() { - $document = new Document(); + $document = new Document; /** @var \PHPUnit\Framework\MockObject\MockObject|\Swis\JsonApi\Client\Interfaces\DocumentClientInterface $client */ $client = $this->createMock(DocumentClientInterface::class); $client->expects($this->once()) ->method('delete') - ->with('mocks/1?foo=bar') + ->with('mocks/1?foo=bar', ['Test-Header' => 'Foo-Bar']) ->willReturn($document); - $repository = new MockRepository($client, new DocumentFactory()); + $repository = new MockRepository($client, new DocumentFactory); - $this->assertSame($document, $repository->delete('1', ['foo' => 'bar'])); + $this->assertSame($document, $repository->delete('1', ['foo' => 'bar'], ['Test-Header' => 'Foo-Bar'])); } } diff --git a/tests/TypeMapperTest.php b/tests/TypeMapperTest.php index 8caf052..19c6b9f 100644 --- a/tests/TypeMapperTest.php +++ b/tests/TypeMapperTest.php @@ -15,9 +15,9 @@ class TypeMapperTest extends TestCase /** * @test */ - public function itRemembersTypeMappingsAfterSetting() + public function it_remembers_type_mappings_after_setting() { - $typeMapper = new TypeMapper(); + $typeMapper = new TypeMapper; $typeMapper->setMapping('item', Item::class); $this->assertTrue($typeMapper->hasMapping('item')); @@ -27,9 +27,9 @@ public function itRemembersTypeMappingsAfterSetting() /** * @test */ - public function itForgetsTypeMappingsAfterRemoving() + public function it_forgets_type_mappings_after_removing() { - $typeMapper = new TypeMapper(); + $typeMapper = new TypeMapper; $typeMapper->setMapping('item', Item::class); $typeMapper->removeMapping('item'); @@ -39,35 +39,35 @@ public function itForgetsTypeMappingsAfterRemoving() /** * @test */ - public function itThrowsAnInvalidargumentexceptionWhenMappingDoesntExist() + public function it_throws_an_invalidargumentexception_when_mapping_doesnt_exist() { $this->expectException(TypeMappingException::class); - $typeMapper = new TypeMapper(); + $typeMapper = new TypeMapper; $typeMapper->getMapping('item'); } /** * @test */ - public function itThrowsAnInvalidargumentexceptionWhenClassDoesntExist() + public function it_throws_an_invalidargumentexception_when_class_doesnt_exist() { $this->expectException(TypeMappingException::class); $this->expectExceptionMessage(sprintf('Class %s not found.', '\Non\Existing\Class')); - $typeMapper = new TypeMapper(); + $typeMapper = new TypeMapper; $typeMapper->setMapping('item', '\Non\Existing\Class'); } /** * @test */ - public function itThrowsAnInvalidargumentexceptionWhenClassDoesntImplementIteminterface() + public function it_throws_an_invalidargumentexception_when_class_doesnt_implement_iteminterface() { $this->expectException(TypeMappingException::class); $this->expectExceptionMessage(sprintf('Class %s must implement %s.', TypeMapper::class, ItemInterface::class)); - $typeMapper = new TypeMapper(); + $typeMapper = new TypeMapper; $typeMapper->setMapping('item', TypeMapper::class); } } diff --git a/tests/UtilTest.php b/tests/UtilTest.php index bc4875a..0913bb7 100644 --- a/tests/UtilTest.php +++ b/tests/UtilTest.php @@ -7,13 +7,13 @@ class UtilTest extends TestCase { - public function testLower() + public function test_lower() { $this->assertSame('foo bar baz', Util::stringLower('FOO BAR BAZ')); $this->assertSame('foo bar baz', Util::stringLower('fOo Bar bAz')); } - public function testSnake() + public function test_snake() { $this->assertSame('laravel_p_h_p_framework', Util::stringSnake('LaravelPHPFramework')); $this->assertSame('laravel_php_framework', Util::stringSnake('LaravelPhpFramework')); @@ -32,7 +32,7 @@ public function testSnake() $this->assertSame('żółtałódka', Util::stringSnake('ŻółtaŁódka')); } - public function testStudly() + public function test_studly() { $this->assertSame('LaravelPHPFramework', Util::stringStudly('laravel_p_h_p_framework')); $this->assertSame('LaravelPhpFramework', Util::stringStudly('laravel_php_framework')); @@ -46,14 +46,14 @@ public function testStudly() $this->assertSame('FooBarBaz', Util::stringStudly('foo-bar_baz')); } - public function testArrayExcept() + public function test_array_except() { $testArray = [ - 'first' => 'First', - 'second' => 'second', - 'third' => 'third', - 'fourth' => 'fourth', - ]; + 'first' => 'First', + 'second' => 'second', + 'third' => 'third', + 'fourth' => 'fourth', + ]; $this->assertArrayHasKey('first', $testArray); $this->assertArrayNotHasKey('first', Util::arrayExcept($testArray, ['first'])); $this->assertArrayNotHasKey('third', Util::arrayExcept($testArray, ['third'])); diff --git a/tests/_mocks/ItemStub.php b/tests/_mocks/ItemStub.php index 7387b22..581f631 100644 --- a/tests/_mocks/ItemStub.php +++ b/tests/_mocks/ItemStub.php @@ -8,8 +8,14 @@ class ItemStub extends Item { + /** + * @var array + */ protected $hidden = ['password']; + /** + * @var array + */ protected $casts = [ 'age' => 'integer', 'score' => 'float', @@ -25,10 +31,16 @@ class ItemStub extends Item 'foo' => 'bar', ]; + /** + * @var array + */ protected $guarded = [ 'secret', ]; + /** + * @var array + */ protected $fillable = [ 'name', 'city', @@ -42,34 +54,46 @@ class ItemStub extends Item 'collection_data', ]; - public function getListItemsAttribute($value) + /** + * @return array + */ + public function getListItemsAttribute(string $value): array { - return json_decode($value, true); + $result = json_decode($value, true); + assert(is_array($result)); + + /** @var array $result */ + return $result; } - public function setListItemsAttribute($value) + /** + * @param array $value + */ + public function setListItemsAttribute(array $value): void { $this->attributes['list_items'] = json_encode($value); } - public function setBirthdayAttribute($value) + public function setBirthdayAttribute(string $value): void { $this->attributes['birthday'] = strtotime($value); } - public function getBirthdayAttribute($value) + public function getBirthdayAttribute(int $value): string { return date('Y-m-d', $value); } - public function getAgeAttribute($value) + public function getAgeAttribute(mixed $value): int { + assert(is_int($this->attributes['birthday'])); $date = \DateTime::createFromFormat('U', (string) $this->attributes['birthday']); + assert($date instanceof \DateTime); return $date->diff(new \DateTime('now'))->y; } - public function getTestAttribute($value) + public function getTestAttribute(mixed $value): string { return 'test'; } diff --git a/tests/_mocks/Items/AnotherRelatedItem.php b/tests/_mocks/Items/AnotherRelatedItem.php index 7d56827..6887933 100644 --- a/tests/_mocks/Items/AnotherRelatedItem.php +++ b/tests/_mocks/Items/AnotherRelatedItem.php @@ -8,13 +8,10 @@ class AnotherRelatedItem extends Item { - /** - * @var string - */ protected $type = 'another-related-item'; /** - * @var array + * @var array */ protected $visible = [ 'test_related_attribute1', diff --git a/tests/_mocks/Items/ChildItem.php b/tests/_mocks/Items/ChildItem.php index f167e7f..5643dc3 100644 --- a/tests/_mocks/Items/ChildItem.php +++ b/tests/_mocks/Items/ChildItem.php @@ -8,13 +8,10 @@ class ChildItem extends Item { - /** - * @var string - */ protected $type = 'child'; /** - * @var array + * @var array */ protected $visible = [ 'active', @@ -22,12 +19,15 @@ class ChildItem extends Item ]; /** - * @var array + * @var array */ protected $casts = [ 'active' => 'bool', ]; + /** + * @var array + */ protected $attributes = [ 'active' => true, ]; diff --git a/tests/_mocks/Items/ParentItem.php b/tests/_mocks/Items/ParentItem.php index 8a09b11..6dfa612 100644 --- a/tests/_mocks/Items/ParentItem.php +++ b/tests/_mocks/Items/ParentItem.php @@ -4,17 +4,16 @@ namespace Swis\JsonApi\Client\Tests\Mocks\Items; +use Swis\JsonApi\Client\Interfaces\ManyRelationInterface; +use Swis\JsonApi\Client\Interfaces\OneRelationInterface; use Swis\JsonApi\Client\Item; class ParentItem extends Item { - /** - * @var string - */ protected $type = 'parent'; /** - * @var array + * @var array */ protected $visible = [ 'active', @@ -23,21 +22,21 @@ class ParentItem extends Item ]; /** - * @var array + * @var array */ protected $casts = [ 'active' => 'bool', ]; /** - * @var array + * @var array */ protected $attributes = [ 'active' => true, ]; /** - * @var array + * @var array */ protected $availableRelations = [ 'child', @@ -47,27 +46,42 @@ class ParentItem extends Item 'does_not_exist', ]; - public function child() + /** + * @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface<\Swis\JsonApi\Client\Tests\Mocks\Items\ChildItem> + */ + public function child(): OneRelationInterface { return $this->hasOne(ChildItem::class); } - public function children() + /** + * @return \Swis\JsonApi\Client\Interfaces\ManyRelationInterface<\Swis\JsonApi\Client\Tests\Mocks\Items\ChildItem> + */ + public function children(): ManyRelationInterface { return $this->hasMany(ChildItem::class); } - public function morph() + /** + * @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface<\Swis\JsonApi\Client\Interfaces\ItemInterface> + */ + public function morph(): OneRelationInterface { return $this->morphTo(); } - public function morphmany() + /** + * @return \Swis\JsonApi\Client\Interfaces\ManyRelationInterface<\Swis\JsonApi\Client\Interfaces\ItemInterface> + */ + public function morphmany(): ManyRelationInterface { return $this->morphToMany(); } - public function empty() + /** + * @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface<\Swis\JsonApi\Client\Tests\Mocks\Items\ChildItem> + */ + public function empty(): OneRelationInterface { return $this->hasOne(ChildItem::class); } diff --git a/tests/_mocks/Items/PlainItem.php b/tests/_mocks/Items/PlainItem.php index 668ce10..8af32c8 100644 --- a/tests/_mocks/Items/PlainItem.php +++ b/tests/_mocks/Items/PlainItem.php @@ -6,6 +6,4 @@ use Swis\JsonApi\Client\Item; -class PlainItem extends Item -{ -} +class PlainItem extends Item {} diff --git a/tests/_mocks/Items/RelatedItem.php b/tests/_mocks/Items/RelatedItem.php index 5f8de74..3144ff4 100644 --- a/tests/_mocks/Items/RelatedItem.php +++ b/tests/_mocks/Items/RelatedItem.php @@ -4,17 +4,15 @@ namespace Swis\JsonApi\Client\Tests\Mocks\Items; +use Swis\JsonApi\Client\Interfaces\OneRelationInterface; use Swis\JsonApi\Client\Item; class RelatedItem extends Item { - /** - * @var string - */ protected $type = 'related-item'; /** - * @var array + * @var array */ protected $visible = [ 'test_related_attribute1', @@ -22,13 +20,16 @@ class RelatedItem extends Item ]; /** - * @var array + * @var array */ protected $availableRelations = [ 'parent_relation', ]; - public function parentRelation() + /** + * @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface<\Swis\JsonApi\Client\Tests\Mocks\Items\WithRelationshipItem> + */ + public function parentRelation(): OneRelationInterface { return $this->hasOne(WithRelationshipItem::class); } diff --git a/tests/_mocks/Items/WithHiddenItem.php b/tests/_mocks/Items/WithHiddenItem.php index 38dae29..11b73f3 100644 --- a/tests/_mocks/Items/WithHiddenItem.php +++ b/tests/_mocks/Items/WithHiddenItem.php @@ -8,6 +8,9 @@ class WithHiddenItem extends Item { + /** + * @var array + */ protected $hidden = [ 'testKey', ]; diff --git a/tests/_mocks/Items/WithRelationshipItem.php b/tests/_mocks/Items/WithRelationshipItem.php index 9f4cc04..a1cb8f1 100644 --- a/tests/_mocks/Items/WithRelationshipItem.php +++ b/tests/_mocks/Items/WithRelationshipItem.php @@ -4,17 +4,16 @@ namespace Swis\JsonApi\Client\Tests\Mocks\Items; +use Swis\JsonApi\Client\Interfaces\ManyRelationInterface; +use Swis\JsonApi\Client\Interfaces\OneRelationInterface; use Swis\JsonApi\Client\Item; class WithRelationshipItem extends Item { - /** - * @var string - */ protected $type = 'item-with-relationship'; /** - * @var array + * @var array */ protected $visible = [ 'test_attribute_1', @@ -22,7 +21,7 @@ class WithRelationshipItem extends Item ]; /** - * @var array + * @var array */ protected $availableRelations = [ 'hasone_relation', @@ -31,22 +30,34 @@ class WithRelationshipItem extends Item 'morphtomany_relation', ]; - public function hasoneRelation() + /** + * @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface<\Swis\JsonApi\Client\Tests\Mocks\Items\RelatedItem> + */ + public function hasoneRelation(): OneRelationInterface { return $this->hasOne(RelatedItem::class); } - public function hasmanyRelation() + /** + * @return \Swis\JsonApi\Client\Interfaces\ManyRelationInterface<\Swis\JsonApi\Client\Tests\Mocks\Items\RelatedItem> + */ + public function hasmanyRelation(): ManyRelationInterface { return $this->hasMany(RelatedItem::class); } - public function morphtoRelation() + /** + * @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface<\Swis\JsonApi\Client\Interfaces\ItemInterface> + */ + public function morphtoRelation(): OneRelationInterface { return $this->morphTo(); } - public function morphtomanyRelation() + /** + * @return \Swis\JsonApi\Client\Interfaces\ManyRelationInterface<\Swis\JsonApi\Client\Interfaces\ItemInterface> + */ + public function morphtomanyRelation(): ManyRelationInterface { return $this->morphToMany(); } diff --git a/tests/_mocks/Items/WithoutRelationshipsItem.php b/tests/_mocks/Items/WithoutRelationshipsItem.php index 3c8f291..a6a509f 100644 --- a/tests/_mocks/Items/WithoutRelationshipsItem.php +++ b/tests/_mocks/Items/WithoutRelationshipsItem.php @@ -8,8 +8,5 @@ class WithoutRelationshipsItem extends Item { - /** - * @var string - */ protected $type = 'item-without-relationships'; } diff --git a/tests/_mocks/MockRepository.php b/tests/_mocks/MockRepository.php index 9697e1d..d5d94ef 100644 --- a/tests/_mocks/MockRepository.php +++ b/tests/_mocks/MockRepository.php @@ -6,6 +6,9 @@ use Swis\JsonApi\Client\Repository; +/** + * @extends Repository<\Swis\JsonApi\Client\Tests\Mocks\ItemStub> + */ class MockRepository extends Repository { protected $endpoint = 'mocks'; diff --git a/types/Item.php b/types/Item.php new file mode 100644 index 0000000..73a5289 --- /dev/null +++ b/types/Item.php @@ -0,0 +1,13 @@ +child()->getData()); +assertType('Swis\JsonApi\Client\Collection|null', $item->children()->getData()); + +assertType('Swis\JsonApi\Client\Interfaces\ItemInterface|null', $item->morph()->getData()); +assertType('Swis\JsonApi\Client\Collection|null', $item->morphmany()->getData()); diff --git a/types/Repository.php b/types/Repository.php new file mode 100644 index 0000000..1e074dd --- /dev/null +++ b/types/Repository.php @@ -0,0 +1,27 @@ +all(); +assertType('Swis\JsonApi\Client\Interfaces\CollectionDocumentInterface', $all); +assertType('Swis\JsonApi\Client\Collection', $all->getData()); +assertType('Swis\JsonApi\Client\Tests\Mocks\ItemStub|null', $all->getData()->first()); + +$find = $repository->find('foo'); +assertType('Swis\JsonApi\Client\Interfaces\ItemDocumentInterface', $find); +assertType('Swis\JsonApi\Client\Tests\Mocks\ItemStub', $find->getData()); + +$take = $repository->take(); +assertType('Swis\JsonApi\Client\Interfaces\ItemDocumentInterface', $take); +assertType('Swis\JsonApi\Client\Tests\Mocks\ItemStub', $take->getData()); + +$save = $repository->save(new ItemStub); +assertType('Swis\JsonApi\Client\Interfaces\ItemDocumentInterface', $save); +assertType('Swis\JsonApi\Client\Tests\Mocks\ItemStub', $save->getData());