Skip to content

Commit

Permalink
Add generic type hints
Browse files Browse the repository at this point in the history
This allows type-hinting the api methods parameters and result by using
generics.
  • Loading branch information
usox committed Oct 31, 2023
1 parent e1397ab commit a1e4cf0
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 44 deletions.
76 changes: 38 additions & 38 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion example/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@

require_once __DIR__ . '/../vendor/autoload.php';

// API Handler which actually contains the business logic
/**
* API Handler which actually contains the business logic
*
* @implements ApiMethodInterface<
* object&stdClass,
* array{beer_style_list: list<string>}
* >
*/
final class BeerlistMethod implements ApiMethodInterface
{
public function handle(
Expand Down
5 changes: 4 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
backupGlobals="true"
bootstrap="tests/bootstrap.php"
colors="true"
cacheDirectory="build/.phpunit.cache">
cacheDirectory="build/.phpunit.cache"
displayDetailsOnTestsThatTriggerDeprecations="true"
displayDetailsOnTestsThatTriggerWarnings="true"
>
<coverage/>
<source>
<include>
Expand Down
11 changes: 7 additions & 4 deletions src/Contract/ApiMethodInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,26 @@
namespace Usox\JsonSchemaApi\Contract;

use Psr\Http\Message\ServerRequestInterface;
use stdClass;
use Usox\JsonSchemaApi\Exception\ApiMethodException;

/**
* @template TParameter of object
* @template TResult of array
*/
interface ApiMethodInterface
{
/**
* This method contains the business logic of the api method
*
* @param stdClass $parameter The method parameter as described in the schema
* @param TParameter $parameter The method parameter as described in the schema
*
* @return array<mixed, mixed> The api method response
* @return TResult The api method response
*
* @throws ApiMethodException
*/
public function handle(
ServerRequestInterface $request,
stdClass $parameter
object $parameter
): array;

/**
Expand Down
3 changes: 3 additions & 0 deletions src/Contract/MethodProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
*/
interface MethodProviderInterface
{
/**
* @return null|ApiMethodInterface<object, array<mixed>>
*/
public function lookup(
string $methodName
): ?ApiMethodInterface;
Expand Down

0 comments on commit a1e4cf0

Please sign in to comment.