{"record":{"id":"1aba1860dd7df122","repo":"briannesbitt/Carbon","slug":"argument-1-passed-to-class-method-must-be","errorCode":null,"errorMessage":"Argument 1 passed to {$class}::{$method}() must be an instance of DatePeriod or {$class}, instance of {$given} given.","messagePattern":"Argument 1 passed to (.+?)::(.+?)\\(\\) must be an instance of DatePeriod or (.+?), instance of (.+?) given\\.","errorType":"exception","errorClass":"NotAPeriodException","httpStatus":null,"severity":"error","filePath":"src/Carbon/CarbonPeriod.php","lineNumber":386,"sourceCode":"                $period->getDateInterval(),\n                $period->getOptions(),\n            );\n        }\n\n        if ($period instanceof DatePeriod) {\n            return new static(\n                $period->start,\n                $period->end ?: ($period->recurrences - 1),\n                $period->interval,\n                $period->include_start_date ? 0 : static::EXCLUDE_START_DATE,\n            );\n        }\n\n        $class = static::class;\n        $type = \\gettype($period);\n        $chunks = explode('::', __METHOD__);\n\n        throw new NotAPeriodException(\n            'Argument 1 passed to '.$class.'::'.end($chunks).'() '.\n            'must be an instance of DatePeriod or '.$class.', '.\n            ($type === 'object' ? 'instance of '.\\get_class($period) : $type).' given.',\n        );\n    }\n\n    /**\n     * Create a new instance.\n     */\n    public static function create(...$params): static\n    {\n        return static::createFromArray($params);\n    }\n\n    /**\n     * Create a new instance from an array of parameters.\n     */\n    public static function createFromArray(array $params): static","sourceCodeStart":368,"sourceCodeEnd":404,"githubUrl":"https://github.com/briannesbitt/Carbon/blob/b13f05955dcfd7da71d60af745fd148cbdb73505/src/Carbon/CarbonPeriod.php#L368-L404","documentation":"CarbonPeriod::instance(mixed $period) (src/Carbon/CarbonPeriod.php:358-390) only accepts an existing CarbonPeriod (copied) or a native DatePeriod (its start/end/interval/recurrences are read out). The parameter is deliberately typed mixed, so any other value (a Carbon date, CarbonInterval, array, string, null) cannot be converted and produces NotAPeriodException instead of a PHP TypeError.","triggerScenarios":"CarbonPeriod::instance(Carbon::parse('2021-01-01')); CarbonPeriod::instance($someCarbonInterval); CarbonPeriod::instance(['2021-01-01', '2021-12-31']); CarbonPeriod::instance('2021-01-01') — even a valid date string is rejected here, only period objects are accepted.","commonSituations":"Code that receives 'maybe a period' from a DTO/user input and calls instance() unconditionally; refactoring create() calls into instance(); passing a DatePeriod look-alike from another library (e.g. league/period) that does not extend DatePeriod.","solutions":["Branch by type: use instance() only for DatePeriod/CarbonPeriod; build with CarbonPeriod::create($start, $end) or CarbonPeriod::create($start, 'P1D', $n) otherwise","Use CarbonPeriod::make($var): it calls instance() and falls back to create() when that throws (src/Carbon/CarbonPeriod.php:346-352)","If you have the pieces, wrap them in a native period first: new DatePeriod($start, $interval, $end)"],"exampleFix":"// before\n$period = CarbonPeriod::instance($input); // explodes on Carbon/array/string\n\n// after\n$period = CarbonPeriod::make($input); // instance() for periods, create() fallback\n\n// or explicit\n$period = ($input instanceof DatePeriod || $input instanceof CarbonPeriod)\n    ? CarbonPeriod::instance($input)\n    : CarbonPeriod::create($input);","handlingStrategy":"type-guard","validationCode":null,"typeGuard":"/** @psalm-assert DatePeriod|CarbonPeriod $value */\nfunction assertPeriodLike(mixed $value): void\n{\n    if (!$value instanceof CarbonPeriod && !$value instanceof DatePeriod) {\n        throw new InvalidArgumentException(\n            'Expected CarbonPeriod|DatePeriod, got '.get_debug_type($value)\n        );\n    }\n}\n\nassertPeriodLike($input);\n$period = CarbonPeriod::instance($input);","tryCatchPattern":"try {\n    $period = CarbonPeriod::instance($input);\n} catch (\\Carbon\\Exceptions\\NotAPeriodException $e) {\n    $period = CarbonPeriod::create($input); // or reject the input\n}","preventionTips":["Type-hint parameters as CarbonPeriod|DatePeriod at your own boundaries so PHP rejects bad types earlier","Prefer CarbonPeriod::make() when the input type is not guaranteed","Never pass raw user input to instance(); it accepts only period objects by design"],"tags":["carbon","carbon-period","argument-type","type-check"],"backgroundTag":"invalid-argument-type","analyzedSha":"b13f05955dcfd7da71d60af745fd148cbdb73505","analyzedAt":"2026-08-17T04:40:56.953Z","schemaVersion":2},"datasetVersion":"2026-08-17T09:17:11.063Z"}