codebase.watch what is new in your dependencies

Options::remove($option)

At a glance

Type
New feature
remove() added
2.1

Since symfony/options-resolver 2.1, Options::remove() removes an option.

Remove an option

Option removed

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('firstName', 'foo');
$options->set('key', 'bar');
$options->remove('key');

// array('firstName' => 'foo')
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('firstName', 'foo');
$options->set('key', 'bar');
$options->remove('key');

// ['firstName' => 'foo']
$all = $options->all();

Array syntax

Options implements ArrayAccess: unsetting an offset calls remove().

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');
// Calls $options->remove('key')
unset($options['key']);

// array()
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');
// Calls $options->remove('key')
unset($options['key']);

// []
$all = $options->all();

Lazy option removed

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set(
    'key',
    // Never called: the option is removed before being read
    function (Options $options) {
        throw new LogicException('foo');
    }
);
$options->remove('key');

// array()
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set(
    'key',
    // Never called: the option is removed before being read
    function (Options $options) {
        throw new LogicException('foo');
    }
);
$options->remove('key');

// []
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set(
    'key',
    // Never called: the option is removed before being read
    fn (Options $options): never => throw new LogicException('foo')
);
$options->remove('key');

// []
$all = $options->all();

Option missing

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
// Does nothing: the option does not exist
$options->remove('key');

// array()
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
// Does nothing: the option does not exist
$options->remove('key');

// []
$all = $options->all();

Set again

An option set again after its removal comes after the other options.

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('firstName', 'foo');
$options->set('key', 'bar');
$options->remove('firstName');
$options->set('firstName', 'baz');

// array('key' => 'bar', 'firstName' => 'baz')
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('firstName', 'foo');
$options->set('key', 'bar');
$options->remove('firstName');
$options->set('firstName', 'baz');

// ['key' => 'bar', 'firstName' => 'baz']
$all = $options->all();

Option read by a lazy option

The lazy option is only evaluated when it is read: the option it reads no longer exists.

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('firstName', 'foo');
$options->set(
    'key',
    function (Options $options) {
        return strtoupper($options->get('firstName'));
    }
);
$options->remove('firstName');

// OutOfBoundsException: The option "firstName" does not exist.
$options->get('key');
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('firstName', 'foo');
$options->set(
    'key',
    fn (Options $options): string => strtoupper($options->get('firstName'))
);
$options->remove('firstName');

// OutOfBoundsException: The option "firstName" does not exist.
$options->get('key');

Its normalizer

Normalizer removed

remove() also removes the normalizer of the option: the value set again is not normalized.

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('email', 'foo@bar.com');
$options->setNormalizer(
    'email',
    function (Options $options, $value) {
        return strtoupper($value);
    }
);
$options->remove('email');
$options->set('email', 'foo@bar.com');

// array('email' => 'foo@bar.com')
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('email', 'foo@bar.com');
$options->setNormalizer(
    'email',
    function (Options $options, $value) {
        return strtoupper($value);
    }
);
$options->remove('email');
$options->set('email', 'foo@bar.com');

// ['email' => 'foo@bar.com']
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('email', 'foo@bar.com');
$options->setNormalizer(
    'email',
    fn (Options $options, string $value): string => strtoupper($value)
);
$options->remove('email');
$options->set('email', 'foo@bar.com');

// ['email' => 'foo@bar.com']
$all = $options->all();

Normalizer without value removed

Without remove(), all() would call the normalizer and create the option.

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->setNormalizer(
    'email',
    function (Options $options, $value) {
        return $value;
    }
);
$options->remove('email');

// array()
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->setNormalizer(
    'email',
    function (Options $options, $value) {
        return $value;
    }
);
$options->remove('email');

// []
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->setNormalizer(
    'email',
    fn (Options $options, $value) => $value
);
$options->remove('email');

// []
$all = $options->all();
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->setNormalizer(
    'email',
    fn (Options $options, mixed $value): mixed => $value
);
$options->remove('email');

// []
$all = $options->all();

Read-only once read

Once an option is read, the options cannot be removed anymore.

Removed after a read

use Symfony\Component\OptionsResolver\Exception\OptionDefinitionException;
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('key', 'foo');
$options->get('key');

// OptionDefinitionException: Options cannot be removed anymore once options have been read.
$options->remove('key');

The sources