From a7e32df87547d2ad0682a230d4757cae8378382e Mon Sep 17 00:00:00 2001 From: Kangin Kim Date: Thu, 3 Dec 2026 16:11:13 +0900 Subject: [PATCH 1/5] Separate Composer installation from PHP development service --- README.md | 24 ++++++++++++++++++++++-- docker-compose.yml | 41 ++++++++++++++++++++++++++++++++++++----- 2 files changed, 58 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 06be68d..b5c78f5 100644 --- a/README.md +++ b/README.md @@ -992,19 +992,39 @@ 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. The one-shot `composer` service installs the locked development dependencies into the host project's `vendor/` directory, then exits. The `php` service starts after Composer exits successfully and 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, inspect `docker compose logs composer`; the dependent PHP service will not start successfully. + +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 or changing the lock file, synchronize dependencies explicitly: + +```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 diff --git a/docker-compose.yml b/docker-compose.yml index 241b21e..b0a4dbf 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,17 +1,48 @@ +x-php-image: &php-image + image: php-enumex:local + build: + context: ./docker/php + args: + TIMEZONE: Asia/Tokyo + services: php: + <<: *php-image container_name: php-enumex_php - build: - context: ./docker/php - args: - TIMEZONE: Asia/Tokyo volumes: - .:/var/www/php-enumex working_dir: /var/www/php-enumex - command: ["bash", "-c", "composer install --optimize-autoloader && echo 'Composer installation complete' && exec tail -f /dev/null"] + depends_on: + composer: + condition: service_completed_successfully + command: + - /bin/bash + - -c + - | + exec tail -f /dev/null + networks: + - php-enumex + + composer: + <<: *php-image + 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: + - /bin/bash + - -c + - | + exec composer install --optimize-autoloader --no-interaction networks: - php-enumex +volumes: + composer-cache: + networks: php-enumex: name: php-enumex From 291a52c0e59eb38e91c5278c18576f956fccf8d0 Mon Sep 17 00:00:00 2001 From: Kangin Kim Date: Thu, 3 Dec 2026 16:22:37 +0900 Subject: [PATCH 2/5] Document responsibility-based method order and coding style conventions --- docs/coding-style.md | 77 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 75 insertions(+), 2 deletions(-) diff --git a/docs/coding-style.md b/docs/coding-style.md index 8a5fbcc..7930e2a 100644 --- a/docs/coding-style.md +++ b/docs/coding-style.md @@ -2,12 +2,60 @@ 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. Static Factories +2. Constructor +3. Normal Methods +4. Predicates +5. Magic Methods + +Place static factories that create instances near the beginning of the class (for example, `of()`, `from()`, `parse()`, or `create()`). Classify a method by its actual responsibility, not merely by its name or static modifier. + +Place the constructor after the factories. Although `__construct()` is a PHP magic method, treat it separately as part of instance construction. + +Normal methods include accessors, transformations, and operations. 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 { + + public static function of(string $value): self { + return new self($value); + } + + private function __construct( + private string $value, + ) {} + + public function value(): string { + return $this->value; + } + + public function isEmpty(): bool { + return $this->value === ''; + } + + public function __toString(): string { + return $this->value; + } +} +``` + +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 { @@ -47,6 +95,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 @@ -121,7 +178,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 /** @@ -138,4 +197,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. From 65c52af0ea2d69c0f110f6c8bbd3abfdc51dcc64 Mon Sep 17 00:00:00 2001 From: Kangin Kim Date: Thu, 3 Dec 2026 16:47:28 +0900 Subject: [PATCH 3/5] Build the shared PHP image once and clarify Composer startup guarantees --- README.md | 8 +++++--- docker-compose.yml | 16 +++++++--------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index b5c78f5..ba996e4 100644 --- a/README.md +++ b/README.md @@ -997,9 +997,11 @@ 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. -Both services use the same PHP 8.4 CLI image and mount the project directory. The one-shot `composer` service installs the locked development dependencies into the host project's `vendor/` directory, then exits. The `php` service starts after Composer exits successfully and 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. +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`. -An exited Composer service with exit code 0 is expected. If installation fails, inspect `docker compose logs composer`; the dependent PHP service will not start successfully. +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: @@ -1010,7 +1012,7 @@ php -d zend.assertions=1 examples/NativeExtension/Presentation/OrderStatus.php vendor/bin/phpunit ``` -After switching branches or changing the lock file, synchronize dependencies explicitly: +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 diff --git a/docker-compose.yml b/docker-compose.yml index b0a4dbf..cffbdb2 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,13 +1,7 @@ -x-php-image: &php-image - image: php-enumex:local - build: - context: ./docker/php - args: - TIMEZONE: Asia/Tokyo - services: php: - <<: *php-image + image: php-enumex:local + pull_policy: never container_name: php-enumex_php volumes: - .:/var/www/php-enumex @@ -24,7 +18,11 @@ services: - php-enumex composer: - <<: *php-image + image: php-enumex:local + build: + context: ./docker/php + args: + TIMEZONE: Asia/Tokyo container_name: php-enumex_composer environment: COMPOSER_CACHE_DIR: /root/.cache/composer From 4ae7a4559cc12a0080002516a1b3dafca5e6dfcb Mon Sep 17 00:00:00 2001 From: Kangin Kim Date: Thu, 3 Dec 2026 16:49:47 +0900 Subject: [PATCH 4/5] Place constructors before static factories in coding style --- docs/coding-style.md | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/docs/coding-style.md b/docs/coding-style.md index 7930e2a..535d576 100644 --- a/docs/coding-style.md +++ b/docs/coding-style.md @@ -6,15 +6,15 @@ EnumEx uses a consistent PHP style that makes declarations and conceptual groups Organize methods by responsibility rather than alphabetically or solely by visibility. The default order is: -1. Static Factories -2. Constructor +1. Constructor +2. Static Factories 3. Normal Methods 4. Predicates 5. Magic Methods -Place static factories that create instances near the beginning of the class (for example, `of()`, `from()`, `parse()`, or `create()`). Classify a method by its actual responsibility, not merely by its name or static modifier. +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 the constructor after the factories. Although `__construct()` is a PHP magic method, treat it separately as part of instance construction. +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. Normal methods include accessors, transformations, and operations. Arrange related accessors according to the structure of the represented concept, rather than alphabetically. @@ -25,14 +25,14 @@ Place other magic methods, such as `__toString()`, `__debugInfo()`, `__serialize ```php final readonly class CaseLabel implements Stringable { - public static function of(string $value): self { - return new self($value); - } - private function __construct( private string $value, ) {} + public static function of(string $value): self { + return new self($value); + } + public function value(): string { return $this->value; } From 4ee1bad2bf7e71c4504dae6cb49510ce95288c99 Mon Sep 17 00:00:00 2001 From: Kangin Kim Date: Thu, 3 Dec 2026 16:53:07 +0900 Subject: [PATCH 5/5] Refine normal method ordering by conceptual responsibility --- docs/coding-style.md | 43 ++++++++++++++++++++++++++++++++++--------- 1 file changed, 34 insertions(+), 9 deletions(-) diff --git a/docs/coding-style.md b/docs/coding-style.md index 535d576..4f4d6b7 100644 --- a/docs/coding-style.md +++ b/docs/coding-style.md @@ -9,6 +9,10 @@ Organize methods by responsibility rather than alphabetically or solely by visib 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 @@ -16,7 +20,16 @@ Place the constructor first among methods. With constructor property promotion, 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. -Normal methods include accessors, transformations, and operations. Arrange related accessors according to the structure of the represented concept, rather than alphabetically. +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. @@ -26,23 +39,35 @@ Place other magic methods, such as `__toString()`, `__debugInfo()`, `__serialize final readonly class CaseLabel implements Stringable { private function __construct( - private string $value, + private UnitEnum $case, ) {} - public static function of(string $value): self { - return new self($value); + 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 value(): string { - return $this->value; + public function withCase(UnitEnum $case): self { + return new self($case); } - public function isEmpty(): bool { - return $this->value === ''; + public function is(UnitEnum $other): bool { + return $this->case === $other; } public function __toString(): string { - return $this->value; + return $this->name(); } } ```