Skip to content

Unify mapping and qualified formatting APIs - #18

Merged
gachakra merged 3 commits into
mainfrom
feature/mapping-and-qualified-formatting
Dec 3, 2026
Merged

gachakra merged 3 commits into
mainfrom
feature/mapping-and-qualified-formatting

Conversation

@gachakra

@gachakra gachakra commented Dec 3, 2026

Copy link
Copy Markdown
Owner

Summary

  • Introduce explicit name/value-to-case and scalar maps; distinguish original native cases from factory-created wrapper elements.
  • Rename native formatting methods to qualifiedName(), qualifiedInfo(), qualifiedNames() and listQualifiedInfo() while preserving the formatting contract.
  • Preserve the historical Class-based Enum APIs and their internal implementations. Add aliases and thin projections; nameToValue() delegates to constants().
  • Update runtime tests, PHPStan fixtures, runnable examples, README migration guidance and ADR 0011.

Compatibility

Native method renames deliberately break the v2 API. New final methods may collide with downstream subclass methods, including on the class-based implementation. This work targets the next major development cycle; no release or tag is created. Review the distinction between native cases and wrapper elements and the historical class-based aliases.

Verification

Runtime and static-analysis coverage has been added. Hosted CI and independent review are pending. Local full-suite execution is unavailable in this environment because the repository cannot be cloned and Composer, PHPUnit and PHPStan are not installed.

gachakra commented Dec 3, 2026

Copy link
Copy Markdown
Owner Author

Independent review of 103b6682145ad51d3b145dbef9f41af4c0928c69

Recommendation — hold merge until the failing CI check is addressed and the Class-based shortcut compatibility impact is explicitly accepted. The native naming and mapping contracts held in the independent runtime tests. The native v2 name removals are within the stated major-release scope.

Findings

  1. [P1, CI] Treat the empty-enum valueToName() report as a narrowly scoped PHPStan false positive. Both hosted jobs fail with return.type at the arrow function. PHPStan 2.3.7 specializes the callback for the concrete trait consumer. Its inferred map for the empty enum is array<int|string, never>, the callback parameter becomes never, and its property access yields an error type that the return rule reports as mixed. PHP calls the callback zero times and returns []. I confirmed this for empty pure and backed enums. In disposable copies on both PHP versions, allowing the exact diagnostic in the existing report checker for the EmptyColour consumer, valueToName() method, identifier and message passed the selection check with the implementation intact. A local @var UnitEnum $case in a block callback also passed. I prefer the report-checker approach to preserve the direct map projection and empty fixture. Removing the callback return type only moved the error to the enclosing method, and an explicit empty guard produced deadCode.unreachable. src/EnumTraits/EnumCollections.php:185–200 · docs/static-analysis/check-selection-report.php:10–48 · Hosted CI

  2. [P2, Class-based compatibility] New aliases shadow historical constant shortcut calls. The historical method bodies and internal calls are unchanged. The dispatch of Example::CONSTANT() still depends on which method names exist, because PHP resolves declared methods case-insensitively before __callStatic(). With constants NAMETOVALUE = 1 and QUALIFIEDNAME = 2, both shortcut calls returned cached elements at the base. On this head, the first returns the entire map and the second raises an error because the new method is non-static. I reproduced both on PHP 8.4 and 8.5. Example::of('NAMETOVALUE') and Example::of('QUALIFIEDNAME') still retrieve the original elements. The README covers existing constant-method collisions and subclass method declaration conflicts. Please explicitly cover the newly reserved alias names in the Class-based migration contract and confirm that this additional breaking behavior is acceptable. If these existing shortcuts must remain compatible, the affected names need a different API placement. src/ClassBased/Enum.php:90–110 · src/ClassBased/Enum.php:195–203 · src/ClassBased/Enum.php:368–376 · README-ClassBased.md:371–377

  3. [P3, static-analysis regression] The new Class-based qualifiedInfo() alias adds a separate attribute.notFound diagnostic. Maximum-level analysis of all sources reports 17 diagnostics at this head versus 16 at the base. The only added diagnostic after accounting for shifted lines is the alias's #[Pure] attribute, whose JetBrains class is absent in the locked environment. The existing annotation on debugOutput() accounts for a baseline diagnostic. Consider omitting the new attribute on the forwarding alias while retaining the historical method and annotation. This is separate from the hosted empty-enum failure. src/ClassBased/Enum.php:205–214

