Skip to content

Allow SortDirection enum in ObjectRepository::findBy() - #524

Closed
GromNaN wants to merge 1 commit into
doctrine:4.3.xfrom
GromNaN:sortdirection-findby-4.3.x
Closed

Allow SortDirection enum in ObjectRepository::findBy()#524
GromNaN wants to merge 1 commit into
doctrine:4.3.xfrom
GromNaN:sortdirection-findby-4.3.x

Conversation

@GromNaN

@GromNaN GromNaN commented Sep 11, 2026

Copy link
Copy Markdown
Member

Follows #523. Companion PR on 5.0.x: #525.

ObjectRepository::findBy() $orderBy now accepts the PHP 8.6 SortDirection enum in addition to the 'asc', 'desc', 'ASC' and 'DESC' string literals. The native signature is unchanged and strings remain accepted, so this is backward compatible for callers.

Implementations that do not already handle the enum must accept it and map it to their underlying sort direction, otherwise a caller passing SortDirection will trigger a runtime error.

Downstream support:

  • doctrine/orm supports SortDirection in findBy() and findOneBy() starting with 3.7.0. Older versions (3.6.x and below, including the 2.20.x/2.21.x LTS lines) only accept strings and will not receive this change; upgrade to 3.7.0 or later to use the enum.
  • doctrine/mongodb-odm supports SortDirection in findBy() starting with 2.18. Older versions (2.17.x and below) only accept strings and will not receive this change; upgrade to 2.18 or later. Its query builder sort() already accepts the enum since 2.17.0, but findBy() bypasses the query builder.

symfony/polyfill-php86 is added to require-dev so PHPStan can resolve SortDirection on PHP 8.1-8.5 without a production dependency. The PHPDoc is validated by the static-analysis CI; the runtime test is a smoke test guarding against a future narrowing of the native type.

Update the PHPDoc contract of findBy() to accept the PHP 8.6 SortDirection
enum in addition to the 'asc', 'desc', 'ASC' and 'DESC' string literals, as
requested in doctrine#523. The native signature is unchanged and strings remain
accepted, so this is backward compatible for callers.

Implementations that do not already handle the enum must accept it and map it
to their underlying sort direction, otherwise a caller passing SortDirection
will trigger a runtime error. doctrine/orm 3.7.0+ and doctrine/mongodb-odm
2.18+ already handle it.

Add symfony/polyfill-php86 to require-dev so PHPStan can resolve the
SortDirection symbol on PHP 8.1-8.5 without a production dependency.

Refs doctrine#523
Comment thread src/ObjectRepository.php
* @param array<string, string>|null $orderBy
* @phpstan-param array<string, 'asc'|'desc'|'ASC'|'DESC'>|null $orderBy
* @param array<string, mixed> $criteria
* @param array<string, string|SortDirection>|null $orderBy

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Widening parameter type is a BC break for implementers (as implementers are not allowed to use a more restrictive type based on the Liskov Substitution Principle). So to me, this change must happen only in the 5.0.x branch.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're absolutely right. And even if it doesn't break the PHP code because the type isn't enforced by PHP, we're providing information about a supported type that isn't respected by all older versions of ORM/ODM. Adding a composer conflict rule with older ORM versions would be really dirty.

The solution, therefore, is to accept string|SortDirection in 5.0 and (possibly) remove support for string in 6.0.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding a composer conflict rule with older ORM versions would be really dirty.

Indeed. An interface package that would be forced to add conflicts for older versions of implementation packages would be a clear signal that the interface package does not follow semver (assuming implementation packages don't use unbounded constraints of course but rely on semver constraints)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this maybe be achieved with @method?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@greg0ire no. We don't want to add a new method. We want to change the signature of an existing method.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, too bad there isn't an annotation to suggest this.

@GromNaN GromNaN closed this Sep 11, 2026
@GromNaN
GromNaN deleted the sortdirection-findby-4.3.x branch September 11, 2026 14:11
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.

3 participants