LatteSupport icon indicating copy to clipboard operation
LatteSupport copied to clipboard

Support for Enums

Open sasule opened this issue 8 months ago • 1 comments

Describe the bug Enums seem not to be supported

Environment (please complete the following information):

  • PhpStorm version [e.g. 2024.3.5]
  • Plugin version [e.g. 1.3.0]

To Reproduce Use enum in code

Code Enum:

namespace Order;

enum State: string
{
	case New = 'new';
	case Preparing = 'preparing';
	case ForTakeOut = 'takeOut';
	case Finished = 'finish';
	case Cancelled = 'cancel';
	case CancelRequest = 'cancelRequest';

}

Latte:

{if $order->getOrderState() === Order\State::ForTakeOut
				&& $order->getPaymentState() === Order\PaymentState::Waiting}
					<a n:href="setState!, $order->getId(), Order\State::Finished->value, forcePaid: true"
							class="btn btn-default mb-2"
					>
						<i class="fas fa-check-double"></i>
						{_messages.default.myEshop.orderManage.takenOut2}
					</a>
				{/if}

This code produces errors in PhpStorm: Constant not found. Also GoTo navigation does not work.

sasule avatar Apr 08 '25 07:04 sasule

I don't have much time for new features and this plugin is in maintenance mode only, but when I will have some time I'll look at it if it's gonna be something quick and I may add it, btw you could completely avoid it, because using full class definitions in template is not very good idea, (except in templateType), it's best just to use variables from {templateType}.

You can add functions to your enum like isWaiting(), isNew() etc. and use only those methods in latte file.

public function isNew(): bool
{
	return $this === self::New;
}

public function isWaiting(): bool
{
	return $this === self::Waiting;
}

I'm using it almost in every enum, conditions in latte are way easier, and you can also use it in PHP. I'm using Copilot to generate this methods, so there is not much manual typing anyways.

rixafy avatar Apr 09 '25 10:04 rixafy