Contracts and verification

  • The six collection maps consistently distinguish declaration names, original logical values, native singleton cases, and factory-created wrapper elements. The native trait exposes the four maps that need no factory. Independent PHPStan fixtures inferred concrete native map values, concrete custom wrapper values, and Class-based subtype values. Qualified formatting returned strings and ordered lists, honored explicit backed delimiters and value export, and left pure cases name-only without invoking wrapper factories.
  • Runtime probes covered declaration order, integer-like and padded strings, negative and empty keys, strict lookups, pure and backed empty enums, and duplicate keys. Value-keyed maps reused checked keys and rejected collisions before factory invocation. Only element maps invoked the native wrapper factory, once per case per call. Class-based aliases retained cached identity, root errors, collisions and historical float, boolean and null behavior.
  • Independently ran on PHP 8.4.28 and 8.5.13. Composer validation, lockfile and platform checks, strict PSR-4, lint of 64 files, 367 PHPUnit tests and 1,568 assertions per version, all 25 assertion-enabled examples, Composite regression probes, and 33 main README code blocks in 15 fixtures passed. Git and Composer archives matched the 23-file allowlist. README anchors and pinned links resolved. The focused collection and Composite PHPStan checks passed, while the committed selection check failed as reported above. The local install reused the lockfile-matched vendor tree. Hosted dependency installation passed before the static-analysis failure stopped later steps.
  • Reviewed the diff against the coding guide, confirmed git diff --check, and found no credential, personal-data, local-path or private-network markers in the added lines. Alternate analyzers and downstream prevalence of alias-name collisions remain unverified.

gachakra commented Dec 3, 2026

Copy link
Copy Markdown
Owner Author

Independent re-review of 3a71965fbf869a67296b5582d084e61c390aafd9

Recommendation — ready to merge for the next major release. The two static-analysis findings are resolved, and the maintainers' decision on the Class-based shortcut incompatibility is documented and covered by regression tests. I found no new actionable implementation issues.

Previous findings

  • P1, empty-enum PHPStan failure — resolved. The checker recognizes the known PHPStan 2.3.7 diagnostic only for the exact trait and EmptyColour consumer, within the reflected valueToName() method, with the expected identifier and message. I independently changed each of the path, consumer, method range, identifier and message in separate test reports. All five variants still failed the checker on both PHP versions. The empty fixture and direct native map projection remain intact, and empty pure and backed enums still return []. docs/static-analysis/check-selection-report.php:22–58
  • P2, Class-based constant shortcuts — accepted breaking behavior with a migration path. The migration guide lists the nine newly reserved alias names and explains the two dispatch outcomes. The tests cover a static alias returning the map, a non-static alias raising an error, and explicit of() and fromValue() calls returning the same cached element. I independently reproduced the base and head behaviors on both PHP versions. The historical methods and internal calls remain in place. README-ClassBased.md:373–390 · ADR 0011:43–47 · tests/ClassBased/BasicMethods/EnumLookupTest.php:89–113
  • P3, new attribute.notFound diagnostic — resolved. The forwarding alias removes its Pure attribute and the original debugOutput() retains its annotation. Full-source PHPStan reports the same 16 baseline diagnostics on both PHP versions after accounting for shifted lines. src/ClassBased/Enum.php:185–213

Verification and remaining scope

  • The hosted PHP 8.4 and 8.5 jobs passed. Independently, on PHP 8.4.28 and 8.5.13, Composer validation and strict PSR-4, lint of 64 PHP files, 369 PHPUnit tests and 1,577 assertions per version, all 25 assertion-enabled examples, independent mapping and formatting probes, and 33 main README code blocks in 15 fixtures passed. Targeted PHPStan type inference passed, and Git and Composer archives matched the 23-file allowlist. Local Composer checks reused the lockfile-matched vendor tree. Hosted CI
  • git diff --check passed. The added diff showed no credential, personal-data, local-path or private-network markers. Downstream collision prevalence and alternate analyzers remain unverified. The Class-based migration note applies to the next major release, and the narrowly scoped PHPStan allowance should be revisited when upgrading the analyzer.

@gachakra
gachakra marked this pull request as ready for review December 3, 2026 07:58
@gachakra
gachakra merged commit 96132b1 into main Dec 3, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant