codebase.watch what is new in your dependencies

Options implements Countable

At a glance

Type
New feature
Countable added
2.1

Since symfony/options-resolver 2.1, Options implements Countable.

Count the options

No option

use Symfony\Component\OptionsResolver\Options;

$options = new Options();

// 0
$count = count($options);

Options counted

use Symfony\Component\OptionsResolver\Options;

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

// 2
$count = count($options);

Method call

count($options) calls the count() method.

use Symfony\Component\OptionsResolver\Options;

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

// 2
$count = $options->count();

Null value

use Symfony\Component\OptionsResolver\Options;

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

// 1
$count = count($options);

Value replaced

use Symfony\Component\OptionsResolver\Options;

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

// 1
$count = count($options);

Lazy value not evaluated

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set(
    'key',
    // Not called by count()
    function (Options $options) {
        throw new LogicException('foo');
    }
);

// 1
$count = count($options);
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set(
    'key',
    // Not called by count()
    fn (Options $options): never => throw new LogicException('foo')
);

// 1
$count = count($options);

Option removed

use Symfony\Component\OptionsResolver\Options;

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

// 1
$count = count($options);

Options cleared

use Symfony\Component\OptionsResolver\Options;

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

// 0
$count = count($options);

Normalizer without value

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
// A normalizer does not create the option
$options->setNormalizer(
    'email',
    function (Options $options, $value) {
        return strtoupper($value);
    }
);

// 0
$count = count($options);
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
// A normalizer does not create the option
$options->setNormalizer(
    'email',
    fn (Options $options, string $value): string => strtoupper($value)
);

// 0
$count = count($options);

Recursive count

COUNT_RECURSIVE is ignored for a Countable object: the values of the options are not counted, unlike with an array.

use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('roles', array('foo', 'bar'));

// 1
$count = count($options, COUNT_RECURSIVE);
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->set('roles', ['foo', 'bar']);

// 1
$count = count($options, COUNT_RECURSIVE);

Normalizer without value counted after all()

all() calls the normalizer of an option that does not exist: its result creates the option, which is then counted.

  • PHP 5.3 – 7.4An E_NOTICE, Undefined index: email, then the option is created and counted.
  • PHP >= 8.0An E_WARNING, Undefined array key "email", then the option is created and counted.
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->setNormalizer(
    'email',
    function (Options $options, $value) {
        return $value;
    }
);
// E_NOTICE: Undefined index: email
$options->all();

// 1
$count = count($options);
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->setNormalizer(
    'email',
    fn (Options $options, $value) => $value
);
// E_NOTICE: Undefined index: email
$options->all();

// 1
$count = count($options);
use Symfony\Component\OptionsResolver\Options;

$options = new Options();
$options->setNormalizer(
    'email',
    fn (Options $options, mixed $value): mixed => $value
);
// E_WARNING: Undefined array key "email"
$options->all();

// 1
$count = count($options);

Not a read

Set after count()

count() does not read the options: they can still be set.

use Symfony\Component\OptionsResolver\Options;

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

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

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

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

The sources