1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Add a few return types and improve formatting

This commit is contained in:
Matthew Brown 2019-02-06 11:40:18 -05:00
parent d18ef9c6e1
commit 7249539fc6
4 changed files with 98 additions and 83 deletions

View File

@ -50,11 +50,14 @@ class TextDocument
* to request the current content of a text document identified by the URI
*
* @param TextDocumentIdentifier $textDocument The document to get the content for
* @return Promise <TextDocumentItem> The document's current content
* @return Promise<TextDocumentItem> The document's current content
*/
public function xcontent(TextDocumentIdentifier $textDocument): Promise
{
return call(
/**
* @return \Generator<int, Promise, mixed, TextDocumentItem>
*/
function () use ($textDocument) {
$result = yield $this->handler->request(
'textDocument/xcontent',

View File

@ -40,48 +40,53 @@ class ClientHandler
*
* @param string $method The method to call
* @param array|object $params The method parameters
* @return Promise <mixed> Resolved with the result of the request or rejected with an error
* @return Promise<mixed> Resolved with the result of the request or rejected with an error
*/
public function request(string $method, $params): Promise
{
$id = $this->idGenerator->generate();
return call(function () use ($id, $method, $params) {
yield $this->protocolWriter->write(
new Message(
new AdvancedJsonRpc\Request($id, $method, (object) $params)
)
);
return call(
/**
* @return \Generator<int, Promise, mixed, mixed>
*/
function () use ($id, $method, $params) {
yield $this->protocolWriter->write(
new Message(
new AdvancedJsonRpc\Request($id, $method, (object) $params)
)
);
$deferred = new Deferred();
$deferred = new Deferred();
$listener =
/**
* @param callable $listener
* @return void
*/
function (Message $msg) use ($id, $deferred, &$listener) {
error_log('request handler');
$listener =
/**
* @psalm-suppress UndefinedPropertyFetch
* @psalm-suppress MixedArgument
* @param callable $listener
* @return void
*/
if ($msg->body
&& AdvancedJsonRpc\Response::isResponse($msg->body)
&& $msg->body->id === $id
) {
// Received a response
$this->protocolReader->removeListener('message', $listener);
if (AdvancedJsonRpc\SuccessResponse::isSuccessResponse($msg->body)) {
$deferred->resolve($msg->body->result);
} else {
$deferred->fail($msg->body->error);
function (Message $msg) use ($id, $deferred, &$listener) {
error_log('request handler');
/**
* @psalm-suppress UndefinedPropertyFetch
* @psalm-suppress MixedArgument
*/
if ($msg->body
&& AdvancedJsonRpc\Response::isResponse($msg->body)
&& $msg->body->id === $id
) {
// Received a response
$this->protocolReader->removeListener('message', $listener);
if (AdvancedJsonRpc\SuccessResponse::isSuccessResponse($msg->body)) {
$deferred->resolve($msg->body->result);
} else {
$deferred->fail($msg->body->error);
}
}
}
};
$this->protocolReader->on('message', $listener);
return $deferred->promise();
});
};
$this->protocolReader->on('message', $listener);
return $deferred->promise();
}
);
}
/**

View File

@ -101,53 +101,58 @@ class LanguageServer extends AdvancedJsonRpc\Dispatcher
$this->protocolReader->on(
'message',
/** @return void */
asyncCoroutine(function (Message $msg) {
if (!$msg->body) {
return;
}
// Ignore responses, this is the handler for requests and notifications
if (AdvancedJsonRpc\Response::isResponse($msg->body)) {
return;
}
$result = null;
$error = null;
try {
// Invoke the method handler to get a result
/**
* @var Promise
* @psalm-suppress UndefinedClass
*/
$dispatched = $this->dispatch($msg->body);
$result = yield $dispatched;
} catch (AdvancedJsonRpc\Error $e) {
// If a ResponseError is thrown, send it back in the Response
$error = $e;
} catch (Throwable $e) {
// If an unexpected error occurred, send back an INTERNAL_ERROR error response
$error = new AdvancedJsonRpc\Error(
(string) $e,
AdvancedJsonRpc\ErrorCode::INTERNAL_ERROR,
null,
$e
);
}
// Only send a Response for a Request
// Notifications do not send Responses
asyncCoroutine(
/**
* @psalm-suppress UndefinedPropertyFetch
* @psalm-suppress MixedArgument
* @return Generator<int, \Amp\Promise, mixed, void>
*/
if (AdvancedJsonRpc\Request::isRequest($msg->body)) {
if ($error !== null) {
$responseBody = new AdvancedJsonRpc\ErrorResponse($msg->body->id, $error);
} else {
$responseBody = new AdvancedJsonRpc\SuccessResponse($msg->body->id, $result);
function (Message $msg) {
if (!$msg->body) {
return;
}
// Ignore responses, this is the handler for requests and notifications
if (AdvancedJsonRpc\Response::isResponse($msg->body)) {
return;
}
$result = null;
$error = null;
try {
// Invoke the method handler to get a result
/**
* @var Promise
* @psalm-suppress UndefinedClass
*/
$dispatched = $this->dispatch($msg->body);
$result = yield $dispatched;
} catch (AdvancedJsonRpc\Error $e) {
// If a ResponseError is thrown, send it back in the Response
$error = $e;
} catch (Throwable $e) {
// If an unexpected error occurred, send back an INTERNAL_ERROR error response
$error = new AdvancedJsonRpc\Error(
(string) $e,
AdvancedJsonRpc\ErrorCode::INTERNAL_ERROR,
null,
$e
);
}
// Only send a Response for a Request
// Notifications do not send Responses
/**
* @psalm-suppress UndefinedPropertyFetch
* @psalm-suppress MixedArgument
*/
if (AdvancedJsonRpc\Request::isRequest($msg->body)) {
if ($error !== null) {
$responseBody = new AdvancedJsonRpc\ErrorResponse($msg->body->id, $error);
} else {
$responseBody = new AdvancedJsonRpc\SuccessResponse($msg->body->id, $result);
}
yield $this->protocolWriter->write(new Message($responseBody));
}
yield $this->protocolWriter->write(new Message($responseBody));
}
})
)
);
$this->protocolReader->on(

View File

@ -41,14 +41,16 @@ class ProtocolStreamReader implements ProtocolReader
public function __construct($input)
{
$input = new ResourceInputStream($input);
asyncCall(function () use ($input): \Generator {
while (($chunk = yield $input->read()) !== null) {
/** @var string $chunk */
$this->readMessages($chunk);
}
asyncCall(
function () use ($input): \Generator {
while (($chunk = yield $input->read()) !== null) {
/** @var string $chunk */
$this->readMessages($chunk);
}
$this->emitClose();
});
$this->emitClose();
}
);
$this->on(
'close',