Skip to content
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

Add more precise types #220

Merged
merged 1 commit into from
Aug 9, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ class JsonErrorException extends \Exception implements OpenSearchException
10 => 'Attempted to decode nonexistent UTF-16 code-point' //JSON_ERROR_UTF16
);

public function __construct($code, $input, $result, $previous = null)
/**
* @param mixed $input
* @param mixed $result
*/
public function __construct(int $code, $input, $result, \Throwable $previous = null)
{
if (isset(self::$messages[$code]) !== true) {
throw new \InvalidArgumentException(sprintf('Encountered unknown JSON error code: [%d]', $code));
Expand Down
4 changes: 2 additions & 2 deletions src/OpenSearch/ConnectionPool/AbstractConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ abstract class AbstractConnectionPool implements ConnectionPoolInterface
protected $selector;

/**
* @var array
* @var array<string, mixed>
*/
protected $connectionPoolParams;

Expand All @@ -65,7 +65,7 @@ abstract class AbstractConnectionPool implements ConnectionPoolInterface
* @param ConnectionInterface[] $connections The Connections to choose from
* @param SelectorInterface $selector A Selector instance to perform the selection logic for the available connections
* @param ConnectionFactoryInterface $factory ConnectionFactory instance
* @param array $connectionPoolParams
* @param array<string, mixed> $connectionPoolParams
*/
public function __construct(array $connections, SelectorInterface $selector, ConnectionFactoryInterface $factory, array $connectionPoolParams)
{
Expand Down
3 changes: 2 additions & 1 deletion src/OpenSearch/ConnectionPool/SimpleConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
class SimpleConnectionPool extends AbstractConnectionPool implements ConnectionPoolInterface
{
/**
* {@inheritdoc}
* @param ConnectionInterface[] $connections
* @param array<string, mixed> $connectionPoolParams
*/
public function __construct($connections, SelectorInterface $selector, ConnectionFactoryInterface $factory, $connectionPoolParams)
{
Expand Down
9 changes: 8 additions & 1 deletion src/OpenSearch/ConnectionPool/SniffingConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ class SniffingConnectionPool extends AbstractConnectionPool
private $nextSniff;

/**
* {@inheritdoc}
* @param ConnectionInterface[] $connections
* @param array<string, mixed> $connectionPoolParams
*/
public function __construct(
$connections,
Expand Down Expand Up @@ -148,6 +149,9 @@ private function sniffConnection(Connection $connection): bool
return true;
}

/**
* @return list<array{host: string, port: int}>
*/
private function parseClusterState($nodeInfo): array
{
$pattern = '/([^:]*):(\d+)/';
Expand All @@ -167,6 +171,9 @@ private function parseClusterState($nodeInfo): array
return $hosts;
}

/**
* @param array<string, mixed> $connectionPoolParams
*/
private function setConnectionPoolParams(array $connectionPoolParams): void
{
$this->sniffingInterval = (int)($connectionPoolParams['sniffingInterval'] ?? 300);
Expand Down
3 changes: 2 additions & 1 deletion src/OpenSearch/ConnectionPool/StaticConnectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class StaticConnectionPool extends AbstractConnectionPool implements ConnectionP
private $maxPingTimeout = 3600;

/**
* {@inheritdoc}
* @param ConnectionInterface[] $connections
* @param array<string, mixed> $connectionPoolParams
*/
public function __construct($connections, SelectorInterface $selector, ConnectionFactoryInterface $factory, $connectionPoolParams)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ class StaticNoPingConnectionPool extends AbstractConnectionPool implements Conne
private $maxPingTimeout = 3600;

/**
* {@inheritdoc}
* @param ConnectionInterface[] $connections
* @param array<string, mixed> $connectionPoolParams
*/
public function __construct($connections, SelectorInterface $selector, ConnectionFactoryInterface $factory, $connectionPoolParams)
{
Expand Down
18 changes: 14 additions & 4 deletions src/OpenSearch/Connections/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Connection implements ConnectionInterface
protected $connectionParams;

/**
* @var array
* @var array<string, list<string>>
*/
protected $headers = [];

Expand Down Expand Up @@ -129,6 +129,10 @@ class Connection implements ConnectionInterface
*/
private $OSVersion = null;

/**
* @param array{host: string, port?: int, scheme?: string, user?: string, pass?: string, path?: string} $hostDetails
* @param array{client?: array{headers?: array<string, list<string>>, curl?: array<int, mixed>}} $connectionParams
*/
public function __construct(
callable $handler,
array $hostDetails,
Expand Down Expand Up @@ -192,10 +196,10 @@ public function __construct(
/**
* @param string $method
* @param string $uri
* @param null|array $params
* @param null|mixed $body
* @param null|array<string, mixed> $params
* @param mixed $body
* @param array $options
* @param Transport $transport
* @param Transport|null $transport
* @return mixed
*/
public function performRequest(string $method, string $uri, ?array $params = [], $body = null, array $options = [], Transport $transport = null)
Expand Down Expand Up @@ -344,6 +348,9 @@ function ($response) use ($connection, $transport, $request, $options) {
};
}

/**
* @param array<string, string|int|bool>|null $params
*/
private function getURI(string $uri, ?array $params): string
{
if (isset($params) === true && !empty($params)) {
Expand All @@ -370,6 +377,9 @@ function ($value) {
return $uri;
}

/**
* @return array<string, list<string>>
*/
public function getHeaders(): array
{
return $this->headers;
Expand Down
3 changes: 3 additions & 0 deletions src/OpenSearch/Connections/ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class ConnectionFactory implements ConnectionFactoryInterface
*/
private $handler;

/**
* @param array{client?: array{headers?: array<string, list<string>>, curl?: array<int, mixed>}} $connectionParams
*/
public function __construct(callable $handler, array $connectionParams, SerializerInterface $serializer, LoggerInterface $logger, LoggerInterface $tracer)
{
$this->handler = $handler;
Expand Down
3 changes: 3 additions & 0 deletions src/OpenSearch/Connections/ConnectionFactoryInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@

interface ConnectionFactoryInterface
{
/**
* @param array{host: string, port?: int, scheme?: string, user?: string, pass?: string, path?: string} $hostDetails
*/
public function create(array $hostDetails): ConnectionInterface;
}
3 changes: 2 additions & 1 deletion src/OpenSearch/Connections/ConnectionInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ public function markDead(): void;
public function getLastRequestInfo(): array;

/**
* @param null $body
* @param array<string, mixed>|null $params
* @param mixed $body
* @return mixed
*/
public function performRequest(string $method, string $uri, ?array $params = [], $body = null, array $options = [], Transport $transport = null);
Expand Down
13 changes: 10 additions & 3 deletions src/OpenSearch/Endpoints/AbstractEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ public function getIndex(): ?string
}

/**
* @param string|string[]|null $index
*
* @return $this
*/
public function setIndex($index)
Expand Down Expand Up @@ -188,7 +190,7 @@ private function getOptionalIndex(): string
}

/**
* @param array $params
* @param array<string, mixed> $params
*
* @throws UnexpectedValueException
*/
Expand Down Expand Up @@ -218,7 +220,7 @@ private function checkUserParams(array $params)
}

/**
* @param array $params Note: this is passed by-reference!
* @param array<string, mixed> $params Note: this is passed by-reference!
*/
private function extractOptions(&$params)
{
Expand Down Expand Up @@ -248,6 +250,11 @@ private function extractOptions(&$params)
}
}

/**
* @param array<string, mixed> $params
*
* @return array<string, mixed>
*/
private function convertCustom(array $params): array
{
if (isset($params['custom']) === true) {
Expand Down Expand Up @@ -285,7 +292,7 @@ private function isNestedArray(array $a): bool
}

/**
* This function returns all param deprecations also optional with an replacement field
* This function returns all param deprecations also optional with a replacement field
*
* @return array<string, string|null>
*/
Expand Down
23 changes: 23 additions & 0 deletions src/OpenSearch/Handlers/SigV4Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,22 @@
use Psr\Http\Message\RequestInterface;
use RuntimeException;

/**
* @phpstan-type RingPhpRequest array{http_method: string, scheme: string, uri: string, query_string?: string, version?: string, headers: array<string, list<string>>, body: string|resource|null, client?: array<string, mixed>}
*/
class SigV4Handler
{
/**
* @var SignatureV4
*/
private $signer;
/**
* @var callable
*/
private $credentialProvider;
/**
* @var callable
*/
private $wrappedHandler;

/**
Expand Down Expand Up @@ -46,6 +58,9 @@ public function __construct(
?: CredentialProvider::defaultProvider();
}

/**
* @phpstan-param RingPhpRequest $request
*/
public function __invoke(array $request)
{
$creds = call_user_func($this->credentialProvider)->wait();
Expand All @@ -66,6 +81,9 @@ public static function assertDependenciesInstalled(): void
}
}

/**
* @phpstan-param RingPhpRequest $ringPhpRequest
*/
private function createPsr7Request(array $ringPhpRequest): Request
{
// fix for uppercase 'Host' array key in elasticsearch-php 5.3.1 and backward compatible
Expand Down Expand Up @@ -96,6 +114,11 @@ private function createPsr7Request(array $ringPhpRequest): Request
);
}

/**
* @phpstan-param RingPhpRequest $originalRequest
*
* @phpstan-return RingPhpRequest
*/
private function createRingRequest(RequestInterface $request, array $originalRequest): array
{
$uri = $request->getUri();
Expand Down
2 changes: 1 addition & 1 deletion src/OpenSearch/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public function getConnection(): ConnectionInterface
*
* @param string $method HTTP method to use
* @param string $uri HTTP URI to send request to
* @param array $params Optional query parameters
* @param array<string, mixed> $params Optional query parameters
* @param mixed|null $body Optional query body
* @param array $options
*
Expand Down
2 changes: 1 addition & 1 deletion tests/ClientIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
class ClientIntegrationTest extends \PHPUnit\Framework\TestCase
{
/**
* ArrayLogger
* @var ArrayLogger
*/
private $logger;

Expand Down
3 changes: 3 additions & 0 deletions tests/Handlers/SigV4HandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class SigV4HandlerTest extends TestCase
{
private const ENV_KEYS_USED = [CredentialProvider::ENV_KEY, CredentialProvider::ENV_SECRET];

/**
* @var array<string, string|false>
*/
private $envTemp = [];

protected function setUp(): void
Expand Down
5 changes: 4 additions & 1 deletion tests/Utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
class Utility
{
/**
* @var array|null
* @var array{number: string, distribution?: string, build_flavor: string, build_date: string, build_hash: string, build_snapshot: bool, build_type: string, lucene_version: string, minimum_index_compatibility_version: string, minimum_wire_compatibility_version: string}|null
*/
private static $version;

Expand Down Expand Up @@ -110,6 +110,9 @@ public static function isElasticSearchVersionAtLeast(Client $client, string $ver
return version_compare($versionNumber, $version) >= 0;
}

/**
* @return array{number: string, distribution?: string, build_flavor: string, build_date: string, build_hash: string, build_snapshot: bool, build_type: string, lucene_version: string, minimum_index_compatibility_version: string, minimum_wire_compatibility_version: string}
*/
private static function getVersion(Client $client): array
{
if (!isset(self::$version)) {
Expand Down
Loading