Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -992,19 +992,41 @@ From a Git checkout (not the installed Composer package):

```bash
docker compose up -d --build --remove-orphans
docker compose logs -f php
docker compose ps -a
```

Docker is a CLI-only development environment; no HTTP service or host port is published. The `--remove-orphans` option also removes the obsolete Nginx service if you used an older checkout.

Wait until the PHP service logs show `Composer installation complete`. On the first run, dependencies may still be installing after the container starts. Press Ctrl+C to stop following the logs (the PHP container remains running), then enter the PHP container and run the example:
Both services use the same PHP 8.4 CLI image and mount the project directory. Only the `composer` service builds the shared local image; the `php` service uses that image without pulling it. To build the image explicitly, run `docker compose build composer`.

The one-shot `composer` service installs the locked development dependencies into the host project's `vendor/` directory, then exits. On a fresh startup with `docker compose up`, the `php` service waits for Composer to exit successfully and then remains available for development. The host IDE and both services see the same installed dependencies. The named `composer-cache` volume retains downloaded package data independently of the installed dependencies.

An exited Composer service with exit code 0 is expected. If installation fails during a fresh startup, the dependent PHP service does not start; inspect `docker compose logs composer`. This startup condition is not a continuous dependency health check: an already-running PHP container can remain available after a failed installation, and `docker compose restart` does not guarantee successful dependency synchronization. Use the explicit Composer command below to synchronize dependencies while PHP is running, and check its exit status before running tests.

Enter the PHP container to run examples or tests:

```bash
docker compose exec php bash

php -d zend.assertions=1 examples/NativeExtension/Presentation/OrderStatus.php
vendor/bin/phpunit
```

After switching branches, changing the lock file, or recovering from a failed installation, synchronize dependencies explicitly (also when PHP is already running):

```bash
docker compose run --rm composer
```

To deliberately update dependency versions and the lock file:

```bash
docker compose run --rm composer composer update
```

The service command can also be overridden with other Composer commands. Avoid running tests or another Composer installation while the shared `vendor/` directory is being modified. The current development image runs as root; on native Linux, files created in the bind mount may be owned by root.

Use `docker compose down` to stop and remove the development containers. The host `vendor/` directory and the named download cache persist. Removing volumes with `docker compose down -v` also removes the download cache.

## Class-based Enums

Expand Down
31 changes: 30 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,17 +1,46 @@
services:
php:
image: php-enumex:local
pull_policy: never
container_name: php-enumex_php
volumes:
- .:/var/www/php-enumex
working_dir: /var/www/php-enumex
depends_on:
composer:
condition: service_completed_successfully
command:
- /bin/bash
- -c
- |
exec tail -f /dev/null
networks:
- php-enumex

composer:
image: php-enumex:local
build:
context: ./docker/php
args:
TIMEZONE: Asia/Tokyo
container_name: php-enumex_composer
environment:
COMPOSER_CACHE_DIR: /root/.cache/composer
volumes:
- .:/var/www/php-enumex
- composer-cache:/root/.cache/composer
working_dir: /var/www/php-enumex
command: ["bash", "-c", "composer install --optimize-autoloader && echo 'Composer installation complete' && exec tail -f /dev/null"]
command:
- /bin/bash
- -c
- |
exec composer install --optimize-autoloader --no-interaction
networks:
- php-enumex

volumes:
composer-cache:

networks:
php-enumex:
name: php-enumex
102 changes: 100 additions & 2 deletions docs/coding-style.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,85 @@

EnumEx uses a consistent PHP style that makes declarations and conceptual groups easy to scan. These conventions complement the architecture and type contracts; formatting must not change behavior. Apply them to source code, tests, executable examples, and current documentation snippets. Preserve historical documentation and intentionally recorded historical examples.

## Method Organization

Organize methods by responsibility rather than alphabetically or solely by visibility. The default order is:

1. Constructor
2. Static Factories
3. Normal Methods
1. Object Access
2. Value Access
3. Derived Information
4. Transformations / Operations
4. Predicates
5. Magic Methods

Place the constructor first among methods. With constructor property promotion, it describes the object's stored state and initialization contract as well as its initialization logic. Reading this structure first helps explain the factories that follow. Although `__construct()` is a PHP magic method, treat it separately from other magic methods. This rule does not require placing the constructor before constants, properties, or trait declarations.

Place static factories that create instances after the constructor (for example, `of()`, `from()`, `parse()`, or `create()`). If no constructor is declared, place them at the beginning of the method group. Classify a method by its actual responsibility, not merely by its name or static modifier.

Within Normal Methods, prefer the following conceptual progression:

- **Object Access:** access the represented object or its constituent objects, such as `unwrap()` and `at()`.
- **Value Access:** access values and basic characteristics, such as `name()`, `value()`, and `count()`.
- **Derived Information:** obtain derived descriptions or representations, such as `qualifiedName()` and `qualifiedInfo()`.
- **Transformations / Operations:** perform operations on the represented concept, such as `filter()`, `only()`, and `iterate()`.

Object access generally precedes scalar value access, but the return type alone does not determine placement. For example, returning a Generator or Closure does not make an operation an object accessor. Derived information precedes predicates: obtaining information and evaluating the truth of a proposition are distinct responsibilities.

