Skip to content

Comments

Create OptionsResolver normalizers#1455

Open
valzargaming wants to merge 5 commits intomasterfrom
normalize-options
Open

Create OptionsResolver normalizers#1455
valzargaming wants to merge 5 commits intomasterfrom
normalize-options

Conversation

@valzargaming
Copy link
Member

This pull request refactors how options are resolved in the Discord class, moving option normalization logic from manual post-processing to the Symfony OptionsResolver's normalizer feature. This change improves code clarity, centralizes validation and normalization, and makes option handling more robust and maintainable.

Refactoring and Centralization of Option Normalization:

  • Moved normalization and default value assignment for logger, loop, intents, dnsConfig, and socket_options from manual code at the end of resolveOptions to dedicated normalizers in the OptionsResolver configuration, ensuring all option logic is handled consistently and in one place. [1] [2] [3]

Validation and Defaults Improvements:

  • Added validation for the intents option to ensure only valid intents are accepted and automatically converted arrays to bitwise integer values using the normalizer.
  • Improved default handling for dnsConfig by loading the system configuration and falling back to Google's DNS if none are found, now handled within the normalizer.
  • Ensured socket_options['happy_eyeballs'] is always set to false to prevent issues with IPv6, now set via the normalizer instead of manual assignment.

Type and Property Documentation Updates:

  • Updated the class property docblock to include all supported options keys, with their types, for better IDE and developer clarity.
  • Updated the allowed types for several options to match their expected values and classes, including switching to the imported DnsConfig class. [1] [2]

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request refactors option normalization in the Discord class by migrating logic from manual post-processing to Symfony OptionsResolver's normalizer feature, improving code organization and maintainability.

Changes:

  • Moved normalization logic for logger, loop, intents, dnsConfig, and socket_options from manual code after resolution to dedicated normalizers within the OptionsResolver configuration
  • Added comprehensive property documentation for the $options array structure including all supported option keys and their types
  • Updated the dnsConfig allowed type to use the imported DnsConfig class instead of the fully qualified class name

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines 1714 to 1750
->setAllowedTypes('loop', LoopInterface::class)
->setNormalizer('loop', fn ($options, $value) => $value ?? Loop::get())
Copy link

Copilot AI Jan 10, 2026

Choose a reason for hiding this comment

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

The 'loop' option needs a default value in the setDefaults() configuration. Currently, 'loop' is defined but has no default, which means if a user doesn't provide it, the option will be undefined and fail the LoopInterface type check before reaching the normalizer. Add 'loop' => null, to the setDefaults array around line 1717, and update setAllowedTypes on line 1749 to include 'null' as an allowed type: ->setAllowedTypes('loop', ['null', LoopInterface::class]).

Copilot uses AI. Check for mistakes.
Comment on lines +1790 to +1800
->setAllowedTypes('dnsConfig', ['string', DnsConfig::class])
->setNormalizer('dnsConfig', function ($options, $value) {
if (null === $value) {
$value = DnsConfig::loadSystemConfigBlocking();
if (! $value->nameservers) {
$value->nameservers[] = '8.8.8.8';
}
}

return $value;
})
Copy link

Copilot AI Jan 10, 2026

Choose a reason for hiding this comment

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

The 'dnsConfig' option needs a default value in the setDefaults() configuration. Currently, 'dnsConfig' is defined but has no default, which means if a user doesn't provide it, the option will be undefined and the normalizer's null check won't be reached. Add 'dnsConfig' => null, to the setDefaults array around line 1717, and update setAllowedTypes on line 1790 to include 'null' as an allowed type: ->setAllowedTypes('dnsConfig', ['null', 'string', DnsConfig::class]).

Copilot uses AI. Check for mistakes.
->setAllowedTypes('token', 'string')
->setAllowedTypes('logger', ['null', LoggerInterface::class])
->setNormalizer('logger', function ($options, $value) {
if (null === $options['logger']) {
Copy link

Copilot AI Jan 10, 2026

Choose a reason for hiding this comment

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

The normalizer is checking $options['logger'] instead of $value. In OptionsResolver normalizers, the first parameter is the full options array and the second parameter is the current value of the option being normalized. This should be if (null === $value) to correctly check if a logger was provided.

Suggested change
if (null === $options['logger']) {
if (null === $value) {

Copilot uses AI. Check for mistakes.
valzargaming and others added 3 commits January 10, 2026 13:21
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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