Create enum

From shopsys/shopsys · View interactive version

Create enum is an AI agent skill from the GitHub repository shopsys/shopsys. Create a Shopsys-style enum (a class extending AbstractEnum with public typed constants, NOT a native PHP enum). Use when you need to define a fixed set of values that downstream projects (like project-base) might extend or restrict.

How to use this skill

Skill file

Create a Shopsys-style enum

Native PHP enums (enum Foo: string { case BAR = 'bar'; }) are implicitly final and cannot be extended. Downstream Shopsys projects (notably project-base) routinely extend framework classes to add domain-specific cases or hide unused ones, so the project uses a class-based enum pattern instead.

Always use this pattern for new enumerations in the framework or any package likely to be extended downstream. Use a native PHP enum only when:

When in doubt, use the Shopsys pattern — it's the project default.

The pattern

<?php

declare(strict_types=1);

namespace Shopsys\FrameworkBundle\Component\EntityLog\Enum;

use Shopsys\FrameworkBundle\Component\Enum\AbstractEnum;

class EntityLogActionEnum extends AbstractEnum
{
    public const string CREATE = 'create';
    public const string UPDATE = 'update';
    public const string DELETE = 'delete';
}

Mandatory rules:

Where the enum file goes

How `AbstractEnum` works

Shopsys\FrameworkBundle\Component\Enum\AbstractEnum provides two instance methods used by callers:

$enum->getAllCases();              // returns all public-constant *values*
$enum->validateCase('some_value'); // throws InvalidEnumCaseException if not a valid case

Both methods reflect on the runtime class, so subclasses automatically include their own constants and inherit framework-defined ones. Nothing about getAllCases() is static — that's intentional, because the runtime class can differ between framework and project (see "Extending downstream").

getUnusedConstants() is a hook for subclasses that want to remove a constant inherited from a parent enum (return an array of values to exclude). Default implementation returns [].

Using the enum at call sites

Inject the enum as a constructor dependency (it's a regular service):

public function __construct(
    protected readonly OrderItemTypeEnum $orderItemTypeEnum,
) {
}

public function someMethod(string $type): void
{
    $this->orderItemTypeEnum->validateCase($type);  // throws if invalid

    foreach ($this->orderItemTypeEnum->getAllCases() as $value) {
        // ...
    }
}

Compare values against the constants directly (string comparisons):

if ($descriptor->run === PostDeployTaskRunEnum::ONE_TIME) {
    // ...
}

match ($run) {
    PostDeployTaskRunEnum::NEVER => /* ... */,
    PostDeployTaskRunEnum::ALWAYS => /* ... */,
    PostDeployTaskRunEnum::ONE_TIME => /* ... */,
};

Don't store enum instances anywhere — values are plain strings/ints. The enum service exists only to provide validation and listing.

Extending downstream

A project (or another bundle) extends a framework enum by declaring a subclass and adding constants:

<?php

namespace App\Model\Order\Status;

use Shopsys\FrameworkBundle\Model\Order\Status\OrderStatusTypeEnum as FrameworkOrderStatusTypeEnum;

class OrderStatusTypeEnum extends FrameworkOrderStatusTypeEnum
{
    public const string PARTIALLY_PAID = 'partially_paid';
}

Then add an alias in project-base/app/config/services.yaml so anything injecting the framework type gets the extended one:

Shopsys\FrameworkBundle\Model\Order\Status\OrderStatusTypeEnum:
    alias: App\Model\Order\Status\OrderStatusTypeEnum

To remove a constant inherited from the parent, override getUnusedConstants():

class OrderStatusTypeEnum extends FrameworkOrderStatusTypeEnum
{
    /**
     * @return string[]
     */
    #[Override]
    protected function getUnusedConstants(): array
    {
        return [
            FrameworkOrderStatusTypeEnum::TYPE_LEGACY,
        ];
    }
}

getAllCases() will then exclude those values without removing the constant from the parent class (so existing references to the constant don't break).

Abstract base + concrete subclass

When several enums share behaviour but expose different value sets, define an abstract enum that adds the shared API and concrete subclasses that supply the constants:

abstract class AbstractTransportTypeEnum extends AbstractEnum
{
    /**
     * @return array<string, string>
     */
    abstract public function getAllIndexedByTranslations(): array;
}

class TransportTypeEnum extends AbstractTransportTypeEnum
{
    public const string TYPE_COMMON = 'common';
    public const string TYPE_PACKETERY = 'packetery';

    #[Override]
    public function getAllIndexedByTranslations(): array
    {
        return [
            t('Standard') => self::TYPE_COMMON,
            t('Packetery') => self::TYPE_PACKETERY,
        ];
    }
}

Reference: packages/framework/src/Model/Transport/AbstractTransportTypeEnum.php and packages/framework/src/Model/Transport/TransportTypeEnum.php.

Don'ts

Checklist before shipping

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.