Skip to content

Commit 69c7bff

Browse files
authored
Add signature for callable types (#1463)
* Add more precise types for the Stream classes * Generate more precise type for streaming shape members * Enforce defining callable signatures
1 parent e3e24bf commit 69c7bff

File tree

13 files changed

+52
-13
lines changed

13 files changed

+52
-13
lines changed

phpstan.neon.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
parameters:
22
level: 5
33
treatPhpDocTypesAsCertain: false
4+
checkMissingCallableSignature: true
45
reportUnmatchedIgnoredErrors: false
56
paths:
67
- src

src/CodeGenerator/src/Generator/CodeGenerator/TypeGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function generateDocblock(StructureShape $shape, ClassName $shapeClassNam
8989
$param = 'array<string, ' . $param . '>';
9090
}
9191
} elseif ($member->isStreaming()) {
92-
$param = 'string|resource|callable|iterable';
92+
$param = 'string|resource|(callable(int): string)|iterable<string>';
9393
} elseif ('timestamp' === $param = $memberShape->getType()) {
9494
$param = $isObject ? '\DateTimeImmutable' : '\DateTimeImmutable|string';
9595
} else {

src/CodeGenerator/src/Generator/InputGenerator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ public function generate(Operation $operation): ClassName
210210
$constructorBody .= strtr('$this->PROPERTY = $input["NAME"] ?? null;' . "\n", ['PROPERTY' => GeneratorHelper::normalizeName($member->getName()), 'NAME' => $member->getName()]);
211211
}
212212
} elseif ($member->isStreaming()) {
213-
$parameterType = 'string|resource|callable|iterable';
213+
$parameterType = 'string|resource|(callable(int): string)|iterable<string>';
214214
$returnType = null;
215215
$constructorBody .= strtr('$this->PROPERTY = $input["NAME"] ?? null;' . "\n", ['PROPERTY' => GeneratorHelper::normalizeName($member->getName()), 'NAME' => $member->getName()]);
216216
} elseif ('timestamp' === $memberShape->getType()) {

src/Core/src/Stream/CallableStream.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,28 @@
1414
*/
1515
final class CallableStream implements ReadOnceResultStream, RequestStream
1616
{
17+
/**
18+
* @var callable(int): string
19+
*/
1720
private $content;
1821

22+
/**
23+
* @var int
24+
*/
1925
private $chunkSize;
2026

27+
/**
28+
* @param callable(int): string $content
29+
*/
2130
private function __construct(callable $content, int $chunkSize = 64 * 1024)
2231
{
2332
$this->content = $content;
2433
$this->chunkSize = $chunkSize;
2534
}
2635

36+
/**
37+
* @param self|callable(int): string $content
38+
*/
2739
public static function create($content, int $chunkSize = 64 * 1024): CallableStream
2840
{
2941
if ($content instanceof self) {

src/Core/src/Stream/IterableStream.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,22 @@
1313
*/
1414
final class IterableStream implements ReadOnceResultStream, RequestStream
1515
{
16+
/**
17+
* @var iterable<string>
18+
*/
1619
private $content;
1720

21+
/**
22+
* @param iterable<string> $content
23+
*/
1824
private function __construct(iterable $content)
1925
{
2026
$this->content = $content;
2127
}
2228

29+
/**
30+
* @param self|iterable<string> $content
31+
*/
2332
public static function create($content): IterableStream
2433
{
2534
if ($content instanceof self) {

src/Core/src/Stream/RequestStream.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
* @author Jérémy Derussé <[email protected]>
99
*
1010
* @internal
11+
*
12+
* @extends \IteratorAggregate<string>
1113
*/
1214
interface RequestStream extends \IteratorAggregate
1315
{

src/Core/src/Stream/ResourceStream.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,18 @@ final class ResourceStream implements RequestStream
2020

2121
private $chunkSize;
2222

23+
/**
24+
* @param resource $content
25+
*/
2326
private function __construct($content, int $chunkSize = 64 * 1024)
2427
{
2528
$this->content = $content;
2629
$this->chunkSize = $chunkSize;
2730
}
2831

32+
/**
33+
* @param self|resource $content
34+
*/
2935
public static function create($content, int $chunkSize = 64 * 1024): ResourceStream
3036
{
3137
if ($content instanceof self) {

src/Core/src/Stream/RewindableStream.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@
1717
*/
1818
final class RewindableStream implements RequestStream
1919
{
20+
/**
21+
* @var RequestStream
22+
*/
2023
private $content;
2124

2225
/**
23-
* @var RequestStream
26+
* @var RequestStream|null
2427
*/
2528
private $fallback;
2629

src/Core/src/Stream/StreamFactory.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111
*/
1212
class StreamFactory
1313
{
14+
/**
15+
* @param string|resource|(callable(int): string)|iterable<string>|null $content
16+
*/
1417
public static function create($content, int $preferredChunkSize = 64 * 1024): RequestStream
1518
{
1619
if (null === $content || \is_string($content)) {

src/Core/src/Stream/StringStream.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ private function __construct(string $content)
2222
$this->content = $content;
2323
}
2424

25+
/**
26+
* @param RequestStream|string $content
27+
*/
2528
public static function create($content): StringStream
2629
{
2730
if ($content instanceof self) {

0 commit comments

Comments
 (0)