{"record":{"id":"d6995f726349e2a2","repo":"briannesbitt/Carbon","slug":"invalid-number-of-recurrences","errorCode":null,"errorMessage":"Invalid number of recurrences.","messagePattern":"Invalid number of recurrences\\.","errorType":"exception","errorClass":"InvalidPeriodParameterException","httpStatus":null,"severity":"error","filePath":"src/Carbon/CarbonPeriod.php","lineNumber":1346,"sourceCode":"\n        $self->handleChangedParameters();\n\n        return $self;\n    }\n\n    /**\n     * Add a recurrences filter (set maximum number of recurrences).\n     *\n     * @throws InvalidArgumentException\n     */\n    public function setRecurrences(int|float|null $recurrences): static\n    {\n        if ($recurrences === null) {\n            return $this->removeFilter(static::RECURRENCES_FILTER);\n        }\n\n        if ($recurrences < 0) {\n            throw new InvalidPeriodParameterException('Invalid number of recurrences.');\n        }\n\n        /** @var self $self */\n        $self = $this->copyIfImmutable();\n        $self->carbonRecurrences = $recurrences === INF ? INF : (int) $recurrences;\n\n        return self::addFilterOrHandleChangedParameters($self, static::RECURRENCES_FILTER);\n    }\n\n    /**\n     * Change the period start date.\n     *\n     * @param DateTime|DateTimeInterface|string $date\n     * @param bool|null                         $inclusive\n     *\n     * @throws InvalidPeriodDateException\n     *\n     * @return static","sourceCodeStart":1328,"sourceCodeEnd":1364,"githubUrl":"https://github.com/briannesbitt/Carbon/blob/b13f05955dcfd7da71d60af745fd148cbdb73505/src/Carbon/CarbonPeriod.php#L1328-L1364","documentation":"setRecurrences() bounds how many dates a period yields. A negative count is meaningless, so anything below 0 throws InvalidPeriodParameterException (src/Carbon/CarbonPeriod.php:1346). Floats are truncated to int and INF is explicitly allowed for endless periods; null removes the limit.","triggerScenarios":"->setRecurrences(-1); ->setRecurrences($count - 1) when $count is 0; ->setRecurrences($page - $offset) arithmetic that can go negative; passing a '-1 means unlimited' convention from another API.","commonSituations":"Off-by-one math producing -1; pagination code where 'limit - offset' goes negative on the last page; user input parsed with a sign error.","solutions":["Clamp to >= 0: max(0, $recurrences)","Pass null to remove the recurrence limit entirely","Use INF for a deliberate endless period: ->setRecurrences(INF)"],"exampleFix":"// before\n$period->setRecurrences($count - 1); // -1 when $count = 0\n\n// after\n$period->setRecurrences(max(0, $count - 1));","handlingStrategy":"validation","validationCode":"if ($recurrences !== null && $recurrences < 0) {\n    throw new InvalidArgumentException('Recurrences must be >= 0, got '.$recurrences);\n}\n$period->setRecurrences($recurrences);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp computed recurrence math with max(0, ...)","Use null to remove the bound and INF for endless periods instead of negative sentinels","Validate numeric request parameters (limit/offset/count) before they reach period APIs"],"tags":["carbon","carbon-period","recurrences","validation","negative-value"],"backgroundTag":"negative-value-rejected","analyzedSha":"b13f05955dcfd7da71d60af745fd148cbdb73505","analyzedAt":"2026-08-17T04:40:56.953Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}