{"record":{"id":"b37994bb8227e27d","repo":"briannesbitt/Carbon","slug":"invalid-constructor-parameters","errorCode":null,"errorMessage":"Invalid constructor parameters.","messagePattern":"Invalid constructor parameters\\.","errorType":"exception","errorClass":"InvalidPeriodParameterException","httpStatus":null,"severity":"error","filePath":"src/Carbon/CarbonPeriod.php","lineNumber":808,"sourceCode":"            } elseif (!isset($sortedArguments['start']) && $parsedDate = $this->makeDateTime($argument)) {\n                $sortedArguments['start'] = $parsedDate;\n                $originalArguments['start'] = $argument;\n            } elseif (!isset($sortedArguments['end']) && ($parsedDate = $parsedDate ?? $this->makeDateTime($argument))) {\n                $sortedArguments['end'] = $parsedDate;\n                $originalArguments['end'] = $argument;\n            } elseif (!isset($sortedArguments['recurrences']) &&\n                !isset($sortedArguments['end']) &&\n                (\\is_int($argument) || \\is_float($argument))\n                && $argument >= 0\n            ) {\n                $sortedArguments['recurrences'] = $argument;\n            } elseif (!$optionsSet && (\\is_int($argument) || $argument === null)) {\n                $optionsSet = true;\n                $sortedArguments['options'] = (((int) $this->options) | ((int) $argument));\n            } elseif ($parsedTimezone = self::makeTimezone($argument)) {\n                $sortedArguments = $this->configureTimezone($parsedTimezone, $sortedArguments, $originalArguments);\n            } else {\n                throw new InvalidPeriodParameterException('Invalid constructor parameters.');\n            }\n        }\n\n        $this->setFromAssociativeArray($sortedArguments);\n\n        if ($this->startDate === null) {\n            $dateClass = $this->dateClass;\n            $this->setStartDate($dateClass::now());\n        }\n\n        if ($this->dateInterval === null) {\n            $this->setDateInterval(CarbonInterval::day());\n\n            $this->isDefaultInterval = true;\n        }\n\n        if ($this->options === null) {\n            $this->setOptions(0);","sourceCodeStart":790,"sourceCodeEnd":826,"githubUrl":"https://github.com/briannesbitt/Carbon/blob/b13f05955dcfd7da71d60af745fd148cbdb73505/src/Carbon/CarbonPeriod.php#L790-L826","documentation":"The CarbonPeriod constructor (and create()) sorts its variadic arguments by type: DateInterval/CarbonInterval become the interval, DateTimeInterface objects become start then end, non-negative int/float become recurrences, int/null become the options bitmask, DateTimeZone or a timezone-name string become the timezone, other strings are tried as ISO specs or dates. An argument matching none of these categories is unusable and throws InvalidPeriodParameterException (src/Carbon/CarbonPeriod.php:808).","triggerScenarios":"new CarbonPeriod('garbage'); new CarbonPeriod(-1) (negative numbers have no slot); new CarbonPeriod(true); new CarbonPeriod(['2021-01-01', '2021-03-01']) passed as one array instead of spread; new CarbonPeriod($unrelatedObject).","commonSituations":"Forwarding unvalidated user input straight into create(...$params); a null-ish variable silently becoming a bool; forgetting to spread an array of prepared arguments; passing a value object instead of a DateTime.","solutions":["Pass recognized types only: parseable date strings, DateTimeInterface, DateInterval/CarbonInterval, non-negative numbers, DateTimeZone","Spread parameter arrays: CarbonPeriod::create(...$args), not create($args)","Sanitize at the boundary: convert input to Carbon::parse()/CarbonInterval::make() results before the constructor sees them"],"exampleFix":"// before\n$period = new CarbonPeriod($params); // $params is an array -> Invalid constructor parameters\n\n// after\n$period = CarbonPeriod::create(...$params);\n\n// before\n$period = CarbonPeriod::create($userInput); // 'garbage'\n// after\n$period = CarbonPeriod::create(Carbon::parse($userInput));","handlingStrategy":"validation","validationCode":"foreach ($arguments as $argument) {\n    $ok = $argument instanceof DateTimeInterface\n        || $argument instanceof DateInterval\n        || $argument instanceof DateTimeZone\n        || \\is_string($argument)\n        || (\\is_int($argument) || \\is_float($argument)) && $argument >= 0\n        || $argument === null;\n    if (!$ok) {\n        throw new InvalidArgumentException('Unsupported CarbonPeriod argument: '.get_debug_type($argument));\n    }\n}\n$period = CarbonPeriod::create(...$arguments);","typeGuard":null,"tryCatchPattern":"try {\n    $period = CarbonPeriod::create(...$args);\n} catch (\\Carbon\\Exceptions\\InvalidPeriodParameterException $e) {\n    // surface as a 422 to the caller instead of a 500\n    throw new ValidationException('Invalid period parameters', 0, $e);\n}","preventionTips":["Never forward unvalidated request data into create(...$params); coerce each value to a known type first","Remember arrays must be spread: create(...$args) not create($args)","Negative numbers, bools and arbitrary objects have no meaning to the constructor"],"tags":["carbon","carbon-period","constructor","argument-validation","variadic"],"backgroundTag":"invalid-constructor-arguments","analyzedSha":"b13f05955dcfd7da71d60af745fd148cbdb73505","analyzedAt":"2026-08-17T04:40:56.953Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}