-
-
Notifications
You must be signed in to change notification settings - Fork 56
feat!: consts for types
#486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ThomasLandauer
wants to merge
2
commits into
martin-georgiev:main
Choose a base branch
from
ThomasLandauer:constants-for-types
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+13
−1
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace MartinGeorgiev\Doctrine\DBAL; | ||
|
|
||
| enum Type | ||
| { | ||
| public const BIGINT_ARRAY = 'bigint[]'; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: martin-georgiev/postgresql-for-doctrine
Length of output: 222
🌐 Web query:
PHP 8.1 enum syntax public const vs case backing values💡 Result:
Short answer: they're different things.
case (backed or pure) defines an enum member (an instance). Backed cases are declared with a scalar type and value (enum E: string { case A = 'a'; }) and each instance has a read‑only ->value plus static from/tryFrom helpers. Backed enums require the enum type (string|int) and every case must have a unique scalar. [1][2]
public const inside an enum is just a class constant (a scalar or a reference). It is not an enum case and does not satisfy an enum typehint. A const can reference a case (aliasing it) — e.g. public const X = self::SomeCase; — but that const holds the case reference or scalar you assign, not a case instance value unless you explicitly use the case->value (PHP 8.2 added easier use of case values in consts). [3][1]
Examples:
Backed case:
enum Status: string { case Active = 'active'; }
$v = Status::Active->value; // 'active' — Status::from('active') returns Status::Active. [2]
Enum const alias / scalar const:
enum Status { case Active; public const LIVE = self::Active; public const TAG = 'live'; }
Status::Active is an enum instance; Status::TAG is a string and does NOT satisfy a Status typehint. [1][3]
If you want to map external scalars (DB values, API keys) to enum instances, use a backed enum (case = 'value') so you can safely convert with from/tryFrom. [2][3]
Sources: PHP manual (enum constants + backed enums) and related docs. [1][2][3]
References
[1] PHP manual — Enumeration constants.
[2] PHP manual — Backed enumerations.
[3] Shopware / community notes on using enums and consts (shows const vs case behavior and PHP 8.2 improvement).
🌐 Web query:
PHP enum public const vs backed enum case DBAL doctrine type constants💡 Result:
Short answer
Why (technical)
Doctrine/DBAL implications and common patterns
References
🌐 Web query:
PHP enum with only public constants without cases valid syntax💡 Result:
Yes — PHP enums may have zero cases and may contain constants, so an enum that only defines public constants (no case statements) is valid syntax. Example:
References: PHP manual — Basic enumerations (zero-case enums valid) [1]; Enumeration constants (enum may include public constants) [2].
Based on my verification, the original review comment contains a critical factual error. The code is syntactically valid PHP—enums can contain zero cases and public constants. However, the architectural concerns are valid. Here's the rewritten review comment:
This enum design pattern is not recommended for DBAL type mapping, even though it's syntactically valid.
An enum with only public constants (no cases) is valid PHP 8.1+ syntax, but it's an unusual pattern that defeats the purpose of using an enum. For DBAL type constants, choose one of these approaches:
Option 1: Backed enum with cases (type-safe, matches Doctrine conventions)
Trade-off: Callers use
Type::BIGINT_ARRAY->valuein DBAL type annotations.Option 2: Abstract class with constants (simpler, original approach)
Trade-off: No enum type safety, but constants are used directly without
->value.Align with the maintainer's preference on which trade-off is acceptable. For DBAL custom types, backed enums (Option 1) are more idiomatic and provide type safety; abstract classes (Option 2) are simpler if enum semantics aren't needed.
🤖 Prompt for AI Agents