Repository: symfony/symfony
File: src/Symfony/Component/Intl/Currencies.php (PHP)

Code before:
    /**
     * @param non-empty-string                                      $country          e.g. 'FR'
     * @param non-empty-string                                      $currency         e.g. 'USD'
     * @param array{from?: non-empty-string, to?: non-empty-string} $currencyMetadata
     * @param \DateTimeInterface                                    $date             The date on which the check will be performed
     */
    private static function isActive(
        string $country,
        string $currency,
        array $currencyMetadata,
        \DateTimeInterface $date = new \DateTimeImmutable('today', new \DateTimeZone('Etc/UTC')),
    ): bool {
        if (!\array_key_exists('from', $currencyMetadata)) {
            // Note: currencies that are not legal tender don't have often validity dates.
            throw new \RuntimeException("Cannot check whether the currency $currency is active or not in $country because they are no validity dates available.");
        }

        /**
         * @var \DateTimeImmutable $from The date format is always valid as it is generated using the CurrencyDataGenerator
         */
        $from = \DateTimeImmutable::createFromFormat('Y-m-d', $currencyMetadata['from']);

        if (\array_key_exists('to', $currencyMetadata)) {
            /**
             * @var \DateTimeImmutable $to The date format is always valid as it is generated using the CurrencyDataGenerator
             */
            $to = \DateTimeImmutable::createFromFormat('Y-m-d', $currencyMetadata['to']);
        } else {
            $to = null;
        }

        return $from <= $date && (null === $to || $to >= $date);
    }

    /**
     * @param non-empty-string   $currency    e.g. 'USD'
     * @param ?bool              $legalTender if the currency must be a legal tender. Using null do not filter anything
     * @param ?bool              $active      Indicates whether the currency should always be active for the given $date. Using null do not filter anything
     * @param \DateTimeInterface $date        the date on which the check will be performed if $active is set to true
     */
    public static function isValidInAnyCountry(
        string $currency,
        ?bool $legalTender = true,
        ?bool $active = true,
        \DateTimeInterface $date = new \DateTimeImmutable('today', new \DateTimeZone('Etc/UTC')),
    ): bool {
        foreach (self::readEntry(['Map'], 'meta') as $countryCode => $country) {
            foreach ($country as $currencyCode => $currencyMetadata) {
                if ($currencyCode !== $currency) {
                    continue;
                }

Review comment (suggestion):
we prefer not polluting the code with such SA concerns:
```suggestion
```

Code after:
     * @param \DateTimeInterface                $date             The date on which the check will be performed
     */
    private static function isDateActive(string $country, string $currency, array $currencyMetadata, \DateTimeInterface $date): bool
    {
        if (!\array_key_exists('from', $currencyMetadata)) {
            // Note: currencies that are not legal tender don't have often validity dates.
            throw new \RuntimeException("Cannot check whether the currency $currency is active or not in $country because they are no validity dates available.");
        }

        $from = \DateTimeImmutable::createFromFormat('Y-m-d', $currencyMetadata['from']);

        if (\array_key_exists('to', $currencyMetadata)) {
            $to = \DateTimeImmutable::createFromFormat('Y-m-d', $currencyMetadata['to']);
        } else {
            $to = null;
        }

        return $from <= $date && (null === $to || $to >= $date);
    }

    /**
     * @param string             $currency    e.g. 'USD'
     * @param ?bool              $legalTender If the currency must be a legal tender; null to not filter anything
     * @param ?bool              $active      Indicates whether the currency should always be active for the given $date; null to not filter anything
     * @param \DateTimeInterface $date        the date on which the check will be performed if $active is set to true
     */
    public static function isValidInAnyCountry(string $currency, ?bool $legalTender = true, ?bool $active = true, \DateTimeInterface $date = new \DateTimeImmutable('today', new \DateTimeZone('Etc/UTC'))): bool
    {
        if (!self::exists($currency)) {
            throw new \InvalidArgumentException("The currency $currency does not exist.");
        }

        foreach (self::readEntry(['Map'], 'meta') as $countryCode => $country) {
            foreach ($country as $currencyCode => $currencyMetadata) {
                if ($currencyCode !== $currency) {
                    continue;
                }

                if (null !== $legalTender && $legalTender !== self::isLegalTender($currencyMetadata)) {
                    continue;
                }

                if (null === $active) {
                    return true;
                }

                if (self::isDateActive($countryCode, $currencyCode, $currencyMetadata, $date) !== $active) {
                    continue;
                }

                return true;