Skip to content

Add support for generics #108

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

/** @implements \Swis\JsonApi\Client\Interfaces\RepositoryInterface<BlogItem> */
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<AuthorItem> */
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<AuthorItem> */
public function comments(): ManyRelationInterface
{
return $this->hasMany(CommentItem::class);
}

```

## Advanced usage

Expand Down
4 changes: 0 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@
},
"license": "MIT",
"authors": [
{
"name": "Hendrik Nijmeijer",
"email": "[email protected]"
},
{
"name": "Jasper Zonneveld",
"email": "[email protected]",
Expand Down
6 changes: 6 additions & 0 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

use Swis\JsonApi\Client\Interfaces\DataInterface;

/**
* @template TKey of array-key
* @template TValue of \Swis\JsonApi\Client\Interfaces\ItemInterface
*
* @extends \Illuminate\Support\Collection<TKey, TValue>
*/
class Collection extends \Illuminate\Support\Collection implements DataInterface
{
/**
Expand Down
8 changes: 6 additions & 2 deletions src/Concerns/HasRelations.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ trait HasRelations
/**
* Create a singular relation to another item.
*
* @template TItem of \Swis\JsonApi\Client\Interfaces\ItemInterface
*
* @return \Swis\JsonApi\Client\Relations\HasOneRelation
* @param class-string<TItem> $itemClass
* @return \Swis\JsonApi\Client\Interfaces\OneRelationInterface<TItem>
*/
public function hasOne(string $itemClass, ?string $name = null): OneRelationInterface
{
Expand All @@ -48,8 +50,10 @@ protected function newHasOne(string $type): OneRelationInterface
/**
* Create a plural relation to another item.
*
* @template TItem of \Swis\JsonApi\Client\Interfaces\ItemInterface
*
* @return \Swis\JsonApi\Client\Relations\HasManyRelation
* @param class-string<TItem> $itemClass
* @return \Swis\JsonApi\Client\Interfaces\ManyRelationInterface<TItem>
*/
public function hasMany(string $itemClass, ?string $name = null): ManyRelationInterface
{
Expand Down
5 changes: 4 additions & 1 deletion src/Interfaces/CollectionDocumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<array-key, TItem>
*/
public function getData();
}
5 changes: 4 additions & 1 deletion src/Interfaces/ItemDocumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
11 changes: 11 additions & 0 deletions src/Interfaces/ManyRelationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,20 @@
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<array-key, TItem>|null $data
* @return static
*/
public function setData(?Collection $data);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Return static also points at a little weirdness, why isn't that type hinted? And should it be?


/**
* @return \Swis\JsonApi\Client\Collection<array-key, TItem>|null
*/
public function getData(): ?Collection;

public function hasData(): bool;
Expand All @@ -23,6 +33,7 @@ public function getIncluded(): Collection;
public function hasIncluded(): bool;

/**
* @param \Swis\JsonApi\Client\Collection<array-key, TItem> $included
* @return static
*/
public function associate(Collection $included);
Expand Down
20 changes: 20 additions & 0 deletions src/Interfaces/OneRelationInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,38 @@
use Swis\JsonApi\Client\Links;
use Swis\JsonApi\Client\Meta;

/**
* @template TItem of \Swis\JsonApi\Client\Interfaces\ItemInterface
*/
interface OneRelationInterface
{
/**
* @param TItem|null $data
* @return static
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same static comment.

*/
public function setData(?ItemInterface $data);

/**
* @return TItem|null
*/
public function getData(): ?ItemInterface;

public function hasData(): bool;

/**
* @param TItem|null $included
*/
public function setIncluded(?ItemInterface $included);

/**
* @return TItem|null
*/
public function getIncluded(): ?ItemInterface;

public function hasIncluded(): bool;

/**
* @param TItem $included
* @return static
*/
public function associate(ItemInterface $included);
Expand All @@ -31,6 +48,9 @@ public function associate(ItemInterface $included);
*/
public function dissociate();

/**
* @return TItem|null
*/
public function getAssociated(): ?ItemInterface;

public function hasAssociated(): bool;
Expand Down
9 changes: 6 additions & 3 deletions src/Interfaces/RepositoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,23 @@

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<TItem>
*/
public function all();

/**
* @return \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface
* @return \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface<TItem>
*/
public function find(string $id);
Copy link
Member

@bbrala bbrala Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When i check the tests, i think this return type is not correct. It seems this can also return a DocumentInterface. See RepositoryTest::it_can_find_one.

This could also mean the interface on the Document is wrong. seems a little weird, since ItemDocument is pretty much the same class with the extra getData method.

Think there is something wrong here. @JaZo any insights?

Copy link
Author

@HendrikN HendrikN Apr 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I had the same questions, but I didnt want to touch actual code in this PR… ItemDocument seems to follow the spec though…

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would probably make it documentinterface, since itemdocumentinterface does extend documentinterface. But that will mean some errors later with phpstan when we are calling getData.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would also make this PR mostly obsolete if find() would not resolve to an Item 😋

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The primary reason to have a CollectionDocumentInterface and an ItemDocumentInterface is to differentiate between the return types of getData. The former returns a collection and the latter an item. This stems from ancient times when generics weren't a thing. I'm okay with changing that to generics, but that would probably be a breaking change, so maybe we should leave that for now and just add a todo.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m fine with that


/**
* @return \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface
* @return \Swis\JsonApi\Client\Interfaces\ItemDocumentInterface<TItem>
*/
public function save(ItemInterface $item);

Expand Down