2022-02-18 18:29:09 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Psalm\Tests\LanguageServer;
|
|
|
|
|
2022-04-28 20:12:50 +02:00
|
|
|
use AdvancedJsonRpc\Error;
|
|
|
|
use AdvancedJsonRpc\ErrorCode;
|
|
|
|
use AdvancedJsonRpc\ErrorResponse;
|
2022-02-18 18:29:09 +01:00
|
|
|
use AdvancedJsonRpc\Message as AdvancedJsonRpcMessage;
|
|
|
|
use AdvancedJsonRpc\Notification;
|
|
|
|
use AdvancedJsonRpc\Request;
|
|
|
|
use AdvancedJsonRpc\SuccessResponse;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base message
|
|
|
|
*/
|
|
|
|
abstract class Message extends AdvancedJsonRpcMessage
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Returns the appropriate Message subclass
|
|
|
|
*
|
|
|
|
* @param array $msg
|
|
|
|
*/
|
|
|
|
public static function parseArray(array $msg): AdvancedJsonRpcMessage
|
|
|
|
{
|
|
|
|
$decoded = (object) $msg;
|
|
|
|
if (Notification::isNotification($decoded)) {
|
2022-04-28 20:12:50 +02:00
|
|
|
/** @psalm-suppress MixedArgument */
|
2022-02-18 18:29:09 +01:00
|
|
|
$obj = new Notification($decoded->method, $decoded->params ?? null);
|
2022-04-28 20:12:50 +02:00
|
|
|
} elseif (Request::isRequest($decoded)) {
|
|
|
|
/** @psalm-suppress MixedArgument */
|
2022-02-18 18:29:09 +01:00
|
|
|
$obj = new Request($decoded->id, $decoded->method, $decoded->params ?? null);
|
2022-04-28 20:12:50 +02:00
|
|
|
} elseif (SuccessResponse::isSuccessResponse($decoded)) {
|
|
|
|
/** @psalm-suppress MixedArgument */
|
2022-02-18 18:29:09 +01:00
|
|
|
$obj = new SuccessResponse($decoded->id, $decoded->result);
|
2022-04-28 20:12:50 +02:00
|
|
|
} elseif (ErrorResponse::isErrorResponse($decoded)) {
|
|
|
|
/** @psalm-suppress MixedArgument, MixedPropertyFetch */
|
2022-02-18 18:29:09 +01:00
|
|
|
$obj = new ErrorResponse($decoded->id, new Error($decoded->error->message, $decoded->error->code, $decoded->error->data ?? null));
|
|
|
|
} else {
|
|
|
|
throw new Error('Invalid message', ErrorCode::INVALID_REQUEST);
|
|
|
|
}
|
|
|
|
return $obj;
|
|
|
|
}
|
|
|
|
}
|