← 输入框|github.com|刷新
Skip to content

Commit 1cbbce1

Browse files
committed
fix(types): declare stream return types on Docker methods
The Docker overrides inherited the generated return types, which say containerLogs(), execStart(), imageCreate() and the other streaming calls return null. They return DockerRawStream, AttachWebsocketStream, BuildStream, CreateImageStream, PushStream or EventStream. Declare the real conditional return types and check them with PHPStan assertType() in tests/StaticAnalysis, which the phpstan script now analyses. Closes #321
1 parent 2165415 commit 1cbbce1

3 files changed

Lines changed: 51 additions & 2 deletions

File tree

‎composer.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
"lint": "vendor/bin/parallel-lint --exclude vendor .",
6969
"php-cs-fixer": "vendor/bin/php-cs-fixer fix --dry-run --verbose --diff",
7070
"php-cs-fixer-fix": "vendor/bin/php-cs-fixer fix --verbose",
71-
"phpstan": "vendor/bin/phpstan analyse --level 2 src",
71+
"phpstan": "vendor/bin/phpstan analyse --level 2 src tests/StaticAnalysis",
7272
"phpunit": "vendor/bin/phpunit ./tests/ --coverage-html=./report/coverage/ --testdox-html=./report/testdox.html --disallow-test-output --process-isolation",
7373
"test": [
7474
"@lint",

‎src/Docker.php‎

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,16 @@
1919
use Docker\Endpoint\SystemEvents;
2020
use Docker\Exception\BadRequestException;
2121
use Docker\Http\InteractiveHttpClient;
22+
use Docker\Stream\AttachWebsocketStream;
23+
use Docker\Stream\BuildStream;
24+
use Docker\Stream\CreateImageStream;
25+
use Docker\Stream\DockerRawStream;
26+
use Docker\Stream\EventStream;
2227
use Docker\Stream\InteractiveExecStream;
28+
use Docker\Stream\PushStream;
2329
use Docker\Stream\SocketReadStream;
2430
use Http\Client\Socket\Stream as SocketStream;
31+
use Psr\Http\Message\ResponseInterface;
2532

2633
/**
2734
* Docker\Docker.
@@ -32,6 +39,8 @@ class Docker extends Client
3239

3340
/**
3441
* {@inheritdoc}
42+
*
43+
* @return ($fetch is 'object' ? DockerRawStream|null : ResponseInterface)
3544
*/
3645
public function containerAttach(string $id, array $queryParameters = [], string $fetch = self::FETCH_OBJECT, array $accept = [])
3746
{
@@ -40,6 +49,8 @@ public function containerAttach(string $id, array $queryParameters = [], string
4049

4150
/**
4251
* {@inheritdoc}
52+
*
53+
* @return ($fetch is 'object' ? AttachWebsocketStream|null : ResponseInterface)
4354
*/
4455
public function containerAttachWebsocket(string $id, array $queryParameters = [], string $fetch = self::FETCH_OBJECT, array $accept = [])
4556
{
@@ -48,6 +59,8 @@ public function containerAttachWebsocket(string $id, array $queryParameters = []
4859

4960
/**
5061
* {@inheritdoc}
62+
*
63+
* @return ($fetch is 'object' ? DockerRawStream|null : ResponseInterface)
5164
*/
5265
public function containerLogs(string $id, array $queryParameters = [], string $fetch = self::FETCH_OBJECT, array $accept = [])
5366
{
@@ -61,6 +74,8 @@ public function containerLogs(string $id, array $queryParameters = [], string $f
6174

6275
/**
6376
* {@inheritdoc}
77+
*
78+
* @return ($fetch is 'object' ? DockerRawStream|null : ResponseInterface)
6479
*/
6580
public function execStart(string $id, ?ExecIdStartPostBody $requestBody = null, string $fetch = self::FETCH_OBJECT, array $accept = [])
6681
{
@@ -96,7 +111,7 @@ public function execStartInteractive(string $id, ?ExecIdStartPostBody $requestBo
96111
|| !preg_match('/(?:^|,)\s*upgrade\s*(?:,|$)/i', $response->getHeaderLine('Connection'))
97112
|| $response->hasHeader('Transfer-Encoding')
98113
|| $response->hasHeader('Content-Encoding')
99-
|| !\in_array($mediaType, [\Docker\Stream\DockerRawStream::HEADER, \Docker\Stream\DockerRawStream::MULTIPLEXED_HEADER], true)
114+
|| !\in_array($mediaType, [DockerRawStream::HEADER, DockerRawStream::MULTIPLEXED_HEADER], true)
100115
|| (!$stream instanceof SocketReadStream && !$stream instanceof SocketStream)) {
101116
$stream->close();
102117
throw new \RuntimeException('Docker did not provide a supported interactive exec upgrade (HTTP '.$response->getStatusCode().').');
@@ -115,6 +130,8 @@ public function execStartInteractive(string $id, ?ExecIdStartPostBody $requestBo
115130

116131
/**
117132
* {@inheritdoc}
133+
*
134+
* @return ($fetch is 'object' ? BuildStream|null : ResponseInterface)
118135
*/
119136
public function imageBuild($requestBody = null, array $queryParameters = [], array $headerParameters = [], string $fetch = self::FETCH_OBJECT)
120137
{
@@ -123,12 +140,19 @@ public function imageBuild($requestBody = null, array $queryParameters = [], arr
123140

124141
/**
125142
* {@inheritdoc}
143+
*
144+
* @return ($fetch is 'object' ? CreateImageStream|null : ResponseInterface)
126145
*/
127146
public function imageCreate(?string $requestBody = null, array $queryParameters = [], array $headerParameters = [], string $fetch = self::FETCH_OBJECT)
128147
{
129148
return $this->executeEndpoint(new ImageCreate($requestBody, $queryParameters, $headerParameters), $fetch);
130149
}
131150

151+
/**
152+
* {@inheritdoc}
153+
*
154+
* @return ($fetch is 'object' ? PushStream|null : ResponseInterface)
155+
*/
132156
public function imagePush(string $name, array $queryParameters = [], array $headerParameters = [], string $fetch = self::FETCH_OBJECT, array $accept = [])
133157
{
134158
if (isset($headerParameters['X-Registry-Auth']) && $headerParameters['X-Registry-Auth'] instanceof AuthConfig) {
@@ -140,6 +164,8 @@ public function imagePush(string $name, array $queryParameters = [], array $head
140164

141165
/**
142166
* {@inheritdoc}
167+
*
168+
* @return ($fetch is 'object' ? EventStream|null : ResponseInterface)
143169
*/
144170
public function systemEvents(array $queryParameters = [], string $fetch = self::FETCH_OBJECT)
145171
{
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Docker\Tests\StaticAnalysis;
6+
7+
use Docker\Docker;
8+
9+
use function PHPStan\Testing\assertType;
10+
11+
// Analysed by PHPStan, not run by PHPUnit.
12+
function dockerReturnTypes(Docker $docker): void
13+
{
14+
assertType('Docker\Stream\DockerRawStream|null', $docker->containerAttach('id'));
15+
assertType('Docker\Stream\AttachWebsocketStream|null', $docker->containerAttachWebsocket('id'));
16+
assertType('Docker\Stream\DockerRawStream|null', $docker->containerLogs('id'));
17+
assertType('Docker\Stream\DockerRawStream|null', $docker->execStart('id'));
18+
assertType('Docker\Stream\BuildStream|null', $docker->imageBuild());
19+
assertType('Docker\Stream\CreateImageStream|null', $docker->imageCreate());
20+
assertType('Docker\Stream\PushStream|null', $docker->imagePush('name'));
21+
assertType('Docker\Stream\EventStream|null', $docker->systemEvents());
22+
assertType('Psr\Http\Message\ResponseInterface', $docker->containerLogs('id', [], Docker::FETCH_RESPONSE));
23+
}

0 commit comments

Comments
 (0)