Skip to content
This repository was archived by the owner on Feb 24, 2025. It is now read-only.

Commit a1e4cf0

Browse files
committed
Add generic type hints
This allows type-hinting the api methods parameters and result by using generics.
1 parent e1397ab commit a1e4cf0

File tree

5 files changed

+60
-44
lines changed

5 files changed

+60
-44
lines changed

composer.lock

Lines changed: 38 additions & 38 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/index.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@
99

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

12-
// API Handler which actually contains the business logic
12+
/**
13+
* API Handler which actually contains the business logic
14+
*
15+
* @implements ApiMethodInterface<
16+
* object&stdClass,
17+
* array{beer_style_list: list<string>}
18+
* >
19+
*/
1320
final class BeerlistMethod implements ApiMethodInterface
1421
{
1522
public function handle(

phpunit.xml.dist

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
backupGlobals="true"
55
bootstrap="tests/bootstrap.php"
66
colors="true"
7-
cacheDirectory="build/.phpunit.cache">
7+
cacheDirectory="build/.phpunit.cache"
8+
displayDetailsOnTestsThatTriggerDeprecations="true"
9+
displayDetailsOnTestsThatTriggerWarnings="true"
10+
>
811
<coverage/>
912
<source>
1013
<include>

src/Contract/ApiMethodInterface.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,26 @@
55
namespace Usox\JsonSchemaApi\Contract;
66

77
use Psr\Http\Message\ServerRequestInterface;
8-
use stdClass;
98
use Usox\JsonSchemaApi\Exception\ApiMethodException;
109

10+
/**
11+
* @template TParameter of object
12+
* @template TResult of array
13+
*/
1114
interface ApiMethodInterface
1215
{
1316
/**
1417
* This method contains the business logic of the api method
1518
*
16-
* @param stdClass $parameter The method parameter as described in the schema
19+
* @param TParameter $parameter The method parameter as described in the schema
1720
*
18-
* @return array<mixed, mixed> The api method response
21+
* @return TResult The api method response
1922
*
2023
* @throws ApiMethodException
2124
*/
2225
public function handle(
2326
ServerRequestInterface $request,
24-
stdClass $parameter
27+
object $parameter
2528
): array;
2629

2730
/**

src/Contract/MethodProviderInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
*/
1010
interface MethodProviderInterface
1111
{
12+
/**
13+
* @return null|ApiMethodInterface<object, array<mixed>>
14+
*/
1215
public function lookup(
1316
string $methodName
1417
): ?ApiMethodInterface;

0 commit comments

Comments
 (0)