Treat this progression as a default, not a mechanical sort order. Preserve cohesive API families, such as mapping operations grouped by their source concept, and arrange related accessors according to the structure of the represented concept rather than alphabetically.

Group predicates such as `isEmpty()`, `isNotEmpty()`, `has()`, `hasAll()`, and `equals()` after normal methods. Classify them by their responsibility for logical decisions, not simply by a bool return type.

Place other magic methods, such as `__toString()`, `__debugInfo()`, `__serialize()`, and `__unserialize()`, near the end of the class.

```php
final readonly class CaseLabel implements Stringable {

private function __construct(
private UnitEnum $case,
) {}

public static function of(UnitEnum $case): self {
return new self($case);
}

public function unwrap(): UnitEnum {
return $this->case;
}

public function name(): string {
return $this->case->name;
}

public function qualifiedName(): string {
return $this->case::class . '::' . $this->case->name;
}

public function withCase(UnitEnum $case): self {
return new self($case);
}

public function is(UnitEnum $other): bool {
return $this->case === $other;
}

public function __toString(): string {
return $this->name();
}
}
```

The example illustrates method placement only; public API documentation is omitted for brevity. The ordering is a default for new and revised code, not a requirement to rearrange unrelated existing methods. The placement of trait declarations, constants, properties, non-factory static methods, protected methods, private helpers, abstract methods, interface implementations, and enum cases is not yet prescribed by a complete ordering rule. Preserve meaningful existing organization for these members. Do not let a formatter determine their semantic placement.

## Basic Layout

- Use four spaces for indentation, not tabs.
- Place opening braces on the same line as class, enum, trait, method, and control-structure declarations, following existing code.
- Preserve the established blank line immediately inside a class when appropriate. Do not add an unnecessary blank line immediately after an enum's opening brace.
- Use trailing commas in multiline parameter and argument lists where supported and appropriate.
- Keep empty method bodies compact where appropriate, including empty constructors (`) {}`).

```php
final readonly class OrderStatusPolicy extends EnumComposite {
Expand Down Expand Up @@ -47,6 +120,15 @@ private int $count;
private string $name;
```

Align consecutive related local assignments where doing so helps the reader see their relationship.

```php
$name = $case->name();
$value = $case->value();
```

Do not force alignment on every expression or across unrelated declarations.

Pure enum cases do not need artificial padding.

```php
Expand Down Expand Up @@ -121,7 +203,9 @@ Preserve intentional spacing in existing code. Avoid unrelated formatting churn

## Types and PHPDoc

Use precise native types, readonly where appropriate, and PHPDoc generics that express the actual type contract. Document important behavior, exceptions, and return shapes. For public operations, provide concrete examples in PHPDoc where practical, following the existing EnumCase and EnumCollection documentation style.
Use precise native types and PHPDoc generics that express the actual type contract. Prefer modern PHP features where appropriate, including strict types, readonly, final, constructor property promotion, first-class callables, match expressions, and named arguments. These are design choices, not unconditional automatic transformations.

Write current PHPDoc in English. Document important behavior, exceptions, and return shapes. As a general rule, public methods should include an `Example:` section with a concrete call and an inline comment showing the result, following the existing EnumCase and EnumCollection documentation style. Show meaningful boundary values, null, empty strings, or exceptions where relevant. Use multiline examples when needed. For factories, explain the meaning of the resulting instance; for void operations, describe the resulting effect or state. Private helpers do not need examples by default.

```php
/**
Expand All @@ -138,4 +222,18 @@ Keep example results consistent with the implementation and executable examples.

## Formatting Tools

These conventions are project-specific; they do not claim full PSR-12 compliance. Do not introduce or run blanket automated formatting that removes intentional alignment or semantic whitespace. If automated rules are introduced later, configure them to preserve these conventions and review their resulting diff.
These conventions are project-specific. PSR-12 and PER Coding Style may inform the rules, but full compliance is not a goal. Prefer conventions that communicate structure and meaning to humans and AI-assisted development.

Keep agreed style conventions and any future shared tooling configuration in the repository, rather than relying solely on local IDE settings. Tool selection, configuration, custom rules, Composer scripts, and CI integration remain separate decisions; no formatter is mandated by this document.

Distinguish three responsibilities:

- **Formatting:** safe changes to indentation, braces, spacing, and alignment.
- **Style validation:** report violations without automatically changing code when a safe correction cannot be established.
- **Design review:** evaluate semantic method placement, responsibilities, naming, types, and the accuracy of PHPDoc examples.

A mechanical check for an `Example:` section is not a verification of the example's meaning or correctness. Method ordering by responsibility may also require human or AI review.

Before introducing tooling, verify support for the project's PHP versions (8.4 and 8.5), intentional alignment, semantic whitespace, compact empty constructors, and PHPDoc examples. Do not introduce unnecessary overlapping tools. Any future CI integration should check rather than automatically rewrite and push source files.

Do not run blanket automated formatting that removes meaningful organization or changes behavior. Exclude preserved historical material from formatting and style validation; respect existing historical documentation. Keep broad style-only changes separate from functional changes, and review configuration and resulting diffs before adoption.