{"record":{"id":"4cb17748943b2c10","repo":"briannesbitt/Carbon","slug":"endless-period-can-t-be-converted-to-array-nor-cou","errorCode":null,"errorMessage":"Endless period can't be converted to array nor counted.","messagePattern":"Endless period can't be converted to array nor counted\\.","errorType":"exception","errorClass":"EndLessPeriodException","httpStatus":null,"severity":"error","filePath":"src/Carbon/CarbonPeriod.php","lineNumber":1686,"sourceCode":"                    break;\n\n                default:\n                    return false;\n            }\n        }\n\n        return true;\n    }\n\n    /**\n     * Convert the date period into an array without changing current iteration state.\n     *\n     * @return CarbonInterface[]\n     */\n    public function toArray(): array\n    {\n        if ($this->isUnfilteredAndEndLess()) {\n            throw new EndLessPeriodException(\"Endless period can't be converted to array nor counted.\");\n        }\n\n        $state = [\n            $this->key,\n            $this->carbonCurrent ? $this->carbonCurrent->avoidMutation() : null,\n            $this->validationResult,\n        ];\n\n        $result = iterator_to_array($this);\n\n        [$this->key, $this->carbonCurrent, $this->validationResult] = $state;\n\n        return $result;\n    }\n\n    /**\n     * Count dates in the date period.\n     */","sourceCodeStart":1668,"sourceCodeEnd":1704,"githubUrl":"https://github.com/briannesbitt/Carbon/blob/b13f05955dcfd7da71d60af745fd148cbdb73505/src/Carbon/CarbonPeriod.php#L1668-L1704","documentation":"toArray() — and count(), which delegates to it — must materialize every date at once. isUnfilteredAndEndLess() (src/Carbon/CarbonPeriod.php:1652) returns true when the period has no custom filter and no finite bound: no recurrences filter at all, INF recurrences, or an end that is null/end-of-time. Materializing such a period would never terminate, so EndLessPeriodException is thrown instead (src/Carbon/CarbonPeriod.php:1686).","triggerScenarios":"CarbonPeriod::create('2021-01-01')->toArray(); CarbonPeriod::days(1)->count(); CarbonPeriod::create()->setRecurrences(INF)->toArray(); any period built with only a start (and the default P1D interval) then passed to count()/toArray()/iterator_to_array().","commonSituations":"Fluent building like CarbonPeriod::days(1) that looks complete but is unbounded, later hitting defensive count()/toArray() calls; generic code that counts results for logging/pagination; forgetting that an omitted end date means 'forever'.","solutions":["Bound the period before materializing: ->setRecurrences(30) or ->setEndDate($date)","Iterate lazily with foreach and stop at your own limit instead of calling toArray()","Check first: if ($period->isUnfilteredAndEndLess()) handle the infinite case explicitly (e.g. paginate by date windows)"],"exampleFix":"// before\n$dates = CarbonPeriod::create('2021-01-01')->toArray(); // endless\n\n// after\n$dates = CarbonPeriod::create('2021-01-01')\n    ->setEndDate('2021-01-31')\n    ->toArray();\n// or iterate lazily with an own limit\n$i = 0;\nforeach (CarbonPeriod::create('2021-01-01') as $date) {\n    if (++$i > 30) break;\n}","handlingStrategy":"validation","validationCode":"if ($period->isUnfilteredAndEndLess()) {\n    throw new InvalidArgumentException('Refusing to materialize an endless period');\n}\n$count = $period->count();","typeGuard":null,"tryCatchPattern":"try {\n    $dates = $period->toArray();\n} catch (\\Carbon\\Exceptions\\EndLessPeriodException $e) {\n    $dates = iterator_to_array(new \\LimitIterator($period->getIterator(), 0, 100));\n}","preventionTips":["Always set an end date or finite recurrences on periods that will be counted or arrayified","Use lazy foreach with your own break condition for potentially unbounded periods","Call isUnfilteredAndEndLess() (public) as an explicit guard in generic/reporting code"],"tags":["carbon","carbon-period","infinite-loop-guard","array","count"],"backgroundTag":"infinite-iteration","analyzedSha":"b13f05955dcfd7da71d60af745fd148cbdb73505","analyzedAt":"2026-08-17T04:40:56.953Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}