1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Ensure correct method call order

This commit is contained in:
Bruce Weirdan 2023-07-26 03:59:59 +02:00
parent 4f6fc3585b
commit c2a05c2e90
No known key found for this signature in database
GPG Key ID: CFC3AAB181751B0D
2 changed files with 52 additions and 10 deletions

View File

@ -10,6 +10,12 @@ use Psalm\Internal\LanguageServer\ClientHandler;
/** @internal */
final class LegacyProgress implements ProgressInterface
{
private const STATUS_INACTIVE = 'inactive';
private const STATUS_ACTIVE = 'active';
private const STATUS_FINISHED = 'finished';
private string $status = self::STATUS_INACTIVE;
private ClientHandler $handler;
private ?string $title = null;
@ -20,19 +26,30 @@ final class LegacyProgress implements ProgressInterface
public function begin(string $title, ?string $message = null, ?int $percentage = null): void
{
if ($this->title !== null) {
if ($this->status === self::STATUS_ACTIVE) {
throw new LogicException('Progress has already been started');
}
if ($this->status === self::STATUS_FINISHED) {
throw new LogicException('Progress has already been finished');
}
$this->title = $title;
$this->notify($message);
$this->status = self::STATUS_ACTIVE;
}
public function update(?string $message = null, ?int $percentage = null): void
{
if ($this->title === null) {
throw new LogicException('The progress has not been started yet');
if ($this->status === self::STATUS_FINISHED) {
throw new LogicException('Progress has already been finished');
}
if ($this->status === self::STATUS_INACTIVE) {
throw new LogicException('Progress has not been started yet');
}
$this->notify($message);
@ -40,11 +57,17 @@ final class LegacyProgress implements ProgressInterface
public function end(?string $message = null): void
{
if ($this->title === null) {
throw new LogicException('The progress has not been started yet');
if ($this->status === self::STATUS_FINISHED) {
throw new LogicException('Progress has already been finished');
}
if ($this->status === self::STATUS_INACTIVE) {
throw new LogicException('Progress has not been started yet');
}
$this->notify($message);
$this->status = self::STATUS_FINISHED;
}
private function notify(?string $message): void

View File

@ -8,10 +8,15 @@ use Psalm\Internal\LanguageServer\ClientHandler;
/** @internal */
final class Progress implements ProgressInterface
{
private const STATUS_INACTIVE = 'inactive';
private const STATUS_ACTIVE = 'active';
private const STATUS_FINISHED = 'finished';
private string $status = self::STATUS_INACTIVE;
private ClientHandler $handler;
private string $token;
private bool $withPercentage = false;
private bool $finished = false;
public function __construct(ClientHandler $handler, string $token)
{
@ -24,7 +29,11 @@ final class Progress implements ProgressInterface
?string $message = null,
?int $percentage = null
): void {
if ($this->finished) {
if ($this->status === self::STATUS_ACTIVE) {
throw new LogicException('Progress has already been started');
}
if ($this->status === self::STATUS_FINISHED) {
throw new LogicException('Progress has already been finished');
}
@ -46,14 +55,20 @@ final class Progress implements ProgressInterface
}
$this->handler->notify('$/progress', $notification);
$this->status = self::STATUS_ACTIVE;
}
public function end(?string $message = null): void
{
if ($this->finished) {
if ($this->status === self::STATUS_FINISHED) {
throw new LogicException('Progress has already been finished');
}
if ($this->status === self::STATUS_INACTIVE) {
throw new LogicException('Progress has not been started yet');
}
$notification = [
'token' => $this->token,
'value' => [
@ -67,15 +82,19 @@ final class Progress implements ProgressInterface
$this->handler->notify('$/progress', $notification);
$this->finished = true;
$this->status = self::STATUS_FINISHED;
}
public function update(?string $message = null, ?int $percentage = null): void
{
if ($this->finished) {
if ($this->status === self::STATUS_FINISHED) {
throw new LogicException('Progress has already been finished');
}
if ($this->status === self::STATUS_INACTIVE) {
throw new LogicException('Progress has not been started yet');
}
$notification = [
'token' => $this->token,
'value' => [